|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Build script for BrowserOS server binaries |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * bun scripts/build_server.ts --mode=prod [--target=darwin-arm64] |
| 7 | + * bun scripts/build_server.ts --mode=dev [--target=all] |
| 8 | + * |
| 9 | + * Modes: |
| 10 | + * prod - Clean environment build using only .env.prod |
| 11 | + * dev - Normal build using shell environment + .env.dev |
| 12 | + * |
| 13 | + * Targets: |
| 14 | + * linux-x64, linux-arm64, windows-x64, darwin-arm64, darwin-x64, all |
| 15 | + */ |
| 16 | + |
| 17 | +import { spawn } from "child_process"; |
| 18 | +import { readFileSync, mkdirSync } from "fs"; |
| 19 | +import { resolve, join } from "path"; |
| 20 | +import { parse } from "dotenv"; |
| 21 | + |
| 22 | +interface BuildTarget { |
| 23 | + name: string; |
| 24 | + bunTarget: string; |
| 25 | + outfile: string; |
| 26 | +} |
| 27 | + |
| 28 | +const TARGETS: Record<string, BuildTarget> = { |
| 29 | + "linux-x64": { |
| 30 | + name: "Linux x64", |
| 31 | + bunTarget: "bun-linux-x64-modern", |
| 32 | + outfile: "dist/server/browseros-server-linux-x64", |
| 33 | + }, |
| 34 | + "linux-arm64": { |
| 35 | + name: "Linux ARM64", |
| 36 | + bunTarget: "bun-linux-arm64", |
| 37 | + outfile: "dist/server/browseros-server-linux-arm64", |
| 38 | + }, |
| 39 | + "windows-x64": { |
| 40 | + name: "Windows x64", |
| 41 | + bunTarget: "bun-windows-x64-modern", |
| 42 | + outfile: "dist/server/browseros-server-windows-x64.exe", |
| 43 | + }, |
| 44 | + "darwin-arm64": { |
| 45 | + name: "macOS ARM64", |
| 46 | + bunTarget: "bun-darwin-arm64", |
| 47 | + outfile: "dist/server/browseros-server-darwin-arm64", |
| 48 | + }, |
| 49 | + "darwin-x64": { |
| 50 | + name: "macOS x64", |
| 51 | + bunTarget: "bun-darwin-x64", |
| 52 | + outfile: "dist/server/browseros-server-darwin-x64", |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +const MINIMAL_SYSTEM_VARS = ["PATH"]; |
| 57 | + |
| 58 | +function parseArgs(): { mode: "prod" | "dev"; targets: string[] } { |
| 59 | + const args = process.argv.slice(2); |
| 60 | + let mode: "prod" | "dev" = "prod"; |
| 61 | + let targetArg = "all"; |
| 62 | + |
| 63 | + for (const arg of args) { |
| 64 | + if (arg.startsWith("--mode=")) { |
| 65 | + const modeValue = arg.split("=")[1]; |
| 66 | + if (modeValue !== "prod" && modeValue !== "dev") { |
| 67 | + console.error(`Invalid mode: ${modeValue}. Must be 'prod' or 'dev'`); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + mode = modeValue; |
| 71 | + } else if (arg.startsWith("--target=")) { |
| 72 | + targetArg = arg.split("=")[1]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const targets = |
| 77 | + targetArg === "all" |
| 78 | + ? Object.keys(TARGETS) |
| 79 | + : targetArg.split(",").map((t) => t.trim()); |
| 80 | + |
| 81 | + for (const target of targets) { |
| 82 | + if (!TARGETS[target]) { |
| 83 | + console.error(`Invalid target: ${target}`); |
| 84 | + console.error(`Available targets: ${Object.keys(TARGETS).join(", ")}, all`); |
| 85 | + process.exit(1); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return { mode, targets }; |
| 90 | +} |
| 91 | + |
| 92 | +function loadEnvFile(path: string): Record<string, string> { |
| 93 | + try { |
| 94 | + const content = readFileSync(path, "utf-8"); |
| 95 | + const parsed = parse(content); |
| 96 | + return parsed; |
| 97 | + } catch (error) { |
| 98 | + console.error(`Failed to load ${path}:`, error); |
| 99 | + process.exit(1); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function createCleanEnv(envVars: Record<string, string>): Record<string, string> { |
| 104 | + const cleanEnv: Record<string, string> = {}; |
| 105 | + |
| 106 | + for (const varName of MINIMAL_SYSTEM_VARS) { |
| 107 | + const value = process.env[varName]; |
| 108 | + if (value) { |
| 109 | + cleanEnv[varName] = value; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + Object.assign(cleanEnv, envVars); |
| 114 | + |
| 115 | + return cleanEnv; |
| 116 | +} |
| 117 | + |
| 118 | +function runCommand( |
| 119 | + command: string, |
| 120 | + args: string[], |
| 121 | + env: NodeJS.ProcessEnv |
| 122 | +): Promise<void> { |
| 123 | + return new Promise((resolve, reject) => { |
| 124 | + const child = spawn(command, args, { |
| 125 | + env, |
| 126 | + stdio: "inherit", |
| 127 | + }); |
| 128 | + |
| 129 | + child.on("close", (code) => { |
| 130 | + if (code === 0) { |
| 131 | + resolve(); |
| 132 | + } else { |
| 133 | + reject(new Error(`Command exited with code ${code}`)); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + child.on("error", (error) => { |
| 138 | + reject(error); |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | +async function buildTarget( |
| 144 | + target: BuildTarget, |
| 145 | + mode: "prod" | "dev", |
| 146 | + envVars: Record<string, string> |
| 147 | +): Promise<void> { |
| 148 | + console.log(`\n📦 Building ${target.name}...`); |
| 149 | + |
| 150 | + const args = [ |
| 151 | + "build", |
| 152 | + "--compile", |
| 153 | + "packages/server/src/index.ts", |
| 154 | + "--outfile", |
| 155 | + target.outfile, |
| 156 | + "--minify", |
| 157 | + "--sourcemap", |
| 158 | + `--target=${target.bunTarget}`, |
| 159 | + "--env", |
| 160 | + "inline", |
| 161 | + ]; |
| 162 | + |
| 163 | + const buildEnv = mode === "prod" ? createCleanEnv(envVars) : { ...process.env, ...envVars }; |
| 164 | + |
| 165 | + try { |
| 166 | + await runCommand("bun", args, buildEnv); |
| 167 | + console.log(`✅ ${target.name} built successfully`); |
| 168 | + |
| 169 | + if (target.outfile.endsWith(".exe")) { |
| 170 | + console.log(`🔧 Patching Windows executable...`); |
| 171 | + await runCommand("bun", ["scripts/patch-windows-exe.ts", target.outfile], process.env); |
| 172 | + } |
| 173 | + } catch (error) { |
| 174 | + console.error(`❌ Failed to build ${target.name}:`, error); |
| 175 | + throw error; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +async function main() { |
| 180 | + const { mode, targets } = parseArgs(); |
| 181 | + const rootDir = resolve(import.meta.dir, ".."); |
| 182 | + process.chdir(rootDir); |
| 183 | + |
| 184 | + console.log(`🚀 Building BrowserOS server binaries`); |
| 185 | + console.log(` Mode: ${mode}`); |
| 186 | + console.log(` Targets: ${targets.join(", ")}`); |
| 187 | + |
| 188 | + const envFile = mode === "prod" ? ".env.prod" : ".env.dev"; |
| 189 | + const envPath = join(rootDir, envFile); |
| 190 | + |
| 191 | + console.log(`\n📄 Loading environment from ${envFile}...`); |
| 192 | + const envVars = loadEnvFile(envPath); |
| 193 | + console.log(` Loaded ${Object.keys(envVars).length} variables`); |
| 194 | + |
| 195 | + if (mode === "prod") { |
| 196 | + console.log(`\n🔒 Production mode: Using CLEAN environment (only ${envFile} + minimal system vars)`); |
| 197 | + console.log(` System vars: ${MINIMAL_SYSTEM_VARS.join(", ")}`); |
| 198 | + } else { |
| 199 | + console.log(`\n🔓 Development mode: Using shell environment + ${envFile}`); |
| 200 | + } |
| 201 | + |
| 202 | + mkdirSync("dist/server", { recursive: true }); |
| 203 | + |
| 204 | + for (const targetKey of targets) { |
| 205 | + const target = TARGETS[targetKey]; |
| 206 | + await buildTarget(target, mode, envVars); |
| 207 | + } |
| 208 | + |
| 209 | + console.log(`\n✨ All builds completed successfully!`); |
| 210 | + console.log(`\n📦 Output files:`); |
| 211 | + for (const targetKey of targets) { |
| 212 | + console.log(` ${TARGETS[targetKey].outfile}`); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +main().catch((error) => { |
| 217 | + console.error("\n💥 Build failed:", error); |
| 218 | + process.exit(1); |
| 219 | +}); |
0 commit comments