-
Notifications
You must be signed in to change notification settings - Fork 18
/
generateJsDoc.js
74 lines (63 loc) · 2.12 KB
/
generateJsDoc.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
const fs = require('fs');
const { computeFileFormat } = require('@ud-viz/utils_shared');
const { exec } = require('child-process-promise');
const docScriptTag = (injectMermaidScriptTag) => {
console.info('include script tag in ./docs/static ', injectMermaidScriptTag);
const SCRIPT_TAG_MERMAID =
'\n' + '<script src="../js/jsdoc-tuts-mermaid.js"></script>';
const parseDirectory = (directoryPath) => {
const dirents = fs.readdirSync(directoryPath, { withFileTypes: true });
dirents.forEach((dirent) => {
if (dirent.isFile() && computeFileFormat(dirent.name) == 'md') {
const filePath = directoryPath + '/' + dirent.name;
// read contents of the file
let data = fs.readFileSync(filePath, {
encoding: 'utf-8',
});
if (data.includes('```mermaid')) {
// contains mermaid graph
if (injectMermaidScriptTag) {
data += SCRIPT_TAG_MERMAID;
fs.writeFileSync(filePath, data);
} else {
fs.writeFileSync(
filePath,
data.replace(new RegExp(SCRIPT_TAG_MERMAID, 'g'), '')
);
}
}
} else if (dirent.isDirectory()) {
parseDirectory(directoryPath + '/' + dirent.name); // recursive
}
});
};
parseDirectory('.');
};
const printExec = function (result) {
console.log('stdout: \n', result.stdout);
console.error('stderr: \n', result.stderr);
};
const generateDoc = () => {
return new Promise((resolve, reject) => {
const promises = [];
promises.push(
exec('jsdoc -c ./docs/jsdocConfig/jsdocHome.js').then(printExec)
);
// packages
const dirents = fs.readdirSync('./packages', { withFileTypes: true });
dirents.forEach(async (dirent) => {
if (dirent.isDirectory()) {
promises.push(
exec(
'cross-env PACKAGE=' +
dirent.name +
' jsdoc -c ./docs/jsdocConfig/jsdocPackage.js'
).then(printExec)
);
}
});
Promise.all(promises).then(resolve).catch(reject);
});
};
docScriptTag(true);
generateDoc().then(() => docScriptTag(false));