Refactor zstd decompression in expand-macos-bundle script
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit

- Replaced the findZstd function with a new decompressTarZst function to streamline the zstd decompression process.
- Improved error handling by logging an error message and exiting the process if zstd is not found.
- Updated the script to directly call the new decompression function, enhancing clarity and maintainability.
This commit is contained in:
Tom Butcher 2026-08-01 21:39:47 +01:00
parent cda0286db7
commit 3107a517c3

View File

@ -46,25 +46,32 @@ if (!existsSync(tarZstPath)) {
process.exit(0); process.exit(0);
} }
function findZstd() { function decompressTarZst(inputPath, outputPath) {
const fromPath = spawnSync("zstd", ["--version"], { encoding: "utf8" }); const systemZstd = spawnSync("zstd", ["--version"], { encoding: "utf8" });
if (fromPath.status === 0) { if (systemZstd.status === 0) {
return "zstd"; return spawnSync("zstd", ["-d", "-f", "-o", outputPath, inputPath], {
stdio: "inherit",
});
} }
const hostArch = process.arch === "arm64" ? "arm64" : "x64"; const hostArch = process.arch === "arm64" ? "arm64" : "x64";
const electrobunZstd = path.join( const zigZstd = path.join(
rootDir, rootDir,
"node_modules/electrobun/dist-macos-" + hostArch, "node_modules/electrobun/dist-macos-" + hostArch,
"zig-zstd", "zig-zstd",
); );
if (existsSync(electrobunZstd)) { if (existsSync(zigZstd)) {
return electrobunZstd; return spawnSync(
zigZstd,
["decompress", "-i", inputPath, "-o", outputPath, "--no-timing"],
{ stdio: "inherit" },
);
} }
throw new Error( console.error(
"expand-macos-bundle: zstd not found (install zstd or use electrobun dist binaries)", "expand-macos-bundle: zstd not found (install zstd or use electrobun dist binaries)",
); );
process.exit(1);
} }
const workDir = path.join(tmpdir(), `farmcontrol-server-expand-${hash}`); const workDir = path.join(tmpdir(), `farmcontrol-server-expand-${hash}`);
@ -72,10 +79,7 @@ const tarPath = path.join(workDir, `${hash}.tar`);
rmSync(workDir, { recursive: true, force: true }); rmSync(workDir, { recursive: true, force: true });
mkdirSync(workDir, { recursive: true }); mkdirSync(workDir, { recursive: true });
const zstd = findZstd(); const decompress = decompressTarZst(tarZstPath, tarPath);
const decompress = spawnSync(zstd, ["-d", "-f", "-o", tarPath, tarZstPath], {
stdio: "inherit",
});
if (decompress.status !== 0) { if (decompress.status !== 0) {
console.error("expand-macos-bundle: zstd decompression failed"); console.error("expand-macos-bundle: zstd decompression failed");