|
1 | 1 | import fs from 'node:fs'; |
2 | | -import {builtinModules} from 'node:module'; |
| 2 | +import process from 'node:process'; |
| 3 | +import {builtinModules, isBuiltin} from 'node:module'; |
| 4 | +import childProcess from 'node:child_process'; |
3 | 5 |
|
4 | | -const final = builtinModules |
5 | | - .filter(x => !/^_|^sys$/.test(x)) |
6 | | - .sort(); |
| 6 | +const JOB_BUILD_LIST = 'buildList'; |
| 7 | +const LIST_FILE = 'builtin-modules.json'; |
| 8 | +const NODE_PROTOCOL = 'node:'; |
7 | 9 |
|
8 | | -fs.writeFileSync('builtin-modules.json', JSON.stringify(final, undefined, '\t') + '\n'); |
| 10 | +const stripNodeProtocol = name => name.startsWith(NODE_PROTOCOL) ? name.slice(NODE_PROTOCOL.length) : name; |
| 11 | +const deprecatedModules = new Set(['node:sys', 'node:punycode'].flatMap(name => [name, stripNodeProtocol(name)])); |
| 12 | + |
| 13 | +const sorter = (nameA, nameB) => { |
| 14 | + const nameAWithoutProtocol = stripNodeProtocol(nameA); |
| 15 | + const nameBWithoutProtocol = stripNodeProtocol(nameB); |
| 16 | + |
| 17 | + if (nameAWithoutProtocol === nameBWithoutProtocol) { |
| 18 | + return nameA.startsWith(NODE_PROTOCOL) ? -1 : 1; |
| 19 | + } |
| 20 | + |
| 21 | + return nameAWithoutProtocol.localeCompare(nameBWithoutProtocol); |
| 22 | +}; |
| 23 | + |
| 24 | +function runInChildProcess(flag) { |
| 25 | + childProcess.execFileSync(process.execPath, [flag, import.meta.filename], { |
| 26 | + encoding: 'utf8', |
| 27 | + env: { |
| 28 | + JOB: JOB_BUILD_LIST, |
| 29 | + }, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function run() { |
| 34 | + if (!process.argv.includes('--incremental')) { |
| 35 | + fs.writeFileSync(LIST_FILE, '[]'); |
| 36 | + } |
| 37 | + |
| 38 | + console.log('Building list without flags...'); |
| 39 | + buildList(); |
| 40 | + |
| 41 | + for (const flag of process.allowedNodeEnvironmentFlags) { |
| 42 | + if (!flag.startsWith('--experimental-')) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + console.log(); |
| 47 | + console.log(`Building list with '${flag}'...`); |
| 48 | + |
| 49 | + try { |
| 50 | + runInChildProcess(flag); |
| 51 | + } catch {} |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function buildList() { |
| 56 | + const existing = new Set( |
| 57 | + fs.existsSync(LIST_FILE) |
| 58 | + ? JSON.parse(fs.readFileSync(LIST_FILE)) |
| 59 | + : [], |
| 60 | + ); |
| 61 | + const found = builtinModules |
| 62 | + .filter(name => !existing.has(name) && !name.startsWith('_')) |
| 63 | + .flatMap(name => name.startsWith(NODE_PROTOCOL) ? [name] : [name, `${NODE_PROTOCOL}${name}`]) |
| 64 | + .filter(name => !deprecatedModules.has(name) && isBuiltin(name)); |
| 65 | + |
| 66 | + if (found.length === 0) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const final = [...existing, ...found].sort((nameA, nameB) => sorter(nameA, nameB)); |
| 71 | + |
| 72 | + fs.writeFileSync(LIST_FILE, JSON.stringify(final, undefined, '\t') + '\n'); |
| 73 | + |
| 74 | + console.log(`${final.length} module(s) found:\n${final.map(name => ` - ${name}`).join('\n')}`); |
| 75 | +} |
| 76 | + |
| 77 | +if (process.env.JOB === JOB_BUILD_LIST) { |
| 78 | + buildList(); |
| 79 | +} else { |
| 80 | + run(); |
| 81 | +} |
0 commit comments