forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-content-tree.js
51 lines (43 loc) · 1.18 KB
/
build-content-tree.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
#!/usr/bin/env node
// ./build-content-tree source output
// ./build-content-tree "./src/content" ".src/_content.json"
const directoryTree = require('directory-tree');
const fs = require('fs');
const path = require('path');
// Import Utils
const { restructure } = require('../utilities/content-tree-enhancers.js');
if (require.main === module) {
main();
} else {
module.exports = buildContentTree;
}
function main() {
const source = process.argv[2];
const output = process.argv[3];
buildContentTree(source, output);
}
function buildContentTree(source, output) {
if (!source) {
return console.error('build-content-tree: you must provide a source path');
}
if (!output) {
return console.error(
'build-content-tree: you must provide a output file name'
);
}
let content = directoryTree(source, { extensions: /\.(md|mdx)/ });
content = restructure(content, {
dir: source,
});
fs.writeFileSync(
path.resolve(output),
JSON.stringify(content, 2),
(error) => {
if (error) {
console.log('scripts/build-content-tree', error);
} else {
console.log('Successfully built content tree file at ' + output);
}
}
);
}