|
| 1 | +import { mkdtemp, writeFile, mkdir } from "node:fs/promises"; |
| 2 | +import { join, basename } from "node:path"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { parseArgs } from "node:util"; |
| 5 | +import { |
| 6 | + renderFormula, |
| 7 | + createArchive, |
| 8 | + computeChecksum, |
| 9 | + parseMajorVersion, |
| 10 | + HOMEBREW_TARGETS, |
| 11 | + type FormulaInput, |
| 12 | +} from "./lib/homebrew.ts"; |
| 13 | +import { run } from "./lib/npm.ts"; |
| 14 | + |
| 15 | +const DEFAULT_ARTIFACTS_DIR = |
| 16 | + process.env.ARTIFACTS_DIR ?? join(import.meta.dir, "../dist/artifacts"); |
| 17 | + |
| 18 | +const { values } = parseArgs({ |
| 19 | + options: { |
| 20 | + version: { type: "string" }, |
| 21 | + "artifacts-dir": { type: "string" }, |
| 22 | + "tap-repo": { type: "string" }, |
| 23 | + "dry-run": { type: "boolean", default: false }, |
| 24 | + }, |
| 25 | + strict: true, |
| 26 | +}); |
| 27 | + |
| 28 | +if (!values.version) { |
| 29 | + console.error("--version is required."); |
| 30 | + process.exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +const version = values.version; |
| 34 | +const artifactsDir = values["artifacts-dir"] ?? DEFAULT_ARTIFACTS_DIR; |
| 35 | +const tapRepo = values["tap-repo"] ?? "clerk/homebrew-stable"; |
| 36 | +const dryRun = values["dry-run"]!; |
| 37 | + |
| 38 | +if (!dryRun && !process.env.HOMEBREW_TAP_TOKEN) { |
| 39 | + console.error("HOMEBREW_TAP_TOKEN is required (use --dry-run to skip)."); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | + |
| 43 | +console.log(`Homebrew distribution: v${version}${dryRun ? " (dry run)" : ""}`); |
| 44 | +console.log(`Artifacts dir: ${artifactsDir}`); |
| 45 | +console.log(`Tap repo: ${tapRepo}`); |
| 46 | + |
| 47 | +const workDir = await mkdtemp(join(tmpdir(), "homebrew-archives-")); |
| 48 | +console.log(`Work directory: ${workDir}`); |
| 49 | + |
| 50 | +const archivePaths = new Map<string, string>(); |
| 51 | + |
| 52 | +for (const target of HOMEBREW_TARGETS) { |
| 53 | + const binaryPath = join(artifactsDir, `clerk-${target.name}`, "clerk"); |
| 54 | + const archivePath = join(workDir, `homebrew-clerk-${target.name}.tar.gz`); |
| 55 | + console.log(`Creating archive for ${target.name}...`); |
| 56 | + createArchive(binaryPath, archivePath); |
| 57 | + archivePaths.set(target.name, archivePath); |
| 58 | +} |
| 59 | + |
| 60 | +const tagName = `v${version}`; |
| 61 | +for (const target of HOMEBREW_TARGETS) { |
| 62 | + const archivePath = archivePaths.get(target.name)!; |
| 63 | + if (dryRun) { |
| 64 | + console.log(`[dry-run] Would upload ${basename(archivePath)} to ${tagName}`); |
| 65 | + } else { |
| 66 | + console.log(`Uploading ${basename(archivePath)} to ${tagName}...`); |
| 67 | + run(["gh", "release", "upload", tagName, archivePath, "--clobber"]); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +console.log("Computing checksums..."); |
| 72 | +const checksums = {} as FormulaInput["checksums"]; |
| 73 | +for (const target of HOMEBREW_TARGETS) { |
| 74 | + const archivePath = archivePaths.get(target.name)!; |
| 75 | + checksums[target.name] = await computeChecksum(archivePath); |
| 76 | +} |
| 77 | + |
| 78 | +for (const target of HOMEBREW_TARGETS) { |
| 79 | + console.log(` ${target.name}: ${checksums[target.name]}`); |
| 80 | +} |
| 81 | + |
| 82 | +const formula = renderFormula({ version, checksums }); |
| 83 | +console.log("\nRendered formula:"); |
| 84 | +console.log(formula); |
| 85 | + |
| 86 | +const major = parseMajorVersion(version); |
| 87 | +const versionedFormula = renderFormula({ version, checksums, major }); |
| 88 | +console.log("\nRendered versioned formula:"); |
| 89 | +console.log(versionedFormula); |
| 90 | + |
| 91 | +if (dryRun) { |
| 92 | + console.log("[dry-run] Skipping tap clone and push."); |
| 93 | + console.log(`[dry-run] Would write Formula/clerk.rb and Formula/clerk@${major}.rb`); |
| 94 | +} else { |
| 95 | + const token = process.env.HOMEBREW_TAP_TOKEN!; |
| 96 | + |
| 97 | + const tapWorkDir = join(workDir, "tap-workdir"); |
| 98 | + console.log(`Cloning tap repo ${tapRepo}...`); |
| 99 | + run(["git", "clone", `https://github.com/${tapRepo}.git`, tapWorkDir]); |
| 100 | + const setUrlResult = Bun.spawnSync( |
| 101 | + [ |
| 102 | + "git", |
| 103 | + "remote", |
| 104 | + "set-url", |
| 105 | + "origin", |
| 106 | + `https://x-access-token:${token}@github.com/${tapRepo}.git`, |
| 107 | + ], |
| 108 | + { cwd: tapWorkDir, stdio: ["ignore", "pipe", "pipe"] }, |
| 109 | + ); |
| 110 | + if (setUrlResult.exitCode !== 0) { |
| 111 | + throw new Error("Failed to configure authenticated remote for tap repo"); |
| 112 | + } |
| 113 | + |
| 114 | + const formulaDir = join(tapWorkDir, "Formula"); |
| 115 | + await mkdir(formulaDir, { recursive: true }); |
| 116 | + const formulaPath = join(formulaDir, "clerk.rb"); |
| 117 | + await writeFile(formulaPath, formula, "utf-8"); |
| 118 | + console.log(`Wrote formula to ${formulaPath}`); |
| 119 | + |
| 120 | + const versionedFormulaPath = join(formulaDir, `clerk@${major}.rb`); |
| 121 | + await writeFile(versionedFormulaPath, versionedFormula, "utf-8"); |
| 122 | + console.log(`Wrote versioned formula to ${versionedFormulaPath}`); |
| 123 | + |
| 124 | + run(["git", "config", "user.name", "clerk-bot"], { cwd: tapWorkDir }); |
| 125 | + run(["git", "config", "user.email", "bot@clerk.com"], { cwd: tapWorkDir }); |
| 126 | + run(["git", "add", "Formula/clerk.rb", `Formula/clerk@${major}.rb`], { cwd: tapWorkDir }); |
| 127 | + |
| 128 | + const diffResult = Bun.spawnSync(["git", "diff", "--cached", "--quiet"], { |
| 129 | + cwd: tapWorkDir, |
| 130 | + stdio: ["ignore", "pipe", "pipe"], |
| 131 | + }); |
| 132 | + |
| 133 | + if (diffResult.exitCode === 0) { |
| 134 | + console.log("No changes to formula, skipping commit and push."); |
| 135 | + } else { |
| 136 | + console.log(`Committing and pushing formula for clerk ${version}...`); |
| 137 | + run(["git", "commit", "-m", `clerk ${version}`], { cwd: tapWorkDir }); |
| 138 | + run(["git", "push", "origin", "main"], { cwd: tapWorkDir }); |
| 139 | + console.log("Pushed formula to tap."); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +console.log("Done!"); |
0 commit comments