-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmake.js
81 lines (64 loc) · 2.14 KB
/
make.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
import fs from 'node:fs';
import process from 'node:process';
import {builtinModules, isBuiltin} from 'node:module';
import childProcess from 'node:child_process';
const JOB_BUILD_LIST = 'buildList';
const LIST_FILE = 'builtin-modules.json';
const NODE_PROTOCOL = 'node:';
const stripNodeProtocol = name => name.startsWith(NODE_PROTOCOL) ? name.slice(NODE_PROTOCOL.length) : name;
const deprecatedModules = new Set(['node:sys', 'node:punycode'].flatMap(name => [name, stripNodeProtocol(name)]));
const sorter = (nameA, nameB) => {
const nameAWithoutProtocol = stripNodeProtocol(nameA);
const nameBWithoutProtocol = stripNodeProtocol(nameB);
if (nameAWithoutProtocol === nameBWithoutProtocol) {
return nameA.startsWith(NODE_PROTOCOL) ? -1 : 1;
}
return nameAWithoutProtocol.localeCompare(nameBWithoutProtocol);
};
function runInChildProcess(flag) {
childProcess.execFileSync(process.execPath, [flag, import.meta.filename], {
encoding: 'utf8',
env: {
JOB: JOB_BUILD_LIST,
},
});
}
function run() {
if (!process.argv.includes('--incremental')) {
fs.writeFileSync(LIST_FILE, '[]');
}
console.log('Building list without flags...');
buildList();
for (const flag of process.allowedNodeEnvironmentFlags) {
if (!flag.startsWith('--experimental-')) {
continue;
}
console.log();
console.log(`Building list with '${flag}'...`);
try {
runInChildProcess(flag);
} catch {}
}
}
function buildList() {
const existing = new Set(
fs.existsSync(LIST_FILE)
? JSON.parse(fs.readFileSync(LIST_FILE))
: [],
);
const found = builtinModules
.filter(name => !existing.has(name) && !name.startsWith('_'))
.flatMap(name => name.startsWith(NODE_PROTOCOL) ? [name] : [name, `${NODE_PROTOCOL}${name}`])
.filter(name => !deprecatedModules.has(name) && isBuiltin(name));
if (found.length === 0) {
return;
}
const final = [...existing, ...found].sort((nameA, nameB) => sorter(nameA, nameB));
fs.writeFileSync(LIST_FILE, JSON.stringify(final, undefined, '\t') + '\n');
console.log(`${final.length} module(s) found:\n${final.map(name => ` - ${name}`).join('\n')}`);
}
if (process.env.JOB === JOB_BUILD_LIST) {
buildList();
} else {
run();
}