forked from GoogleChrome/developer.chrome.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
104 lines (90 loc) · 2.47 KB
/
parse.js
File metadata and controls
104 lines (90 loc) · 2.47 KB
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
const typedoc = require('typedoc');
const {LogLevel: TypeDocLogLevel} = require('typedoc/dist/lib/utils');
const path = require('path');
const fs = require('fs');
const debug = true;
const modules = [
'../preamble.d.ts',
'node_modules/workbox-routing/index.d.ts',
'node_modules/workbox-core/index.d.ts',
];
const outputPath = path.join(
__dirname,
'../../site/_data/namespaces-source.json'
);
// Work from cache dir, so node resolve stuff.
process.chdir(path.join(__dirname, 'cache'));
class Transform {
/**
* @param {typedoc.JSONOutput.ProjectReflection} project
*/
constructor(project) {
this.project = project;
}
/**
* @return {Promise<typedoc.JSONOutput.DeclarationReflection[]>}
*/
async run() {
this.walk(this.project);
for (const entrypoint of this.project.children ?? []) {
const lastPart = path.basename(entrypoint.name);
console.info(lastPart);
}
return this.project.children ?? [];
}
/**
* @param {typedoc.JSONOutput.DeclarationReflection} node
*/
walk(node) {
if (node.children) {
node.children = node.children.filter(c => this.filter(c));
for (const c of node.children) {
this.walk(c);
}
}
}
/**
* @param {typedoc.JSONOutput.DeclarationReflection} node
* @return {boolean}
*/
filter(node) {
// nb. All of our parsed namespaces are external.
return !(node.flags?.isPrivate || node.name.startsWith('_'));
}
}
(async function run() {
// TODO: trigger "npm update" or install
const app = new typedoc.Application();
app.options.addReader(new typedoc.TSConfigReader());
app.bootstrap({
entryPoints: modules,
logger(message, level) {
switch (level) {
case TypeDocLogLevel.Warn:
case TypeDocLogLevel.Error:
throw new Error(`failed to parse typedoc: ${message}`);
}
},
});
app.options.setCompilerOptions(
modules,
{
// nb. just for workbox; change for other parse
lib: ['lib.webworker.d.ts'],
declaration: true,
},
undefined
);
const reflection = app.convert();
if (!reflection) {
throw new Error(`failed to convert modules: ${modules}`);
}
const json = app.serializer.projectToObject(reflection);
const t = new Transform(json);
const out = await t.run();
const render = debug
? JSON.stringify(out, undefined, 2)
: JSON.stringify(out);
fs.writeFileSync(outputPath, render);
console.info('Written', out.length, 'namespaces to', outputPath);
})();