Refactor zstd decompression handling in expand-windows-installer.mjs
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit

- Updated the findZstdDecompressCommand function to improve compatibility with Windows by adding shell options for command execution.
- Simplified candidate path resolution for zstd executables by utilizing a dedicated electrobun directory, enhancing clarity and maintainability.
- Improved error handling during decompression and extraction processes to ensure better reliability on Windows platforms.
This commit is contained in:
Tom Butcher 2026-08-01 22:13:10 +01:00
parent f6bd45c214
commit 4307d74e92

View File

@ -10,38 +10,45 @@ import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const electrobunDir = path.join(rootDir, "node_modules/electrobun");
function findZstdDecompressCommand() { function findZstdDecompressCommand() {
const systemZstd = spawnSync("zstd", ["--version"], { encoding: "utf8" }); const spawnOpts = process.platform === "win32" ? { shell: true } : {};
const systemZstd = spawnSync("zstd", ["--version"], {
encoding: "utf8",
...spawnOpts,
});
if (systemZstd.status === 0) { if (systemZstd.status === 0) {
return { command: "zstd", args: (inputPath, outputPath) => [ return {
command: "zstd",
args: (inputPath, outputPath) => [
"-d", "-d",
"-f", "-f",
"-o", "-o",
outputPath, outputPath,
inputPath, inputPath,
] }; ],
};
} }
const hostArch = process.arch === "arm64" ? "arm64" : "x64"; const hostArch = process.arch === "arm64" ? "arm64" : "x64";
const candidates = [ const candidates = [
path.join( path.join(electrobunDir, `dist-win-${hostArch}`, "zig-zstd.exe"),
rootDir, path.join(electrobunDir, "dist-win-x64", "zig-zstd.exe"),
"node_modules/electrobun/dist-win-x64/zig-zstd", path.join(electrobunDir, `dist-macos-${hostArch}`, "zig-zstd"),
hostArch, path.join(electrobunDir, "dist-macos-x64", "zig-zstd"),
"zig-zstd.exe", path.join(electrobunDir, "dist-win-x64", "zig-zstd", "x64", "zig-zstd.exe"),
), path.join(electrobunDir, "dist-win-x64", "zig-zstd", "arm64", "zig-zstd.exe"),
path.join( path.join(electrobunDir, "vendors", "zig-zstd", "x64", "zig-zstd.exe"),
rootDir, path.join(electrobunDir, "vendors", "zig-zstd", "arm64", "zig-zstd.exe"),
"node_modules/electrobun/dist-win-x64/zig-zstd",
"x64",
"zig-zstd.exe",
),
path.join(rootDir, "node_modules/electrobun/dist-win-x64", "zig-zstd"),
]; ];
for (const candidate of candidates) { for (const candidate of candidates) {
if (existsSync(candidate)) { if (!existsSync(candidate)) {
continue;
}
return { return {
command: candidate, command: candidate,
args: (inputPath, outputPath) => [ args: (inputPath, outputPath) => [
@ -54,7 +61,6 @@ function findZstdDecompressCommand() {
], ],
}; };
} }
}
return null; return null;
} }
@ -69,6 +75,7 @@ function decompressTarZst(inputPath, outputPath) {
const result = spawnSync(zstd.command, zstd.args(inputPath, outputPath), { const result = spawnSync(zstd.command, zstd.args(inputPath, outputPath), {
stdio: "inherit", stdio: "inherit",
shell: process.platform === "win32",
}); });
if (result.status !== 0) { if (result.status !== 0) {
@ -94,6 +101,7 @@ export function expandWindowsAppFromArchive(setupArchivePath, parentDir) {
const extractTar = spawnSync("tar", ["-xf", tarPath, "-C", workDir], { const extractTar = spawnSync("tar", ["-xf", tarPath, "-C", workDir], {
stdio: "inherit", stdio: "inherit",
shell: process.platform === "win32",
}); });
if (extractTar.status !== 0) { if (extractTar.status !== 0) {