-
-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathbump-nightly.ts
More file actions
executable file
·139 lines (119 loc) · 3.64 KB
/
bump-nightly.ts
File metadata and controls
executable file
·139 lines (119 loc) · 3.64 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
import { promises as fsp } from "node:fs";
import { execaCommand } from "execa";
import { glob } from "tinyglobby";
import { resolve } from "pathe";
const nightlyPackages = {
// h3: "h3-nightly",
} as Record<string, string>;
async function loadPackage(dir: string) {
const pkgPath = resolve(dir, "package.json");
const data = JSON.parse(await fsp.readFile(pkgPath, "utf8").catch(() => "{}"));
const save = () => fsp.writeFile(pkgPath, JSON.stringify(data, null, 2) + "\n");
const updateDeps = (reviver: (dep: any) => any) => {
for (const type of [
"dependencies",
"devDependencies",
"optionalDependencies",
"peerDependencies",
]) {
if (!data[type]) {
continue;
}
for (const e of Object.entries(data[type])) {
const dep = { name: e[0], range: e[1], type };
delete data[type][dep.name];
const updated = reviver(dep) || dep;
data[updated.type] = data[updated.type] || {};
data[updated.type][updated.name] = updated.range;
}
}
};
return {
dir,
data,
save,
updateDeps,
};
}
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;
type Package = ThenArg<ReturnType<typeof loadPackage>>;
async function loadWorkspace(dir: string) {
const workspacePkg = await loadPackage(dir);
const pkgDirs = await glob(workspacePkg.data.workspaces || [], {
onlyDirectories: true,
});
const packages: Package[] = [workspacePkg];
for (const pkgDir of pkgDirs) {
const pkg = await loadPackage(pkgDir);
if (!pkg.data.name) {
continue;
}
packages.push(pkg);
}
const find = (name: string) => {
const pkg = packages.find((pkg) => pkg.data.name === name);
if (!pkg) {
throw new Error("Workspace package not found: " + name);
}
return pkg;
};
const rename = (from: string, to: string) => {
find(from).data.name = to;
for (const pkg of packages) {
pkg.updateDeps((dep) => {
if (dep.name === from && !dep.range.startsWith("npm:")) {
dep.range = "npm:" + to + "@" + dep.range;
}
});
}
};
const setVersion = (name: string, newVersion: string) => {
find(name).data.version = newVersion;
for (const pkg of packages) {
pkg.updateDeps((dep) => {
if (dep.name === name) {
dep.range = newVersion;
}
});
}
};
const save = () => Promise.all(packages.map((pkg) => pkg.save()));
return {
dir,
workspacePkg,
packages,
save,
find,
rename,
setVersion,
};
}
function fmtDate(d: Date): string {
// YYMMDD-HHMMSS: 20240919-140954
const date = joinNumbers([d.getFullYear(), d.getMonth() + 1, d.getDate()]);
const time = joinNumbers([d.getHours(), d.getMinutes(), d.getSeconds()]);
return `${date}-${time}`;
}
function joinNumbers(items: number[]): string {
return items.map((i) => (i + "").padStart(2, "0")).join("");
}
async function main() {
const workspace = await loadWorkspace(process.cwd());
const commit = await execaCommand("git rev-parse --short HEAD").then((r) => r.stdout.trim());
for (const pkg of workspace.packages.filter((p) => !p.data.private)) {
workspace.setVersion(pkg.data.name, `${pkg.data.version}-${fmtDate(new Date())}.${commit}`);
workspace.rename(pkg.data.name, pkg.data.name + "-nightly");
pkg.updateDeps((dep) => {
if (nightlyPackages[dep.name]) {
dep.range = "npm:" + nightlyPackages[dep.name] + "@latest";
}
});
}
await workspace.save();
}
// eslint-disable-next-line unicorn/prefer-top-level-await
main().catch((error) => {
console.error(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});