farmcontrol-server/scripts/expand-windows-installer.mjs
Tom Butcher 72cea3a740
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit
Refactor Windows installer scripts and enhance build process
- Updated farmcontrol-server.nsi to simplify setup file handling by defining APP_SOURCE_DIR and removing unnecessary variables.
- Modified installer.nsh to reference the correct launcher executable path.
- Refactored build-windows-nsis.ps1 to accept APP_DIR instead of individual setup file parameters, streamlining the build process.
- Introduced expand-windows-installer.mjs for handling decompression and extraction of Windows app archives, improving artifact management.
- Enhanced finalize-desktop-artifacts.mjs to integrate the new expansion process and ensure proper cleanup of temporary files.
2026-08-01 22:10:41 +01:00

124 lines
3.2 KiB
JavaScript

import {
existsSync,
mkdirSync,
readdirSync,
rmSync,
statSync,
} from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
function findZstdDecompressCommand() {
const systemZstd = spawnSync("zstd", ["--version"], { encoding: "utf8" });
if (systemZstd.status === 0) {
return { command: "zstd", args: (inputPath, outputPath) => [
"-d",
"-f",
"-o",
outputPath,
inputPath,
] };
}
const hostArch = process.arch === "arm64" ? "arm64" : "x64";
const candidates = [
path.join(
rootDir,
"node_modules/electrobun/dist-win-x64/zig-zstd",
hostArch,
"zig-zstd.exe",
),
path.join(
rootDir,
"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) {
if (existsSync(candidate)) {
return {
command: candidate,
args: (inputPath, outputPath) => [
"decompress",
"-i",
inputPath,
"-o",
outputPath,
"--no-timing",
],
};
}
}
return null;
}
function decompressTarZst(inputPath, outputPath) {
const zstd = findZstdDecompressCommand();
if (!zstd) {
throw new Error(
"expand-windows-installer: zstd not found (install zstd or use electrobun dist binaries)",
);
}
const result = spawnSync(zstd.command, zstd.args(inputPath, outputPath), {
stdio: "inherit",
});
if (result.status !== 0) {
throw new Error(
`expand-windows-installer: decompression failed (exit ${result.status ?? 1})`,
);
}
}
export function expandWindowsAppFromArchive(setupArchivePath, parentDir) {
const resolvedArchive = path.resolve(setupArchivePath);
if (!existsSync(resolvedArchive)) {
throw new Error(`expand-windows-installer: archive not found: ${resolvedArchive}`);
}
const workDir = path.join(path.resolve(parentDir), ".expanded-app");
const tarPath = path.join(workDir, "app.tar");
rmSync(workDir, { recursive: true, force: true });
mkdirSync(workDir, { recursive: true });
decompressTarZst(resolvedArchive, tarPath);
const extractTar = spawnSync("tar", ["-xf", tarPath, "-C", workDir], {
stdio: "inherit",
});
if (extractTar.status !== 0) {
rmSync(workDir, { recursive: true, force: true });
throw new Error(
`expand-windows-installer: tar extraction failed (exit ${extractTar.status ?? 1})`,
);
}
const appDir = readdirSync(workDir)
.filter((entry) => entry !== "app.tar" && entry !== "__MACOSX")
.map((entry) => path.join(workDir, entry))
.find((entryPath) => statSync(entryPath).isDirectory());
if (!appDir) {
rmSync(workDir, { recursive: true, force: true });
throw new Error("expand-windows-installer: app folder not found in archive");
}
console.log(`expand-windows-installer: expanded ${appDir}`);
return appDir;
}
export function cleanExpandedWindowsApp(parentDir) {
const workDir = path.join(path.resolve(parentDir), ".expanded-app");
rmSync(workDir, { recursive: true, force: true });
}