|
| 1 | +import { Command } from "commander"; |
| 2 | +import fs from "node:fs"; |
| 3 | + |
| 4 | +const program = new Command(); |
| 5 | + |
| 6 | +function logAndError(msg: string) { |
| 7 | + console.error(msg); |
| 8 | + process.exit(1); |
| 9 | +} |
| 10 | + |
| 11 | +function bumpVersion( |
| 12 | + version: string, |
| 13 | + type: "major" | "minor" | "patch" | "prerelease", |
| 14 | +) { |
| 15 | + const [major, minor, patch] = version.split(".").map((v) => |
| 16 | + Number.parseInt( |
| 17 | + // Remove everything after the - if there is one |
| 18 | + v.includes("-") ? v.split("-")[0] : v, |
| 19 | + ), |
| 20 | + ); |
| 21 | + switch (type) { |
| 22 | + case "major": |
| 23 | + return `${major + 1}.0.0`; |
| 24 | + case "minor": |
| 25 | + return `${major}.${minor + 1}.0`; |
| 26 | + case "patch": |
| 27 | + return `${major}.${minor}.${patch + 1}`; |
| 28 | + case "prerelease": |
| 29 | + return `${major}.${minor}.${patch}-pre.${Date.now()}`; |
| 30 | + default: |
| 31 | + throw new Error(`Invalid release type: ${type}`); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function getLocalVersion() { |
| 36 | + const packagejsonFile = Bun.file("package.json"); |
| 37 | + const packagejson = await packagejsonFile.json(); |
| 38 | + return packagejson.version; |
| 39 | +} |
| 40 | + |
| 41 | +async function saveVersion(version: string) { |
| 42 | + const packagejsonFile = Bun.file("package.json"); |
| 43 | + const packagejson = await packagejsonFile.json(); |
| 44 | + packagejson.version = version; |
| 45 | + await Bun.write("package.json", JSON.stringify(packagejson, null, 2)); |
| 46 | +} |
| 47 | + |
| 48 | +const COMPILE_TARGETS: string[] = [ |
| 49 | + "bun-linux-x64", |
| 50 | + "bun-linux-arm64", |
| 51 | + "bun-darwin-x64", |
| 52 | + "bun-darwin-arm64", |
| 53 | +]; |
| 54 | + |
| 55 | +async function compileDistribution() { |
| 56 | + for (const target of COMPILE_TARGETS) { |
| 57 | + const result = |
| 58 | + await Bun.$`bun build ./src/index.ts --compile --target=${target} --outfile dist/sf-${target}`; |
| 59 | + if (result.exitCode !== 0) { |
| 60 | + logAndError(`Failed to compile for ${target}`); |
| 61 | + } |
| 62 | + console.log(`✅ Compiled for ${target}`); |
| 63 | + |
| 64 | + const zipFileName = `dist/sf-${target}.zip`; |
| 65 | + const zipResult = await Bun.$`zip -j ${zipFileName} dist/sf-${target}`; |
| 66 | + if (zipResult.exitCode !== 0) { |
| 67 | + logAndError(`Failed to zip the binary for ${target}`); |
| 68 | + } |
| 69 | + console.log(`✅ Zipped binary for ${target}`); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function createRelease(version: string) { |
| 74 | + const distFiles = fs.readdirSync("./dist"); |
| 75 | + const zipFiles = distFiles |
| 76 | + .filter((entry) => fs.statSync(`./dist/${entry}`).isFile()) |
| 77 | + .filter((entry) => entry.endsWith(".zip")) |
| 78 | + .map((entry) => `./dist/${entry}`); |
| 79 | + |
| 80 | + console.log(zipFiles); |
| 81 | + |
| 82 | + const releaseFlag = version.includes("pre") ? "--prerelease" : "--latest"; |
| 83 | + const result = |
| 84 | + await Bun.$`gh release create ${version} ${zipFiles} --generate-notes ${releaseFlag}`; |
| 85 | + if (result.exitCode !== 0) { |
| 86 | + logAndError(`Failed to create GitHub release for version ${version}`); |
| 87 | + } |
| 88 | + console.log(`✅ Created GitHub release for version ${version}`); |
| 89 | + |
| 90 | + const gitAddResult = await Bun.$`git add package.json`; |
| 91 | + if (gitAddResult.exitCode !== 0) { |
| 92 | + logAndError("Failed to add package.json to git"); |
| 93 | + } |
| 94 | + console.log("✅ Added package.json to git"); |
| 95 | + |
| 96 | + const gitCommitResult = await Bun.$`git commit -m "release: v${version}"`; |
| 97 | + if (gitCommitResult.exitCode !== 0) { |
| 98 | + logAndError(`Failed to commit with message "release: v${version}"`); |
| 99 | + } |
| 100 | + console.log(`✅ Committed with message "release: v${version}"`); |
| 101 | + |
| 102 | + const gitPushResult = await Bun.$`git push origin main`; |
| 103 | + if (gitPushResult.exitCode !== 0) { |
| 104 | + logAndError("Failed to push to origin main"); |
| 105 | + } |
| 106 | + console.log("✅ Pushed to origin main"); |
| 107 | +} |
| 108 | + |
| 109 | +async function cleanDist() { |
| 110 | + fs.rmSync("./dist", { recursive: true, force: true }); |
| 111 | +} |
| 112 | + |
| 113 | +program |
| 114 | + .name("release") |
| 115 | + .description("A github release tool for the project") |
| 116 | + .arguments("[type]") |
| 117 | + .action(async (type) => { |
| 118 | + try { |
| 119 | + if (!type || type === "") { |
| 120 | + program.help(); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | + |
| 124 | + const validTypes = ["major", "minor", "patch", "prerelease"]; |
| 125 | + if (!validTypes.includes(type)) { |
| 126 | + console.error( |
| 127 | + `Invalid release type: ${type}. Valid types are: ${validTypes.join(", ")}`, |
| 128 | + ); |
| 129 | + process.exit(1); |
| 130 | + } |
| 131 | + |
| 132 | + await cleanDist(); |
| 133 | + const version = await getLocalVersion(); |
| 134 | + const bumpedVersion = bumpVersion(version, type); |
| 135 | + await saveVersion(bumpedVersion); |
| 136 | + await compileDistribution(); |
| 137 | + await createRelease(bumpedVersion); |
| 138 | + } catch (err) { |
| 139 | + console.error(err); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | +program.parse(process.argv); |
0 commit comments