76 lines
2.0 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 {
try {
await run("git", ["tag", tag]);
await run("git", ["push", "origin", tag]);
} catch {
created = false;
}
}
if (created) {
const out = Deno.env.get("GITHUB_OUTPUT");
if (out) {
await Deno.writeTextFile(out, `tag_created=true\ntag_name=${tag}\n`, {
append: true,
});
} else {
console.error("GITHUB_OUTPUT not set.");
}
} else {
const out = Deno.env.get("GITHUB_OUTPUT");
if (out) {
await Deno.writeTextFile(out, `tag_created=false\n`, { append: true });
} else {
console.error("GITHUB_OUTPUT not set.");
}
Deno.exit(0);
}
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}`);
}
}