-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscan-apis.mjs
More file actions
150 lines (129 loc) · 4.21 KB
/
scan-apis.mjs
File metadata and controls
150 lines (129 loc) · 4.21 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
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
144
145
146
147
148
149
150
import shell from "shelljs";
import fs from "node:fs/promises";
import { fileURLToPath } from "node:url";
import path from "node:path";
shell.set("-e");
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const supportedEnvironments = ["node", "bun", "deno", "workerd"];
// args check
if (
!(
process.argv.length === 2 ||
(process.argv.length === 4 &&
process.argv[2] === "--only" &&
process.argv[3]
.split(",")
.every((env) => supportedEnvironments.includes(env)))
)
) {
console.error(`
Error: Incorrect arguments!
${process.argv.length} ${process.argv[2]} ${process.argv[3]}
This script can be called with --only option followed by comma separated list of environments to scan.
Examples:
--only workerd
--only node,workerd
Supported environments: node, bun, deno, workerd
`);
process.exit(1);
}
const regenerateEnvs =
process.argv.length === 4
? process.argv[3].split(",")
: supportedEnvironments;
if (!shell.env.FNM_DIR) {
console.error(
"You must have fnm installed to continue. Refer to README.md for instructions."
);
process.exit(1);
}
if (!shell.which("bun")) {
console.error(
"You must have bun installed to continue. Refer to README.md for instructions."
);
process.exit(1);
}
if (!shell.which("deno")) {
console.error(
"You must have deno installed to continue. Refer to README.md for instructions."
);
process.exit(1);
}
// shelljs doesn't read from .bashrc or .zshrc which normally inject FNM_DIR
// into your PATH variable, so a lookup is needed.
// Trailing space is intentional, for DX
const fnm = `${shell.env.FNM_DIR}/fnm `;
const versionMap = {};
// Compare node versions to the baseline
if (regenerateEnvs.includes("node")) {
const nodeVersions = [20, 22, 24];
for (const version of nodeVersions) {
shell.echo(`Generate node v${version} apis...`);
shell.exec(
fnm + `exec --using=${version} node node/dump.mjs --compare-to-baseline`
);
shell.echo("=== Done ====================================\n\n");
const versionOutput = shell
.exec(fnm + `exec --using=${version} node --version`, { silent: true })
.stdout.match(/v(?<version>\S+)/).groups.version;
versionMap[`node${version}`] = versionOutput;
}
}
// bun
if (regenerateEnvs.includes("bun")) {
shell.echo("Generate bun apis...");
shell.exec("bun run bun/dump.js");
shell.echo("=== Done ====================================\n\n");
versionMap["bun"] = shell
.exec(`bun --version`, { silent: true })
.stdout.match(/(?<version>\S+)/).groups.version;
}
// deno
if (regenerateEnvs.includes("deno")) {
shell.echo("Generate deno apis...");
shell.exec(
"deno run --allow-write=./data/deno.json --allow-read --allow-env deno/dump.js"
);
shell.echo("=== Done ====================================\n\n");
versionMap["deno"] = shell
.exec(`deno --version`, { silent: true })
.stdout.match(/deno (?<version>\S+)/).groups.version;
}
// workerd
if (regenerateEnvs.includes("workerd")) {
shell.echo(
'Generate `workerd --compatibility-flags="nodejs_compat"` apis...'
);
const compatibilityDate = getWorkerdDate("workerd");
shell.exec(`node workerd/dump.mjs ${compatibilityDate}`);
shell.echo("=== Done ====================================\n\n");
versionMap["workerd"] = extractNpmVersion("workerd", "workerd");
}
await fs.writeFile(
path.join(__dirname, "report", "src", "data", "versionMap.json"),
JSON.stringify(versionMap, null, 2)
);
const now = Date.now();
shell
.echo(JSON.stringify({ timestamp: now }))
.to("report/src/data/timestamp.json");
/** return YYYY-MM-DD */
function getWorkerdDate(projectName) {
const version = extractNpmVersion(projectName, "workerd");
const m = version.match(/(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})/);
if (m === null) {
throw new Error(`Invalid version ${version}`);
}
return `${m.groups?.year}-${m.groups?.month}-${m.groups?.day}`;
}
function extractNpmVersion(projectName, packageName) {
return (
shell
.exec(`pnpm --dir ./${projectName}/ list ${packageName} --depth=2`, {
silent: true,
})
.stdout.match(`${packageName} (?<version>[\\w.-]+)`)?.groups.version ??
`${packageName}@???`
);
}