-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscan-apis.mjs
184 lines (161 loc) · 5.21 KB
/
scan-apis.mjs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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",
"wrangler-v3",
"wrangler-unenv",
];
// 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 wrangler-unenv
--only workerd,wrangler-unenv
Supported environments: node, bun, deno, workerd, wrangler-v3, and wrangler-unenv
`);
process.exit(1);
}
const regenerateEnvs =
process.argv.length === 4
? process.argv[3].split(",")
: supportedEnvironments;
if (!shell.env.VOLTA_HOME) {
console.error(
"You must have volta 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 VOLTA_HOME
// into your PATH variable, so a lookup is needed.
// Trailing space is intentional, for DX
const volta = `${shell.env.VOLTA_HOME}/bin/volta `;
const versionMap = {};
// Compare node versions to the baseline
if (regenerateEnvs.includes("node")) {
const nodeVersions = [18, 20, 22];
for (const version of nodeVersions) {
shell.echo(`Generate node v${version} apis...`);
shell.exec(
volta + `run --node ${version} node node/dump.mjs --compare-to-baseline`
);
shell.echo("=== Done ====================================\n\n");
const versionOutput = shell
.exec(volta + `run --node ${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("wrangler-unenv-polyfills");
shell.exec(`node workerd/dump.mjs ${compatibilityDate}`);
shell.echo("=== Done ====================================\n\n");
versionMap["workerd"] = extractNpmVersion("workerd", "workerd");
}
// wrangler-v3 (with polyfills)
if (regenerateEnvs.includes("wrangler-v3")) {
shell.echo("Generate `wrangler --node_compat` apis...");
shell.exec(volta + "run --node 20 node wrangler-v3-polyfills/dump.mjs");
shell.echo("=== Done ====================================\n\n");
versionMap["wranglerV3"] = extractNpmVersion(
"wrangler-v3-polyfills",
"wrangler"
);
}
// wrangler-unenv
if (regenerateEnvs.includes("wrangler-unenv")) {
shell.echo(
'Generate `wrangler --compatibility-flags="nodejs_compat"` apis...'
);
const compatibilityDate = getWorkerdDate("wrangler-unenv-polyfills");
shell.exec(
`${volta} run --node 20 node wrangler-unenv-polyfills/dump.mjs ${compatibilityDate}`
);
shell.echo("=== Done ====================================\n\n");
versionMap["wranglerUnenv"] = extractNpmVersion(
"wrangler-unenv-polyfills",
"wrangler"
);
}
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}@???`
);
}