g1t/scripts/build-rust-worker.mjs
| 1 | #!/usr/bin/env node |
| 2 | // Builds the Rust Worker in the current folder: the build command in each |
| 3 | // Rust unit's wrangler.jsonc, so `wrangler deploy`, `wrangler dev` and |
| 4 | // scripts/deploy.mjs all build the same way. |
| 5 | // |
| 6 | // worker-build is installed only when it is missing or another version: |
| 7 | // `cargo install` on every build cost a crates.io index check each time, |
| 8 | // and a full compile on a fresh machine. Every Rust Worker shares the |
| 9 | // workspace's Cargo target directory, so builds running side by side take |
| 10 | // turns on Cargo's lock while their wasm-bindgen and wasm-opt steps |
| 11 | // overlap. How hard wasm-opt works is each crate's |
| 12 | // [package.metadata.wasm-pack.profile.release] (docs/DEPLOYING.md). |
| 13 | // |
| 14 | // node ../../scripts/build-rust-worker.mjs [worker-build args] |
| 15 | // node scripts/build-rust-worker.mjs --ensure # install only |
| 16 | |
| 17 | import { spawnSync } from "node:child_process"; |
| 18 | |
| 19 | export const WORKER_BUILD_VERSION = "0.8.7"; |
| 20 | |
| 21 | const run = (command, args, options = {}) => |
| 22 | spawnSync(command, args, { stdio: "inherit", shell: process.platform === "win32", ...options }); |
| 23 | |
| 24 | /** The installed worker-build's version, or null. */ |
| 25 | export function installedVersion() { |
| 26 | const found = spawnSync("worker-build", ["--version"], { encoding: "utf8", shell: process.platform === "win32" }); |
| 27 | return found.status === 0 ? found.stdout.trim() : null; |
| 28 | } |
| 29 | |
| 30 | /** Installs the pinned worker-build unless it is already there. */ |
| 31 | export function ensureWorkerBuild() { |
| 32 | const version = installedVersion(); |
| 33 | if (version === WORKER_BUILD_VERSION) return; |
| 34 | console.error(`worker-build ${version ?? "is missing"}; installing ${WORKER_BUILD_VERSION}`); |
| 35 | const installed = run("cargo", ["install", "-q", "--locked", `worker-build@${WORKER_BUILD_VERSION}`, "--force"]); |
| 36 | if (installed.status !== 0) process.exit(installed.status ?? 1); |
| 37 | } |
| 38 | |
| 39 | // Run, not imported (scripts/deploy.mjs imports ensureWorkerBuild). |
| 40 | if (process.argv[1]?.replaceAll("\\", "/").endsWith("scripts/build-rust-worker.mjs")) { |
| 41 | const args = process.argv.slice(2); |
| 42 | ensureWorkerBuild(); |
| 43 | if (!args.includes("--ensure")) { |
| 44 | const built = run("worker-build", ["--release", ...args]); |
| 45 | process.exit(built.status ?? 1); |
| 46 | } |
| 47 | } |