2025-05-06 23:24:36 -06:00

60 lines
1.7 KiB
TypeScript

const decoder = new TextDecoder();
const denoJson = JSON.parse(await Deno.readTextFile("deno.json"));
let version = denoJson.version;
if (!version) {
console.error("Missing 'version' in deno.json");
Deno.exit(1);
}
const branch = Deno.env.get("GITHUB_REF")?.replace("refs/heads/", "") ?? "";
console.log(`Branch: ${branch}`);
if (branch.startsWith("prerelease-")) {
const suffix = branch.replace("prerelease-", "");
const base = version.split("-")[0];
const expected = `${base}-${suffix}`;
if (version !== expected) {
denoJson.version = expected;
await Deno.writeTextFile("deno.json", JSON.stringify(denoJson, null, 2));
await run("git", ["config", "user.name", "CI"]);
await run("git", ["config", "user.email", "ci@cyborggrizzly.com"]);
await run("git", ["add", "deno.json"]);
await run("git", [
"commit",
"-m",
`fix: sync version with branch ${branch}`,
]);
await run("git", ["push"]);
version = expected;
}
}
const tag = `v${version}`;
let created = true;
try {
await run("git", ["rev-parse", tag]);
console.log(`Tag ${tag} already exists.`);
created = false;
} catch {
await run("git", ["tag", tag]);
await run("git", ["push", "origin", tag]);
}
if (created) {
console.log(`::set-output name=tag_created::true`);
console.log(`::set-output name=tag_name::${tag}`);
} else {
console.log(`::set-output name=tag_created::false`);
}
async function run(cmd: string, args: string[]) {
const command = new Deno.Command(cmd, {
args,
stdout: "piped",
stderr: "piped",
});
const status = await command.output();
if (!status.success) {
const err = decoder.decode(await status.stderr);
throw new Error(`Command failed: ${cmd} ${args.join(" ")}\n${err}`);
}
}