-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgen.js
96 lines (89 loc) · 2.77 KB
/
gen.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
const fs = require('fs');
const path = require('path');
const TypeDoc = require('typedoc');
const json = require('./typedoc.json');
const workspaceDirs = ['packages', 'plugins'];
const ignorePackages = ['turbox-snippets'];
const entryPath = './src/index.ts';
const clEntryPath = './CHANGELOG.md';
const genEnv = process.env.GEN_ENV;
const GEN_ENV = {
TYPEDOC: 'typedoc',
CHANGELOG: 'changelog',
DTS: 'dts',
};
const cwd = process.cwd();
async function main() {
const entryPoints = [];
const clEntryPoints = [];
/** Generate index.d.ts */
if (genEnv === GEN_ENV.DTS) {
const pkg = require(path.resolve(cwd, './package.json'));
fs.writeFileSync(path.resolve(cwd, './dist/index.d.ts'), `export * from '../typings/index';\n\ndeclare module '${pkg.name}';\n`);
return;
}
workspaceDirs.forEach(dir => {
const files = fs.readdirSync(dir);
for (let i = files.length - 1; i >= 0; i--) {
const file = files[i];
const fullPath = `${dir}/${file}`;
const stat = fs.lstatSync(fullPath);
if (stat.isDirectory() && !ignorePackages.some(pkgName => fullPath.includes(pkgName))) {
const entry = path.resolve(fullPath, entryPath);
const clEntry = path.resolve(fullPath, clEntryPath);
if (fs.existsSync(entry)) {
entryPoints.push(entry);
}
if (fs.existsSync(clEntry)) {
clEntryPoints.push(clEntry);
}
}
}
});
/** Generate type doc */
if (genEnv === GEN_ENV.TYPEDOC && entryPoints.length) {
try {
const app = await TypeDoc.Application.bootstrapWithPlugins({
entryPoints,
});
const project = await app.convert();
if (project) {
const outputDir = json.out;
// Rendered docs
await app.generateDocs(project, outputDir);
// Alternatively generate JSON output
// await app.generateJson(project, outputDir + "/documentation.json");
}
} catch (error) {
console.error(error);
}
}
/** Generate changelog */
if (genEnv === GEN_ENV.CHANGELOG && clEntryPoints.length) {
const writerStream = fs.createWriteStream('./docs/CHANGELOG.md');
writerStream.on('finish', () => {
console.log('*** Generate changelog success! ***');
});
writerStream.on('error', (err) => {
console.error(err);
});
function mergeFile(entryPoints, ws) {
if (!entryPoints.length) {
return ws.end();
}
const rs = fs.createReadStream(entryPoints.shift());
rs.pipe(ws, { end: false });
rs.on('end', () => {
if (entryPoints.length) {
ws.write('\n');
}
mergeFile(entryPoints, ws);
});
rs.on('error', (error) => {
ws.destroy(error);
});
}
mergeFile(clEntryPoints, writerStream);
}
}
main().catch(console.error);