-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheck-bun-node.ts
More file actions
215 lines (191 loc) · 5.79 KB
/
check-bun-node.ts
File metadata and controls
215 lines (191 loc) · 5.79 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env bun
/**
* Usage:
* bun check-bun-node.ts --bun canary,latest
* bun check-bun-node.ts --node 20,22,24,25
*/
// @ts-expect-error - no types
import nodevu from "@nodevu/core";
import { $ } from "bun";
const nodevuData = await nodevu({ fetch });
/**
* Filters Node.js release data to return only major releases with documented support.
*/
async function getMajorNodeReleases() {
return Object.entries(
nodevuData as Record<
string,
{
releases: Record<
string,
{
modules: { version: string };
dependencies: { npm: string; v8: string };
semver: {
major: number;
minor: number;
patch: number;
raw: string;
};
releaseDate: string;
}
>;
support: {
phases: {
dates: {
start: string;
lts: string;
maintenance: string;
end: string;
};
};
codename: string;
};
}
>
).filter(([version, { support }]) => {
// Filter out those without documented support
// Basically those not in schedule.json
if (!support) {
return false;
}
// nodevu returns duplicated v0.x versions (v0.12, v0.10, ...).
// This behavior seems intentional as the case is hardcoded in nodevu,
// see https://github.com/cutenode/nodevu/blob/0c8538c70195fb7181e0a4d1eeb6a28e8ed95698/core/index.js#L24.
// This line ignores those duplicated versions and takes the latest
// v0.x version (v0.12.18). It is also consistent with the legacy
// nodejs.org implementation.
if (version.startsWith("v0.") && version !== "v0.12") {
return false;
}
return true;
});
}
// Gets the appropriate release status for each major release
const getNodeReleaseStatus = (
now: Date,
support: {
endOfLife: string;
maintenanceStart: string;
ltsStart: string;
currentStart: string;
}
) => {
const { endOfLife, maintenanceStart, ltsStart, currentStart } = support;
if (endOfLife && now >= new Date(endOfLife)) {
return "End-of-life";
}
if (maintenanceStart && now >= new Date(maintenanceStart)) {
return "Maintenance LTS";
}
if (ltsStart && now >= new Date(ltsStart)) {
return "Active LTS";
}
if (currentStart && now >= new Date(currentStart)) {
return "Current";
}
return "Pending";
};
/**
* This method is used to generate the Node.js Release Data
* for self-consumption during RSC and Static Builds
*
* @returns {Promise<Array<import('../../types').NodeRelease>>}
*/
const generateReleaseData = async () => {
const majors = await getMajorNodeReleases();
return majors.map(([, major]) => {
const [latestVersion] = Object.values(major.releases);
const support = {
currentStart: major.support.phases.dates.start,
ltsStart: major.support.phases.dates.lts,
maintenanceStart: major.support.phases.dates.maintenance,
endOfLife: major.support.phases.dates.end,
};
// Get the major release status based on our Release Schedule
const status = getNodeReleaseStatus(new Date(), support);
const minorVersions = Object.entries(major.releases).map(([, release]) => ({
modules: release.modules.version || "",
npm: release.dependencies.npm || "",
releaseDate: release.releaseDate,
v8: release.dependencies.v8,
version: release.semver.raw,
versionWithPrefix: `v${release.semver.raw}`,
}));
if (!latestVersion) {
return null;
}
return {
...support,
status,
major: latestVersion.semver.major,
version: latestVersion.semver.raw,
versionWithPrefix: `v${latestVersion.semver.raw}`,
codename: major.support.codename || "",
isLts: status.endsWith("LTS"),
npm: latestVersion.dependencies.npm || "",
v8: latestVersion.dependencies.v8,
releaseDate: latestVersion.releaseDate,
modules: latestVersion.modules.version || "",
minorVersions,
};
});
};
async function getNpmDistTags(
pkgName: string
): Promise<Record<string, string>> {
const url = `https://registry.npmjs.org/${pkgName}`;
const response = await fetch(url);
if (!response.ok)
throw new Error(`Fetch failed for ${pkgName}: ${response.status}`);
const data = (await response.json()) as Record<
string,
string | Record<string, string>
>;
return data["dist-tags"] as Record<string, string>;
}
async function getNpmDistTagsFallback(
pkgName: string
): Promise<Record<string, string>> {
try {
const { stdout } = await $`npm view ${pkgName} dist-tags --json`.quiet();
return JSON.parse(stdout.toString().trim());
} catch {
return {};
}
}
async function getVersions(
pkgName: string,
tags: Array<string>
): Promise<Array<string>> {
try {
const tagsData = await getNpmDistTags(pkgName);
return tags.map((tag) => tagsData[tag] || "").filter(Boolean);
} catch {
const tagsData = await getNpmDistTagsFallback(pkgName);
return tags.map((tag) => tagsData[tag] || "").filter(Boolean);
}
}
/**
* This will detect, wether --bun or --node is requested
*/
const main = async () => {
if (process.argv.includes("--bun")) {
const arg = process.argv.find((a) => a.startsWith("--bun"))!;
const tagsArg =
arg.split("=")[1] ?? process.argv[process.argv.indexOf("--bun") + 1];
const tags = (tagsArg || "latest").split(",");
const versions = await getVersions("bun", tags);
console.log(versions.join(","));
return;
}
if (process.argv.includes("--node")) {
console.log(
(await generateReleaseData())
.filter((release) => [20, 22, 24, 25].includes(release?.major || 0))
.map((release) => release?.versionWithPrefix.replace("v", ""))
.join(",")
);
}
};
await main();