-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.publish.ts
164 lines (146 loc) · 4.18 KB
/
build.publish.ts
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
// 👉 usage example: `bun pub --bump=1.2.3`
import { relinka } from "@reliverse/relinka";
import { destr } from "destr";
import { execaCommand } from "execa";
import fs from "fs-extra";
import { globby } from "globby";
import mri from "mri";
import path from "pathe";
function showHelp() {
relinka(
"info",
`Usage: bun tsx build.publish.ts [newVersion] [options]
Arguments:
newVersion The new version to set (e.g. 1.2.3)
Options:
--jsr Publish to JSR registry
--dry-run Perform a dry run of the publish process
-h, --help Show help
`,
);
}
const argv = mri(process.argv.slice(2), {
alias: {
h: "help",
},
boolean: ["jsr", "dry-run", "help"],
default: {
jsr: false,
"dry-run": false,
help: false,
},
});
// If help flag is present, display help and exit
if (argv["help"]) {
showHelp();
process.exit(0);
}
// Handle flags
const validFlags = ["jsr", "dry-run", "help", "h"];
const unknownFlags = Object.keys(argv).filter(
(key) => !validFlags.includes(key) && key !== "_",
);
if (unknownFlags.length > 0) {
relinka("error", `❌ Unknown flag(s): ${unknownFlags.join(", ")}`);
showHelp();
process.exit(1);
}
async function publishNpm(dryRun: boolean) {
try {
if (dryRun) {
await execaCommand("npm publish --dry-run", { stdio: "inherit" });
} else {
await execaCommand("bun build:npm", { stdio: "inherit" });
await execaCommand("npm publish", { stdio: "inherit" });
}
relinka("success", "Published to npm successfully.");
} catch (error) {
relinka(
"error",
"❌ Failed to publish to npm:",
error instanceof Error ? error.message : String(error),
);
process.exit(1);
}
}
async function publishJsr(dryRun: boolean) {
try {
if (dryRun) {
await execaCommand("bunx jsr publish --allow-dirty --dry-run", {
stdio: "inherit",
});
} else {
await execaCommand("bun build:jsr", { stdio: "inherit" });
await execaCommand("bunx jsr publish --allow-dirty", {
stdio: "inherit",
});
}
relinka("success", "Published to JSR successfully.");
} catch (error) {
relinka(
"error",
"❌ Failed to publish to JSR:",
error instanceof Error ? error.message : String(error),
);
process.exit(1);
}
}
async function bumpVersions(oldVersion: string, newVersion: string) {
// Update package.json
const pkgPath = path.resolve("package.json");
const pkg = destr<{ version: string }>(await fs.readFile(pkgPath, "utf-8"));
pkg.version = newVersion;
await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2));
// Update jsr.jsonc
const jsrPath = path.resolve("jsr.jsonc");
if (await fs.pathExists(jsrPath)) {
const jsrConfig = destr<{ version: string }>(
await fs.readFile(jsrPath, "utf-8"),
);
jsrConfig.version = newVersion;
await fs.writeFile(jsrPath, JSON.stringify(jsrConfig, null, 2));
}
// Replace version in src/**/*.ts
const tsFiles = await globby("src/**/*.ts");
for (const file of tsFiles) {
const content = await fs.readFile(file, "utf-8");
if (content.includes(oldVersion)) {
const updated = content.replaceAll(oldVersion, newVersion);
await fs.writeFile(file, updated);
}
}
relinka("success", `Version updated from ${oldVersion} to ${newVersion}`);
}
async function main() {
const { jsr, "dry-run": dryRun } = argv as unknown as {
jsr: boolean;
"dry-run": boolean;
};
const newVersion = argv._[0]; // The new version provided by the user (if any)
if (newVersion) {
// Perform version bump
const pkg = destr<{ version: string }>(
await fs.readFile("package.json", "utf-8"),
);
const oldVersion = pkg.version;
if (oldVersion !== newVersion) {
await bumpVersions(oldVersion, newVersion);
} else {
relinka("info", `No version change required: already at ${oldVersion}`);
}
}
// After potential bump, proceed with publishing
if (jsr) {
await publishJsr(dryRun);
} else {
await publishNpm(dryRun);
}
}
main().catch((error) => {
relinka(
"error",
"❌ An unexpected error occurred:",
error instanceof Error ? error.message : String(error),
);
process.exit(1);
});