forked from ngneat/falso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs-generator.js
133 lines (110 loc) Β· 3.43 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const jsdoc2md = require('jsdoc-to-markdown');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { minify } = require('terser');
function imageModifier(generator) {
// we chain the query param to trigger refetching of the image.
return (
'<img src={`${' + generator + '()}?${Math.random()}}`} alt="Random image"/>'
);
}
function stringModifier(generator) {
return `${generator}().toString()`;
}
const functionModifiers = {
randBoolean: stringModifier,
randFutureDate: stringModifier,
randPastDate: stringModifier,
randRecentDate: stringModifier,
randSoonDate: stringModifier,
randAvatar: imageModifier,
randImg: imageModifier,
randChanceBoolean: 'randChanceBoolean({chanceTrue: 0.78}).toString()',
rand: 'rand([1,2,3])',
randBetweenDate:
"randBetweenDate({ from: new Date('10/07/2020'), to: new Date('10/07/2025') }).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)) {
let funcCall = `${name}()`;
const modifier = functionModifiers[name];
if (modifier) {
funcCall = typeof modifier === 'function' ? modifier(name) : modifier;
}
const source = `() => ${funcCall}`;
section += `\`\`\`jsx live\nfunction Demo(props) {\n return <Preview source={${source}}/>;\n}\n\`\`\`\n\n`;
}
return section;
}