forked from arkmadj/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
46 lines (37 loc) · 1.26 KB
/
build.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
const path = require('path');
const fse = require('fs-extra');
const dirTree = require('directory-tree');
// eslint-disable-next-line no-console
console.log('Building docs...');
const tree = dirTree('.', {
extensions: /\.md$/,
exclude: /(node_modules|.vuepress|.vscode|dist|README.md)/,
normalizePath: true,
attributes: ['type', 'extension'],
});
const index = `export default ${generateIndex(tree.children)};`;
fse.ensureDirSync('dist');
fse.writeFileSync('dist/index.js', index);
// eslint-disable-next-line no-console
console.log('Built docs');
function generateIndex(tree) {
const children = tree
.map((child) => {
if (child.type === 'file') {
const baseName = path.posix.basename(child.name, child.extension);
const basePath = path.posix.join(
path.posix.dirname(child.path),
path.posix.basename(child.path, child.extension)
);
return `{name:'${baseName}',path:'${basePath}',import:()=>import('../${child.path}')}`;
} else if (child.type === 'directory') {
const children = generateIndex(child.children);
if (children === '[]') return null;
return `{name:'${child.name}',path:'${child.path}',children:${children}}`;
} else {
return null;
}
})
.filter((child) => child !== null);
return `[${children.join(',')}]`;
}