-
-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathbump-version.ts
More file actions
executable file
·94 lines (80 loc) · 2.85 KB
/
bump-version.ts
File metadata and controls
executable file
·94 lines (80 loc) · 2.85 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
#!/bin/env node
import { promises as fsp } from "node:fs";
import { resolve } from "pathe";
const c = {
cyan: (s: string) => `\x1B[36m${s}\x1B[0m`,
green: (s: string) => `\x1B[32m${s}\x1B[0m`,
yellow: (s: string) => `\x1B[33m${s}\x1B[0m`,
gray: (s: string) => `\x1B[90m${s}\x1B[0m`,
red: (s: string) => `\x1B[31m${s}\x1B[0m`,
bold: (s: string) => `\x1B[1m${s}\x1B[0m`,
};
export function fmtDate(d: Date): string {
const y = d.getFullYear() % 100;
const m = (d.getMonth() + 1).toString().padStart(2, "0");
const day = d.getDate().toString().padStart(2, "0");
return `${y}${m}${day}`;
}
async function fetchExistingVersions(pkgName: string): Promise<string[]> {
const url = `https://registry.npmjs.org/${pkgName}`;
console.log(c.gray(`Fetching versions from npm registry for ${c.cyan(pkgName)}...`));
try {
const res = await fetch(url);
if (!res.ok) {
console.log(c.yellow(` Registry returned ${res.status}, assuming first release`));
return [];
}
const data = (await res.json()) as { versions?: Record<string, unknown> };
const versions = Object.keys(data.versions || {});
return versions;
} catch (err) {
console.log(c.yellow(` Failed to fetch registry: ${err}`));
return [];
}
}
export async function resolveVersion(
pkgName: string,
dateStr: string,
prerelease = "beta"
): Promise<string> {
const versions = await fetchExistingVersions(pkgName);
const base = `3.0.${dateStr}`;
const prefix = prerelease ? `${base}-${prerelease}` : base;
const matching = versions.filter((v) => v.startsWith(prefix));
const sep = prerelease ? "." : "-";
let max = 0;
for (const v of matching) {
const rest = v.slice(prefix.length);
if (rest === "") {
max = Math.max(max, 1);
} else if (rest.startsWith(sep)) {
const n = Number.parseInt(rest.slice(1), 10);
if (!Number.isNaN(n)) {
max = Math.max(max, n);
}
}
}
const version = max === 0 ? prefix : `${prefix}${sep}${max + 1}`;
console.log(c.gray(` Resolved version: ${c.cyan(version)}`));
return version;
}
async function main() {
console.log(c.bold("\nBump version to beta\n"));
const pkgPath = resolve(process.cwd(), "package.json");
const pkg = JSON.parse(await fsp.readFile(pkgPath, "utf8"));
const oldVersion = pkg.version;
const dateStr = fmtDate(new Date());
console.log(c.gray(`Date: ${c.cyan(dateStr)}`));
const newVersion = await resolveVersion(pkg.name, dateStr);
console.log();
console.log(` ${c.cyan(pkg.name)} ${c.gray(oldVersion)} → ${c.green(newVersion)}`);
pkg.version = newVersion;
await fsp.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
console.log(c.green(`\nDone!\n`));
}
if (process.argv[1] && import.meta.url.endsWith(process.argv[1])) {
main().catch((error) => {
console.error(c.red(`\nError: ${error.message}\n`));
process.exit(1);
});
}