-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
66 lines (54 loc) · 1.49 KB
/
run.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
const path = require('path');
const inspector = require('inspector');
const fs = require('fs/promises');
const session = new inspector.Session();
session.connect();
const TS_TYPE_MAP = {
Function: '(...args: any[]) => any',
Module: 'Record<string, any>',
Object: 'Record<string, any>',
Array: 'any[]',
Promise: 'Promise<any>',
Set: 'Set<any>',
Map: 'Map<any, any>',
WeakSet: 'WeakSet<any>',
WeakMap: 'WeakMap<any, any>',
};
function post(method) {
return new Promise((resolve, reject) => {
session.post(method, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
async function run(scriptPath) {
let exited = false;
await post('Profiler.enable');
await post('Profiler.start');
await post('Profiler.startTypeProfile');
process.on('beforeExit', async () => {
if (exited) return;
exited = true;
const { result } = await post('Profiler.takeTypeProfile');
await post('Profiler.stop');
const filtered = result
.filter(({ url }) => url.startsWith('file://'))
.map(({ url, entries }) => ({
url: url.slice('file://'.length),
entries: entries.map(({ offset, types }) => ({
offset,
types: types.map(({ name }) => TS_TYPE_MAP[name] ?? name),
})),
}));
await fs.writeFile(
path.resolve(process.cwd(), './ts-annotate-map.json'),
JSON.stringify(filtered, null, 2)
);
});
require(scriptPath);
}
module.exports = { run };