forked from ngneat/falso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs-generator.js
49 lines (37 loc) · 1.56 KB
/
docs-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const jsdoc2md = require('jsdoc-to-markdown');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { minify } = require("terser");
jsdoc2md.getTemplateData({
files : glob.sync('packages/falso/src/lib/*.ts'),
configure: 'jsdoc.json'
}).then(res => {
const categories = res.reduce((acc, current) => {
const c = current.category.toLowerCase();
if(!acc[c]) {
acc[c] = [];
}
acc[c].push(current);
return acc;
}, {});
const docsOutputPath = path.join('docs', 'docs', 'auto-generated');
if (!fs.existsSync(docsOutputPath)) {
fs.mkdirSync(docsOutputPath);
}
for(const [category, items] of Object.entries(categories)) {
let md = `---\nslug: /${category.toLowerCase()}\n---\n\n# ${capitalize(category)}\n\n`;
const funcs = items.map(item => {
return `### \`\`\`${item.name}\`\`\`\n\n${item.description}\n\n\`\`\`ts\nimport { ${item.name} } from '@ngneat/falso';\n\n${item.examples.join('\n')}\n\`\`\`\n\n\`\`\`jsx live\nfunction Demo(props) {\n return <Preview source={() => ${item.name}()}/>;\n}\n\`\`\`\n\n`;
});
md += funcs.join('');
fs.writeFileSync(path.join(docsOutputPath, `${category.toLowerCase()}.mdx`), md, { encoding: 'utf8' });
}
const [falsoESMPath] = glob.sync('dist/**/index.esm.js');
minify(fs.readFileSync(falsoESMPath, "utf8")).then((minified) => {
fs.writeFileSync(path.join('docs', 'src', 'theme', 'ReactLiveScope', 'falso.min.js'), minified.code);
});
});
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}