-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.publish.ts
254 lines (230 loc) · 7.01 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 👉 usage example: `bun pub --bump=1.2.3`
import { parseJSONC, parseJSON5 } from "confbox";
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";
// TODO: implement @reliverse/bump npm library
function showHelp() {
console.log(
`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) {
console.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" });
}
console.log("success", "Published to npm successfully.");
} catch (error) {
console.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 --dry-run", {
stdio: "inherit",
});
} else {
await execaCommand("bun build:jsr", { stdio: "inherit" });
await execaCommand("bunx jsr publish --allow-slow-types --allow-dirty", {
stdio: "inherit",
});
}
console.log("success", "Published to JSR successfully.");
} catch (error) {
console.error(
"❌ Failed to publish to JSR:",
error instanceof Error ? error.message : String(error),
);
process.exit(1);
}
}
async function bumpVersions(oldVersion: string, newVersion: string) {
try {
// Find all relevant files
const codebase = await globby("**/*.{reliverse,json,jsonc,json5,ts}", {
ignore: [
"**/node_modules/**",
"**/.git/**",
"**/dist/**",
"**/build/**",
"**/.next/**",
"**/coverage/**",
"**/.cache/**",
"**/tmp/**",
"**/.temp/**",
"**/package-lock.json",
"**/pnpm-lock.yaml",
"**/yarn.lock",
"**/bun.lockb",
],
});
// Track which files were updated
const updatedFiles: string[] = [];
// Process each file
for (const file of codebase) {
try {
const content = await fs.readFile(file, "utf-8");
// Handle different file types
if (file.endsWith(".reliverse")) {
const parsed = parseJSONC(content);
if (parsed && typeof parsed === "object" && "version" in parsed) {
parsed.version = newVersion;
await fs.writeFile(file, `${JSON.stringify(parsed, null, 2)}\n`);
updatedFiles.push(file);
continue;
}
} else if (file.endsWith(".json")) {
const parsed = destr(content);
if (parsed && typeof parsed === "object" && "version" in parsed) {
parsed.version = newVersion;
await fs.writeFile(file, `${JSON.stringify(parsed, null, 2)}\n`);
updatedFiles.push(file);
continue;
}
} else if (file.endsWith(".jsonc")) {
const parsed = parseJSONC(content);
if (parsed && typeof parsed === "object" && "version" in parsed) {
parsed.version = newVersion;
await fs.writeFile(file, `${JSON.stringify(parsed, null, 2)}\n`);
updatedFiles.push(file);
continue;
}
} else if (file.endsWith(".json5")) {
const parsed = parseJSON5(content);
if (parsed && typeof parsed === "object" && "version" in parsed) {
parsed.version = newVersion;
await fs.writeFile(file, `${JSON.stringify(parsed, null, 2)}\n`);
updatedFiles.push(file);
continue;
}
}
// For other files (including .ts), do string replacement if version is found
if (content.includes(oldVersion)) {
const updated = content.replaceAll(oldVersion, newVersion);
await fs.writeFile(file, updated);
updatedFiles.push(file);
}
} catch (error) {
console.warn(
`Failed to process ${file}:`,
error instanceof Error ? error.message : String(error),
);
}
}
if (updatedFiles.length > 0) {
console.log(
`Version updated from ${oldVersion} to ${newVersion}`,
`Updated ${String(updatedFiles.length)} files:`,
updatedFiles.join("\n"),
);
} else {
console.warn("No files were updated with the new version");
}
} catch (error) {
console.error(
"Failed to bump versions:",
error instanceof Error ? error.message : String(error),
);
throw error;
}
}
async function main() {
try {
const { jsr, "dry-run": dryRun } = argv as unknown as {
jsr: boolean;
"dry-run": boolean;
};
const newVersion = argv._[0];
if (!newVersion) {
console.log("No version specified, skipping version bump");
} else {
// Validate version format
if (!/^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/.test(newVersion)) {
throw new Error(
"Invalid version format. Must be a valid semver (e.g., 1.2.3, 1.2.3-beta.1)",
);
}
// Read current version
const pkgPath = path.resolve("package.json");
if (!(await fs.pathExists(pkgPath))) {
throw new Error("package.json not found");
}
const pkg = destr<{ version: string }>(
await fs.readFile(pkgPath, "utf-8"),
);
if (!pkg.version) {
throw new Error("No version field found in package.json");
}
const oldVersion = pkg.version;
if (oldVersion === newVersion) {
console.log(`No version change required: already at ${oldVersion}`);
} else {
await bumpVersions(oldVersion, newVersion);
}
}
// Proceed with publishing
if (jsr) {
await publishJsr(dryRun);
} else {
await publishNpm(dryRun);
}
} catch (error) {
console.error(
"❌ An unexpected error occurred:",
error instanceof Error ? error.message : String(error),
);
process.exit(1);
}
}
main().catch((error: unknown) => {
console.error(
"❌ An unexpected error occurred:",
error instanceof Error ? error.message : String(error),
);
process.exit(1);
});