-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathdocs-generator.js
132 lines (115 loc) Β· 3.92 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
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) => {
try {
if (!current.category) throw new Error('No category found');
const category = current.category.toLowerCase();
// Handle multiple categories
if (category.includes(',')) {
const categoryList = category.split(', ');
categoryList.forEach((c) => {
acc[c] = acc[c] || [];
acc[c].push({ ...current, category: c });
});
} else {
acc[category] = acc[category] || [];
acc[category].push(current);
}
return acc;
} catch (error) {
console.warn(`Skipping entry due to error: ${error.message}`);
return acc;
}
}, {});
const docsOutputPath = path.join('docs', 'docs', 'auto-generated');
fs.mkdirSync(docsOutputPath, { recursive: true });
if (Object.keys(categories).length > 0) {
for (const [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' }
);
}
} else {
console.warn('No categories found in the documentation data.');
}
const [falsoESMPath] = glob.sync('dist/packages/falso/index.esm.js');
if (falsoESMPath) {
const fileContent = fs.readFileSync(falsoESMPath, 'utf8');
minify(fileContent).then((minified) => {
const minifiedPath = path.join(
'docs',
'src',
'theme',
'ReactLiveScope',
'falso.min.js'
);
fs.writeFileSync(minifiedPath, minified.code);
});
} else {
console.warn('falso ESM file not found.');
}
})
.catch((err) => {
console.error(`Error generating documentation: ${err.message}`);
});
function sortFunctions(funcA, funcB) {
return funcA.name.localeCompare(funcB.name);
}
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;
}