-
Notifications
You must be signed in to change notification settings - Fork 677
/
rebuild_specs.js
143 lines (128 loc) · 4.81 KB
/
rebuild_specs.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
133
134
135
136
137
138
139
140
141
142
143
// @ts-check
const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');
const TypeDoc = require('..');
const ts = require('typescript');
const app = new TypeDoc.Application();
app.bootstrap({
mode: 'Modules',
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
experimentalDecorators: true,
jsx: ts.JsxEmit.React,
lib: [
"lib.dom.d.ts",
"lib.es5.d.ts",
"lib.es2015.iterable.d.ts",
"lib.es2015.collection.d.ts"
],
name: 'typedoc',
excludeExternals: true
});
// Note that this uses the test files in dist, not in src, this is important since
// when running the tests we copy the tests to dist and then convert them.
const base = path.join(__dirname, '../dist/test/converter');
/** @type {[string, () => void, () => void][]} */
const conversions = [
['specs', () => { }, () => { }],
['specs.d',
() => app.options.setValue('includeDeclarations', true),
() => app.options.setValue('includeDeclarations', false)
],
['specs-without-exported',
() => app.options.setValue('excludeNotExported', true),
() => app.options.setValue('excludeNotExported', false)
],
['specs-with-lump-categories',
() => app.options.setValue('categorizeByGroup', false),
() => app.options.setValue('categorizeByGroup', true)
],
];
/**
* Rebuilds the converter specs for the provided dirs.
* @param {string[]} dirs
*/
function rebuildConverterTests(dirs) {
return Promise.all(dirs.map(fullPath => {
console.log(fullPath);
const src = app.expandInputFiles([fullPath]);
return Promise.all(conversions.map(([file, before, after]) => {
const out = path.join(fullPath, `${file}.json`);
if (fs.existsSync(out)) {
TypeDoc.resetReflectionID();
before();
const result = app.convert(src);
const serialized = app.serializer.toObject(result);
const data = JSON.stringify(serialized, null, ' ')
.split(TypeDoc.normalizePath(base))
.join('%BASE%');
after();
return fs.writeFile(out.replace('dist', 'src'), data);
}
}));
}));
}
async function rebuildRendererTest() {
await fs.remove(path.join(__dirname, '../src/test/renderer/specs'));
const src = path.join(__dirname, '../examples/basic/src');
const out = path.join(__dirname, '../src/test/renderer/specs');
await fs.remove(out)
app.options.setValue('excludeExternals', false);
app.generateDocs(app.expandInputFiles([src]), out)
app.options.setValue('excludeExternals', true);
await fs.remove(path.join(out, 'assets'))
/**
* Avoiding sync methods here is... difficult.
* @param {string} base
* @param {string} dir
* @param {string[]} results
* @returns {string[]}
*/
function getFiles(base, dir = '', results = []) {
const files = fs.readdirSync(path.join(base, dir));
for (const file of files) {
const relativeToBase = path.join(dir, file);
if (fs.statSync(path.join(base, relativeToBase)).isDirectory()) {
getFiles(base, relativeToBase, results);
} else {
results.push(relativeToBase);
}
}
return results;
}
const gitHubRegExp = /https:\/\/github.com\/[A-Za-z0-9\-]+\/typedoc\/blob\/[^\/]*\/examples/g;
return getFiles(out).map(file => {
const full = path.join(out, file);
return fs.readFile(full, { encoding: 'utf-8' })
.then(text => fs.writeFile(
full,
text.replace(gitHubRegExp, 'https://github.com/sebastian-lenz/typedoc/blob/master/examples')
));
});
}
async function main(command = 'all', filter = '') {
if (!['all', 'converter', 'renderer'].includes(command)) {
console.error('Invalid command. Usage: node scripts/rebuild_specs.js <all|converter|renderer> [filter]');
throw new Error();
}
if (['all', 'converter'].includes(command)) {
const dirs = await Promise.all((await fs.readdir(base)).map(dir => {
const dirPath = path.join(base, dir);
return Promise.all([dirPath, fs.stat(dirPath)]);
}));
await rebuildConverterTests(dirs.filter(([fullPath, stat]) => {
if (!stat.isDirectory()) return false;
return fullPath.endsWith(filter);
}).map(([path]) => path));
} else if (filter !== '') {
console.warn('Specifying a filter when rebuilding render specs only has no effect.');
}
if (['all', 'renderer'].includes(command)) {
await rebuildRendererTest();
}
}
main(process.argv[2], process.argv[3]).catch(reason => {
console.error(reason);
process.exit(1);
});