|
| 1 | +#!/usr/bin/env node |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { readdirSync, statSync } from "node:fs"; |
| 4 | +import { dirname, resolve } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), ".."); |
| 8 | +const args = process.argv.slice(2); |
| 9 | +const skipNpm = args.includes("--skip-npm"); |
| 10 | +const skipCache = args.includes("--skip-cache"); |
| 11 | + |
| 12 | +if (!skipCache) { |
| 13 | + run("node", ["scripts/codex-worktree-cache.mjs", "hydrate"]); |
| 14 | +} |
| 15 | + |
| 16 | +ensureNativeBuildDependencies(); |
| 17 | + |
| 18 | +if (!skipNpm) { |
| 19 | + ensureNodeModules(".", "root"); |
| 20 | + ensureNodeModules("client", "client"); |
| 21 | +} |
| 22 | + |
| 23 | +function ensureNativeBuildDependencies() { |
| 24 | + if (process.platform !== "darwin") { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (!commandSucceeds("pkg-config", ["--version"])) { |
| 29 | + installBrewPackage("pkgconf", "pkg-config"); |
| 30 | + } |
| 31 | + |
| 32 | + if (!commandSucceeds("pkg-config", ["--exists", "x264"])) { |
| 33 | + installBrewPackage("x264", "x264 pkg-config metadata"); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function installBrewPackage(formula, label) { |
| 38 | + if (truthy(process.env.SIMDECK_CODEX_SKIP_BREW)) { |
| 39 | + throw new Error( |
| 40 | + `Missing ${label}. Install it with \`brew install ${formula}\` or unset SIMDECK_CODEX_SKIP_BREW.`, |
| 41 | + ); |
| 42 | + } |
| 43 | + if (!commandSucceeds("brew", ["--version"])) { |
| 44 | + throw new Error(`Missing ${label}, and Homebrew is not available to install ${formula}.`); |
| 45 | + } |
| 46 | + run("brew", ["install", formula]); |
| 47 | +} |
| 48 | + |
| 49 | +function ensureNodeModules(prefix, label) { |
| 50 | + const modulesPath = |
| 51 | + prefix === "." ? resolve(ROOT, "node_modules") : resolve(ROOT, prefix, "node_modules"); |
| 52 | + if (existsAndHasContent(modulesPath)) { |
| 53 | + console.log(`[setup] skip ${label} npm install; node_modules already exists`); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const args = prefix === "." ? ["ci"] : ["ci", "--prefix", prefix]; |
| 58 | + run("npm", args); |
| 59 | +} |
| 60 | + |
| 61 | +function existsAndHasContent(path) { |
| 62 | + try { |
| 63 | + const stats = statSync(path); |
| 64 | + if (stats.isDirectory()) { |
| 65 | + return readdirSync(path).length > 0; |
| 66 | + } |
| 67 | + return stats.size > 0; |
| 68 | + } catch { |
| 69 | + return false; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function commandSucceeds(command, args) { |
| 74 | + const result = spawnSync(command, args, { |
| 75 | + cwd: ROOT, |
| 76 | + stdio: "ignore", |
| 77 | + env: process.env, |
| 78 | + }); |
| 79 | + return result.status === 0; |
| 80 | +} |
| 81 | + |
| 82 | +function run(command, args) { |
| 83 | + console.log(`\n$ ${[command, ...args].join(" ")}`); |
| 84 | + const result = spawnSync(command, args, { |
| 85 | + cwd: ROOT, |
| 86 | + stdio: "inherit", |
| 87 | + env: process.env, |
| 88 | + }); |
| 89 | + if (result.error) { |
| 90 | + throw result.error; |
| 91 | + } |
| 92 | + if (result.status !== 0) { |
| 93 | + process.exit(result.status ?? 1); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function truthy(value) { |
| 98 | + return value === "1" || value === "true" || value === "yes"; |
| 99 | +} |
0 commit comments