Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion apps/server/script/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawnSync } from "node:child_process";
import { mkdirSync } from "node:fs";
import { mkdirSync, readFileSync } from "node:fs";
import { join, resolve } from "node:path";

const bunRuntime = (globalThis as typeof globalThis & {
Expand Down Expand Up @@ -88,6 +88,43 @@ function outputName(filename: string, target?: string) {
return `${filename}${suffix}${ext}`;
}

function targetMatchesHost(target?: string): boolean {
if (!target) return true;
const isWindowsTarget = target.includes("windows");
const isLinuxTarget = target.includes("linux");
const isDarwinTarget = target.includes("darwin");
if (process.platform === "win32") return isWindowsTarget;
if (process.platform === "linux") return isLinuxTarget;
if (process.platform === "darwin") return isDarwinTarget;
return false;
}

function readPackageVersion(): string | null {
try {
const pkg = JSON.parse(readFileSync(resolve("package.json"), "utf8")) as { version?: unknown };
return typeof pkg.version === "string" ? pkg.version.trim() : null;
} catch {
return null;
}
}

function verifyBuiltBinary(outfile: string, expectedVersion: string | null) {
if (!expectedVersion) return;
const result = spawnSync(outfile, ["--version"], { encoding: "utf8" });
const stdout = (result.stdout ?? "").trim();
if (result.status === 0 && stdout === expectedVersion) return;
console.error(
[
`openwork-server build verification failed for ${outfile}.`,
` expected --version: ${expectedVersion}`,
` actual exit status: ${result.status ?? "unknown"}`,
` actual stdout: ${stdout || "(empty)"}`,
" the binary may be a bare Bun runtime instead of a compiled openwork-server.",
].join("\n"),
);
process.exit(1);
}

async function buildOnce(entrypoint: string, outdir: string, filename: string, target?: string) {
mkdirSync(outdir, { recursive: true });
const outfile = join(outdir, outputName(filename, target));
Expand All @@ -101,6 +138,10 @@ async function buildOnce(entrypoint: string, outdir: string, filename: string, t
if (result.status !== 0) {
process.exit(result.status ?? 1);
}

if (targetMatchesHost(target)) {
verifyBuiltBinary(outfile, readPackageVersion());
}
}

const options = readArgs(bun.argv.slice(2));
Expand Down
Loading