|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { createHash } from "node:crypto"; |
| 5 | +import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises"; |
| 6 | +import path from "node:path"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | +const packageDir = path.resolve(__dirname, ".."); |
| 12 | +const packageJsonPath = path.join(packageDir, "package.json"); |
| 13 | +const releaseDir = path.join(packageDir, "dist", "release"); |
| 14 | +const stageDir = path.join(releaseDir, "stage"); |
| 15 | +const binaryName = "nylio"; |
| 16 | + |
| 17 | +const parseArgs = (argv) => { |
| 18 | + const values = { |
| 19 | + repo: process.env.GITHUB_REPOSITORY ?? "", |
| 20 | + tag: process.env.GITHUB_REF_NAME ?? "", |
| 21 | + }; |
| 22 | + |
| 23 | + for (let index = 0; index < argv.length; index += 1) { |
| 24 | + const arg = argv[index]; |
| 25 | + if (arg === "--repo") { |
| 26 | + values.repo = argv[index + 1] ?? ""; |
| 27 | + index += 1; |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if (arg === "--tag") { |
| 32 | + values.tag = argv[index + 1] ?? ""; |
| 33 | + index += 1; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return values; |
| 38 | +}; |
| 39 | + |
| 40 | +const run = (command, args, cwd) => { |
| 41 | + const result = spawnSync(command, args, { |
| 42 | + cwd, |
| 43 | + stdio: "inherit", |
| 44 | + env: process.env, |
| 45 | + }); |
| 46 | + |
| 47 | + if (result.status !== 0) { |
| 48 | + process.exit(result.status ?? 1); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +const sha256File = async (filePath) => { |
| 53 | + const content = await readFile(filePath); |
| 54 | + return createHash("sha256").update(content).digest("hex"); |
| 55 | +}; |
| 56 | + |
| 57 | +const buildFormula = ({ repo, version, tag, arm64, x64 }) => `class Nylio < Formula |
| 58 | + desc "CLI for the Nylio public API" |
| 59 | + homepage "https://github.com/${repo}" |
| 60 | + version "${version}" |
| 61 | +
|
| 62 | + on_macos do |
| 63 | + if Hardware::CPU.arm? |
| 64 | + url "https://github.com/${repo}/releases/download/${tag}/${arm64.assetName}" |
| 65 | + sha256 "${arm64.sha256}" |
| 66 | + else |
| 67 | + url "https://github.com/${repo}/releases/download/${tag}/${x64.assetName}" |
| 68 | + sha256 "${x64.sha256}" |
| 69 | + end |
| 70 | + end |
| 71 | +
|
| 72 | + def install |
| 73 | + bin.install "${binaryName}" |
| 74 | + end |
| 75 | +
|
| 76 | + test do |
| 77 | + output = shell_output("#{bin}/${binaryName} --help") |
| 78 | + assert_match "CLI for the Nylio public API.", output |
| 79 | + end |
| 80 | +end |
| 81 | +`; |
| 82 | + |
| 83 | +const main = async () => { |
| 84 | + const { repo, tag } = parseArgs(process.argv.slice(2)); |
| 85 | + const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8")); |
| 86 | + const version = packageJson.version; |
| 87 | + const expectedTag = `nylio-cli-v${version}`; |
| 88 | + |
| 89 | + if (!repo) { |
| 90 | + throw new Error("Missing --repo <owner/repo> or GITHUB_REPOSITORY."); |
| 91 | + } |
| 92 | + |
| 93 | + if (!tag) { |
| 94 | + throw new Error("Missing --tag <release-tag> or GITHUB_REF_NAME."); |
| 95 | + } |
| 96 | + |
| 97 | + if (tag !== expectedTag) { |
| 98 | + throw new Error( |
| 99 | + `Release tag ${tag} does not match package version ${version}. Expected ${expectedTag}.`, |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + await rm(releaseDir, { force: true, recursive: true }); |
| 104 | + await mkdir(stageDir, { recursive: true }); |
| 105 | + |
| 106 | + const targets = [ |
| 107 | + { bunTarget: "bun-darwin-arm64", suffix: "darwin-arm64" }, |
| 108 | + { bunTarget: "bun-darwin-x64", suffix: "darwin-x64" }, |
| 109 | + ]; |
| 110 | + |
| 111 | + const artifacts = []; |
| 112 | + |
| 113 | + for (const target of targets) { |
| 114 | + const stageName = `nylio-cli-${version}-${target.suffix}`; |
| 115 | + const stagePath = path.join(stageDir, stageName); |
| 116 | + const outputPath = path.join(stagePath, binaryName); |
| 117 | + const archiveName = `${stageName}.tar.gz`; |
| 118 | + const archivePath = path.join(releaseDir, archiveName); |
| 119 | + |
| 120 | + await mkdir(stagePath, { recursive: true }); |
| 121 | + |
| 122 | + run( |
| 123 | + "bun", |
| 124 | + [ |
| 125 | + "build", |
| 126 | + "--compile", |
| 127 | + `--target=${target.bunTarget}`, |
| 128 | + `--outfile=${outputPath}`, |
| 129 | + "./src/index.ts", |
| 130 | + ], |
| 131 | + packageDir, |
| 132 | + ); |
| 133 | + |
| 134 | + await chmod(outputPath, 0o755); |
| 135 | + |
| 136 | + run("tar", ["-czf", archivePath, "-C", stageDir, stageName], packageDir); |
| 137 | + |
| 138 | + artifacts.push({ |
| 139 | + target: target.suffix, |
| 140 | + assetName: archiveName, |
| 141 | + assetPath: archivePath, |
| 142 | + sha256: await sha256File(archivePath), |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + const checksums = artifacts |
| 147 | + .map((artifact) => `${artifact.sha256} ${artifact.assetName}`) |
| 148 | + .join("\n"); |
| 149 | + |
| 150 | + await writeFile(path.join(releaseDir, "SHA256SUMS"), `${checksums}\n`); |
| 151 | + |
| 152 | + const arm64 = artifacts.find((artifact) => artifact.target === "darwin-arm64"); |
| 153 | + const x64 = artifacts.find((artifact) => artifact.target === "darwin-x64"); |
| 154 | + |
| 155 | + if (!arm64 || !x64) { |
| 156 | + throw new Error("Missing macOS release artifacts."); |
| 157 | + } |
| 158 | + |
| 159 | + const formulaDir = path.join(releaseDir, "homebrew"); |
| 160 | + await mkdir(formulaDir, { recursive: true }); |
| 161 | + await writeFile( |
| 162 | + path.join(formulaDir, "nylio.rb"), |
| 163 | + buildFormula({ repo, version, tag, arm64, x64 }), |
| 164 | + ); |
| 165 | + |
| 166 | + console.log(`Release artifacts written to ${releaseDir}`); |
| 167 | +}; |
| 168 | + |
| 169 | +await main().catch((error) => { |
| 170 | + console.error(error instanceof Error ? error.message : String(error)); |
| 171 | + process.exit(1); |
| 172 | +}); |
0 commit comments