#!/usr/bin/env bun import * as path from "node:path"; import { signMacOSBinary } from "../../../scripts/macos-code-signing"; import { buildDevCompileArgs } from "./compile-args"; const packageDir = path.join(import.meta.dir, ".."); const outputPath = path.join(packageDir, "dist", "gjc"); const nativeDir = path.join(packageDir, "..", "natives", "native"); function shouldAdhocSignDarwinBinary(): boolean { return process.platform === "darwin"; } async function runCommand(command: string[], env: NodeJS.ProcessEnv = Bun.env): Promise { const proc = Bun.spawn(command, { cwd: packageDir, env, stdout: "inherit", stderr: "inherit", }); const exitCode = await proc.exited; if (exitCode !== 0) { throw new Error(`Command failed with exit code ${exitCode}: ${command.join(" ")}`); } } async function stageWorkspaceNativeAddons(): Promise { await Array.fromAsync(new Bun.Glob("pi_natives.*.node").scan({ cwd: nativeDir }), async filename => { await Bun.write(path.join(packageDir, "dist", filename), Bun.file(path.join(nativeDir, filename))); }); } async function main(): Promise { await runCommand(["bun", "--cwd=../stats", "scripts/generate-client-bundle.ts", "--generate"]); try { await runCommand(["bun", "--cwd=../natives", "run", "embed:native"]); try { const buildEnv = shouldAdhocSignDarwinBinary() ? { ...Bun.env, BUN_NO_CODESIGN_MACHO_BINARY: "1" } : Bun.env; await runCommand(buildDevCompileArgs(), buildEnv); await stageWorkspaceNativeAddons(); // Bun 1.3.12 emits a truncated Mach-O signature on darwin builds. if (shouldAdhocSignDarwinBinary()) { await signMacOSBinary(outputPath, runCommand); } } finally { await runCommand(["bun", "--cwd=../natives", "run", "embed:native", "--reset"]); } } finally { await runCommand(["bun", "--cwd=../stats", "scripts/generate-client-bundle.ts", "--reset"]); } } await main();