feat: add siteConfig loader

This commit is contained in:
endiliey 2018-07-28 16:06:07 +08:00
parent 06faff3474
commit 94c45e36cb
11 changed files with 4726 additions and 51 deletions

2
.gitignore vendored
View file

@ -4,4 +4,4 @@
.eslintcache
node_modules
dist
yarn-error.log

0
lib/loader/blog.js Normal file
View file

14
lib/loader/config.js Normal file
View file

@ -0,0 +1,14 @@
const fs = require('fs-extra');
const path = require('path');
module.exports = function loadConfig(sourceDir, deleteCache = true) {
const configPath = path.resolve(sourceDir, '.blogi', 'config.js');
if (deleteCache) {
delete require.cache[configPath];
}
let config = {};
if (fs.existsSync(configPath)) {
config = require(configPath);
}
return config;
};

12
lib/loader/index.js Normal file
View file

@ -0,0 +1,12 @@
const path = require('path');
const loadConfig = require('./config');
module.exports = async function load(sourceDir) {
// 1. load siteConfig
const siteConfig = loadConfig(sourceDir);
// 2. extract metadata from markdown files
const metadatas = [];
return null; // todo
};

View file

@ -9,9 +9,9 @@
"scripts": {
"dev": "node bin/blogi dev blog",
"build": "node bin/blogi build blog",
"prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\"",
"lint": "eslint --cache \"lib/**/*.js\" \"bin/**/*.js\"",
"test": "echo \"Error: no test specified\" && exit 1"
"prettier": "prettier --config .prettierrc --write \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"",
"lint": "eslint --cache \"lib/**/*.js\" \"bin/**/*.js\" \"test/**/*.js\"",
"test": "jest"
},
"repository": {
"type": "git",
@ -35,14 +35,31 @@
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.9.1",
"jest": "^23.4.2",
"prettier": "^1.13.7"
},
"dependencies": {
"chalk": "^2.4.1",
"chokidar": "^2.0.4",
"commander": "^2.16.0",
"semver": "^5.5.0"
"connect-history-api-fallback": "^1.5.0",
"fs-extra": "^7.0.0",
"koa-connect": "^2.0.1",
"koa-convert": "^1.2.0",
"koa-mount": "^3.0.0",
"koa-range": "^0.3.0",
"koa-static": "^5.0.0",
"ora": "^3.0.0",
"semver": "^5.5.0",
"webpack": "^4.16.3",
"webpack-chain": "^4.8.0",
"webpack-serve": "^2.0.2"
},
"engines": {
"node": ">=8"
},
"jest": {
"testPathIgnorePatterns": ["/node_modules/", "__fixtures__"],
"testEnvironment": "node"
}
}

View file

@ -0,0 +1,6 @@
module.exports = {
title: 'Hello World',
description: 'Hello World',
dest: 'blogi',
base: 'blogi'
};

View file

@ -0,0 +1,5 @@
---
title: Hello, World !
---
Hello World

View file

@ -0,0 +1,4 @@
module.exports = {
title: 'Hello World',
description: 'Hello World'
};

View file

@ -0,0 +1,5 @@
---
title: Hello, World !
---
Hello World

View file

@ -0,0 +1,28 @@
const path = require('path');
const fs = require('fs');
const loadConfig = require('../../lib/loader/config');
describe('loadConfig', () => {
const simpleDir = path.join(__dirname, '__fixtures__', 'simple');
const customDir = path.join(__dirname, '__fixtures__', 'custom');
test('simple', () => {
expect(loadConfig(simpleDir)).toMatchInlineSnapshot(`
Object {
"description": "Hello World",
"title": "Hello World",
}
`);
});
test('custom', () => {
expect(loadConfig(customDir)).toMatchInlineSnapshot(`
Object {
"base": "blogi",
"description": "Hello World",
"dest": "blogi",
"title": "Hello World",
}
`);
});
});

4676
yarn.lock

File diff suppressed because it is too large Load diff