ant-design/scripts/build-demos-list.js

43 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-02-29 14:27:11 +08:00
const fs = require('fs');
const path = require('path');
const R = require('ramda');
const utils = require('./utils');
2016-03-08 15:35:03 +08:00
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);
2016-03-05 10:45:01 +08:00
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;
2016-03-08 15:35:03 +08:00
}, demos);
2016-03-05 10:45:01 +08:00
2016-03-21 16:49:59 +08:00
let content =
'const React = require("react");\n' +
'const ReactDOM = require("react-dom");\n' +
'module.exports = {';
2016-03-05 10:45:01 +08:00
Object.keys(groupedDemos).forEach((key) => {
content += `\n '${key}': [`;
groupedDemos[key].forEach((fileName) => {
2016-04-14 11:27:31 +08:00
const requirePath = path.relative(path.dirname(outputPath), fileName);
2016-04-08 15:29:04 +08:00
content += `\n require('${requirePath}'),`;
2016-03-05 10:45:01 +08:00
});
2016-04-14 11:27:31 +08:00
content += '\n ],';
2016-03-05 10:45:01 +08:00
});
2016-03-21 16:49:59 +08:00
content += '\n};\n';
// Extract preview as a component
content +=
'Object.keys(module.exports).map((key) => module.exports[key])\n' +
' .forEach((demos) => {\n' +
' demos.forEach((demo) => {\n' +
' if (typeof demo.preview !== "function") return;\n' +
2016-04-05 14:17:45 +08:00
' demo.preview = demo.preview(React, ReactDOM);\n' +
2016-03-21 16:49:59 +08:00
' });\n' +
' });';
2016-02-29 14:27:11 +08:00
fs.writeFile(outputPath, content);
};