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}`; try { await run("git", ["rev-parse", tag]); console.log(`Tag ${tag} already exists.`); } catch { await run("git", ["tag", tag]); await run("git", ["push", "origin", tag]); } 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}`); } }