@ 261027.7
current
const WASM_BUILDER_CONTAINER =
	"ghcr.io/nodejs/wasm-builder@sha256:542fdbef9fa6eb003cc1eb89a9f3e6b967359471fdb29db4910c54474e70911d"; // v0.0.10

const { execSync, execFileSync } = require("node:child_process");
const { resolve } = require("node:path");
const os = require("node:os");
const fs = require("node:fs");
const path = require("node:path");

const ROOT = resolve(__dirname, "../");

// Either empty, "--dry-run", or "--in-container".
const mode = process.argv[2];

let platform = process.env.WASM_PLATFORM;
if (!platform && mode !== "--in-container") {
	platform = execFileSync("docker", [
		"info",
		"-f",
		"{{.OSType}}/{{.Architecture}}",
	])
		.toString()
		.trim();
}

if (mode !== "--in-container") {
	// we must map in a home directory since we might not run as a user that
	// is defined in the container. Create it here.
	const workdir = fs.mkdtempSync(path.join(os.tmpdir(), "amaro-build"));

	// build and execute the docker command to run the build
	const args = [];
	args.push("run");
	args.push("--rm");
	args.push("--platform");
	args.push(`${platform.toString().trim()}`);
	if (process.platform === "linux") {
		args.push("--user");
		args.push(`${process.getuid()}:${process.getegid()}`);
	}
	args.push("--mount");
	args.push(`type=bind,source=${ROOT}/crates,target=/home/node/build/crates`);
	if (mode !== "--dry-run") {
		fs.mkdirSync(path.join(ROOT, "lib"), { recursive: true });
		args.push("--mount");
		args.push(`type=bind,source=${ROOT}/lib,target=/home/node/build/lib`);
	} else {
		// In dry-run mode, generate the products in the temporary workdir.
		args.push("--mount");
		args.push(`type=bind,source=${workdir},target=/home/node/build/lib`);
	}
	args.push("--mount");
	args.push(`type=bind,source=${ROOT}/tools,target=/home/node/build/tools`);
	args.push("--mount");
	args.push(`type=bind,source=${workdir},target=/home/node/home`);
	args.push("-t");
	args.push(`${WASM_BUILDER_CONTAINER}`);
	args.push("node");
	args.push("./tools/build-wasm.js");
	args.push("--in-container");
	console.log(`> docker ${args.join(" ")}\n\n`);
	execFileSync("docker", args, { stdio: "inherit" });

	// clean up the temporary working directory
	fs.rmSync(workdir, { recursive: true });
	process.exit(0);
}

// wasm-bindgen-cli must match the wasm-bindgen crate in Cargo.lock exactly.
const wasmBindgenVersion = fs
	.readFileSync(resolve(ROOT, "crates/amaro/Cargo.lock"), "utf8")
	.match(/name = "wasm-bindgen"\nversion = "([^"]+)"/)?.[1];
if (!wasmBindgenVersion) {
	throw new Error("Could not find wasm-bindgen in crates/amaro/Cargo.lock");
}

execSync(
	`cp -r /home/node/.rustup /home/node/home/.rustup && \
     export HOME=/home/node/home && \
     export PATH=/home/node/home/.cargo/bin:$PATH && \
     rustc --version && \
     cd crates/amaro && \
     cargo install --locked wasm-pack wasm-bindgen-cli@${wasmBindgenVersion} && \
     wasm-pack build --out-name wasm --release --target nodejs -- --locked --config ../../tools/config.toml && \
     node ../../tools/patch-wasm.mjs pkg && \
     cp -r pkg/* ../../lib`,
	{
		stdio: "inherit",
	},
);