|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +/** |
| 4 | + * Pre-release sanity check — run BEFORE tagging a release. |
| 5 | + * |
| 6 | + * Verifies: |
| 7 | + * 1. All required external NAPI modules are in package.json dependencies |
| 8 | + * 2. The publish script will include them in the wrapper package |
| 9 | + * 3. A local build produces a binary that actually starts |
| 10 | + * |
| 11 | + * Usage: bun run packages/opencode/script/pre-release-check.ts |
| 12 | + */ |
| 13 | + |
| 14 | +import fs from "fs" |
| 15 | +import path from "path" |
| 16 | +import { spawnSync } from "child_process" |
| 17 | +import { fileURLToPath } from "url" |
| 18 | + |
| 19 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 20 | +const pkgDir = path.resolve(__dirname, "..") |
| 21 | +const repoRoot = path.resolve(pkgDir, "../..") |
| 22 | + |
| 23 | +const pkg = JSON.parse(fs.readFileSync(path.join(pkgDir, "package.json"), "utf-8")) |
| 24 | + |
| 25 | +let failures = 0 |
| 26 | + |
| 27 | +function pass(msg: string) { |
| 28 | + console.log(` ✓ ${msg}`) |
| 29 | +} |
| 30 | + |
| 31 | +function fail(msg: string) { |
| 32 | + console.error(` ✗ ${msg}`) |
| 33 | + failures++ |
| 34 | +} |
| 35 | + |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// Check 1: Required externals are in package.json dependencies |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | +console.log("\n[1/4] Checking required externals in package.json...") |
| 40 | + |
| 41 | +const requiredExternals = ["@altimateai/altimate-core"] |
| 42 | + |
| 43 | +for (const ext of requiredExternals) { |
| 44 | + if (pkg.dependencies?.[ext]) { |
| 45 | + pass(`${ext} is in dependencies (${pkg.dependencies[ext]})`) |
| 46 | + } else { |
| 47 | + fail(`${ext} is NOT in dependencies — binary will crash at runtime`) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// --------------------------------------------------------------------------- |
| 52 | +// Check 2: Required externals are resolvable in node_modules |
| 53 | +// --------------------------------------------------------------------------- |
| 54 | +console.log("\n[2/4] Checking required externals are installed...") |
| 55 | + |
| 56 | +for (const ext of requiredExternals) { |
| 57 | + try { |
| 58 | + require.resolve(ext) |
| 59 | + pass(`${ext} resolves from node_modules`) |
| 60 | + } catch { |
| 61 | + fail(`${ext} is NOT installed — run \`bun install\``) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// Check 3: Build and smoke-test the binary |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +console.log("\n[3/4] Building local binary...") |
| 69 | + |
| 70 | +const buildResult = spawnSync("bun", ["run", "build:local"], { |
| 71 | + cwd: pkgDir, |
| 72 | + encoding: "utf-8", |
| 73 | + timeout: 120_000, |
| 74 | + env: { |
| 75 | + ...process.env, |
| 76 | + MODELS_DEV_API_JSON: path.join(pkgDir, "test/tool/fixtures/models-api.json"), |
| 77 | + }, |
| 78 | +}) |
| 79 | + |
| 80 | +if (buildResult.status !== 0) { |
| 81 | + fail(`Build failed:\n${buildResult.stderr}`) |
| 82 | +} else { |
| 83 | + pass("Local build succeeded") |
| 84 | + |
| 85 | + // Find the binary — walk recursively for scoped packages (@altimateai/...) |
| 86 | + const distDir = path.join(pkgDir, "dist") |
| 87 | + let binaryPath: string | undefined |
| 88 | + const binaryNames = process.platform === "win32" ? ["altimate.exe", "altimate"] : ["altimate"] |
| 89 | + function searchDist(dir: string): string | undefined { |
| 90 | + if (!fs.existsSync(dir)) return undefined |
| 91 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 92 | + if (!entry.isDirectory()) continue |
| 93 | + const sub = path.join(dir, entry.name) |
| 94 | + for (const name of binaryNames) { |
| 95 | + const candidate = path.join(sub, "bin", name) |
| 96 | + if (fs.existsSync(candidate)) return candidate |
| 97 | + } |
| 98 | + const nested = searchDist(sub) |
| 99 | + if (nested) return nested |
| 100 | + } |
| 101 | + return undefined |
| 102 | + } |
| 103 | + binaryPath = searchDist(distDir) |
| 104 | + |
| 105 | + if (!binaryPath) { |
| 106 | + fail("No binary found in dist/ after build") |
| 107 | + } else { |
| 108 | + console.log("\n[4/4] Smoke-testing compiled binary...") |
| 109 | + |
| 110 | + // Resolve NODE_PATH like the bin wrapper does — start from pkgDir |
| 111 | + // to include workspace-level node_modules where NAPI modules live |
| 112 | + const nodePaths: string[] = [] |
| 113 | + let current = pkgDir |
| 114 | + for (;;) { |
| 115 | + const nm = path.join(current, "node_modules") |
| 116 | + if (fs.existsSync(nm)) nodePaths.push(nm) |
| 117 | + const parent = path.dirname(current) |
| 118 | + if (parent === current) break |
| 119 | + current = parent |
| 120 | + } |
| 121 | + |
| 122 | + const smokeResult = spawnSync(binaryPath, ["--version"], { |
| 123 | + encoding: "utf-8", |
| 124 | + timeout: 15_000, |
| 125 | + env: { |
| 126 | + ...process.env, |
| 127 | + NODE_PATH: nodePaths.join(path.delimiter), |
| 128 | + OPENCODE_DISABLE_TELEMETRY: "1", |
| 129 | + }, |
| 130 | + }) |
| 131 | + |
| 132 | + if (smokeResult.status === 0) { |
| 133 | + const version = (smokeResult.stdout ?? "").trim() |
| 134 | + pass(`Binary starts successfully (${version})`) |
| 135 | + } else { |
| 136 | + const output = (smokeResult.stdout ?? "") + (smokeResult.stderr ?? "") |
| 137 | + fail(`Binary crashed on startup:\n${output}`) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | +// Summary |
| 144 | +// --------------------------------------------------------------------------- |
| 145 | +console.log("") |
| 146 | +if (failures > 0) { |
| 147 | + console.error(`FAILED: ${failures} check(s) failed. Do NOT tag a release.`) |
| 148 | + process.exit(1) |
| 149 | +} else { |
| 150 | + console.log("ALL CHECKS PASSED. Safe to tag a release.") |
| 151 | +} |
0 commit comments