refactor: refactor build scripts

This commit is contained in:
Benjy Cui 2016-03-08 15:35:03 +08:00
parent c4b2981e53
commit 0b328f81cc
4 changed files with 9 additions and 71 deletions

View File

@ -9,8 +9,8 @@ module.exports = function buildCommon(dirs, outputFile) {
.filter((file) => !/install_en\.md$/gi.test(file)); // TODO
let content = 'module.exports = {';
mds.forEach((md) => {
content += `\n '${md}': require('antd-md!../../${md}'),`;
mds.forEach((fileName) => {
content += `\n '${fileName}': require('antd-md?fileName=${fileName}!../../${fileName}'),`;
});
content += '\n};';

View File

@ -3,48 +3,18 @@
const fs = require('fs');
const path = require('path');
const R = require('ramda');
const devil = require('./devil'); // TODO: extract as a module?
const utils = require('./utils');
const isIntro = function isIntro(element) {
const type = element.type;
return type !== 'h1' && type !== 'ul' && type !== 'code' && type !== 'hr';
};
const isCode = R.whereEq({ type: 'code', props: { lang: 'jsx' } });
const isStyle = R.whereEq({ type: 'code', props: { lang: 'css' } });
const getChildren = R.compose(R.prop('children'), R.defaultTo({}));
const sortByOrder = R.sortBy(R.prop('order'));
const parseDemosList = function parseDemosList(demoList) {
const demos = R.map((fileName) => {
const data = utils.parseFileContent(fileName);
const parts = fileName.split(path.sep);
const demo = {};
demo.order = parseInt(utils.parseMeta(data).order);
const demoIndex = parts.indexOf('demo');
demo.parent = parts.slice(0, demoIndex).join('/') + '/index.md';
demo.id = 'components-' + demo.parent + '-demo-' + path.basename(fileName, '.md');
demo.title = data[0].children;
demo.intro = data.filter(isIntro);
demo.code = getChildren(data.find(isCode));
demo.preview = devil(demo.code, ['React', 'ReactDOM', '_antd', 'BrowserDemo']);
demo.style = getChildren(data.find(isStyle));
return demo;
}, demoList);
const demosList = R.groupBy((demo) => demo.parent.replace('-', ''), demos);
const sortedDemosList = R.mapObjIndexed(sortByOrder, demosList);
return sortedDemosList;
};
module.exports = function buildDemosList(demoList, outputPath) {
const isDemo = R.compose(R.test(/\/demo$/i), path.dirname);
module.exports = function buildDemosList(dirs, outputPath) {
const mds = utils.findMDFile(dirs);
const demos = R.filter(isDemo, mds);
const groupedDemos = R.groupBy((fileName) => {
const parts = fileName.split(path.sep);
const demoIndex = parts.indexOf('demo');
const relativeIndex = path.join(parts.slice(0, demoIndex).join(path.sep), 'index.md');
return relativeIndex;
}, demoList);
}, demos);
let content = 'module.exports = {';
Object.keys(groupedDemos).forEach((key) => {
@ -58,4 +28,3 @@ module.exports = function buildDemosList(demoList, outputPath) {
fs.writeFile(outputPath, content);
};
module.exports.parse = parseDemosList;

View File

@ -5,24 +5,15 @@
// Ensure that data directory exist.
require('mkdirp').sync('./_site/data');
const fs = require('fs');
const path = require('path');
const R = require('ramda');
const utils = require('./utils');
const buildDemosList = require('./build-demos-list');
buildDemosList(['./components', './docs'], './_site/data/demos-list.js');
const buildCommon = require('./build-common');
const mds = utils.findMDFile(['./components', './docs']);
const isDemo = R.compose(R.test(/\/demo$/i), path.dirname);
const demos = R.filter(isDemo, mds);
buildDemosList(demos, './_site/data/demos-list.js');
buildCommon([
'./components',
'./docs/react',
'./CHANGELOG.md',
], './_site/data/react-components.js');
buildCommon('./docs/practice', './_site/data/practice.js');
buildCommon('./docs/pattern', './_site/data/pattern.js');
buildCommon('./docs/spec', './_site/data/spec.js');

View File

@ -33,28 +33,6 @@ exports.findMDFile = function findMDFile(fileName, shallow) {
return mds;
};
exports.isIndex = R.compose(R.equals('index.md'), R.unary(path.basename));
const MT = require('mark-twain');
exports.parseFileContent = R.pipe(
fs.readFileSync,
R.toString,
MT,
R.prop('content')
);
const parseBasicMeta = R.pipe(
R.path(['1', 'children']),
R.map((child) => R.split(/:\s?/, child.children)),
R.fromPairs
);
const parseEnglishTitle = R.path(['0', 'children']);
exports.parseMeta = function parseMeta(fileContent) {
const meta = parseBasicMeta(fileContent);
meta.english = parseEnglishTitle(fileContent);
return meta;
};
exports.stringify = function stringify(data, d) {
const depth = d || 1;