forked from ngneat/falso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs-generator.js
99 lines (78 loc) · 2.82 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const jsdoc2md = require('jsdoc-to-markdown');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { minify } = require("terser");
const functionModifiers = {
rand: 'rand([1,2,3])',
randBoolean: 'randBoolean().toString()',
randFutureDate: 'randFutureDate().toString()',
randBetweenDate: 'randBetweenDate({ from: new Date(\'10/07/2020\'), to: new Date(\'10/07/2025\') }).toString()',
randPastDate: 'randPastDate().toString()',
randRecentDate: 'randRecentDate().toString()',
randSoonDate: 'randSoonDate().toString()',
randTextRange: 'randTextRange({ min: 10, max: 100 })',
}
const skipLivePreview = ['seed'];
jsdoc2md.getTemplateData({
files: glob.sync('packages/falso/src/lib/*.ts'),
configure: 'jsdoc.json'
}).then(res => {
const categories = res.reduce((acc, current) => {
const category = current.category.toLowerCase();
// Handle multiple categories
if (category.includes(',')) {
const categories = category.split(', ');
categories.forEach((c) => {
if (!acc[c]) {
acc[c] = [];
}
acc[c].push({
...current,
category: c,
});
});
}
else {
if (!acc[category]) {
acc[category] = [];
}
acc[category].push(current);
}
return acc;
}, {});
const docsOutputPath = path.join('docs', 'docs', 'auto-generated');
if (!fs.existsSync(docsOutputPath)) {
fs.mkdirSync(docsOutputPath);
}
for (let [category, items] of Object.entries(categories)) {
let md = `---\nslug: /${category.toLowerCase()}\n---\n\n# ${capitalize(category)}\n\n`;
md += items.sort((funcA, funcB) => sortFunctions(funcA, funcB)).map(getDocsSection).join('');
fs.writeFileSync(path.join(docsOutputPath, `${category.toLowerCase()}.mdx`), md, { encoding: 'utf8' });
}
const [falsoESMPath] = glob.sync('dist/packages/falso/index.esm.js');
minify(fs.readFileSync(falsoESMPath, "utf8")).then((minified) => {
fs.writeFileSync(path.join('docs', 'src', 'theme', 'ReactLiveScope', 'falso.min.js'), minified.code);
});
});
function sortFunctions(funcA, funcB) {
if (funcA.name < funcB.name) {
return -1;
}
if (funcA.name > funcB.name) {
return 1;
}
return 0;
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getDocsSection({ name, description, examples }) {
let section = `### \`\`\`${name}\`\`\`\n\n${description}\n\n\`\`\`ts\nimport { ${name} } from '@ngneat/falso';\n\n${examples.join('\n')}\n\`\`\`\n\n`;
if (!skipLivePreview.includes(name)) {
const funcCall = functionModifiers[name] ? functionModifiers[name] : `${name}()`;
const source = `() => ${funcCall}`;
section += `\`\`\`jsx live\nfunction Demo(props) {\n return <Preview source={${source}}/>;\n}\n\`\`\`\n\n`;
}
return section;
}