"use strict";
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const PACKAGE = "testwall";
const VERSION = require("./package.json").version;
const REPO = "matthewdavidson/testwall";
const PLATFORMS = {
"darwin-arm64": { pkg: `@testwall/darwin-arm64`, file: `testwall-aarch64-apple-darwin` },
"darwin-x64": { pkg: `@testwall/darwin-x64`, file: `testwall-x86_64-apple-darwin` },
"linux-x64": { pkg: `@testwall/linux-x64`, file: `testwall-x86_64-unknown-linux-musl` },
"linux-arm64": { pkg: `@testwall/linux-arm64`, file: `testwall-aarch64-unknown-linux-musl` },
"win32-x64": { pkg: `@testwall/win32-x64`, file: `testwall-x86_64-pc-windows-msvc.exe` },
};
function getPlatformKey() {
return `${process.platform}-${process.arch}`;
}
function tryOptionalPackage(platformKey) {
const info = PLATFORMS[platformKey];
if (!info) return null;
try {
const binPath = require.resolve(`${info.pkg}/${PACKAGE}${process.platform === "win32" ? ".exe" : ""}`);
return binPath;
} catch {
return null;
}
}
function downloadBinary(platformKey) {
const info = PLATFORMS[platformKey];
if (!info) {
console.error(`testwall: unsupported platform ${platformKey}`);
process.exit(1);
}
const ext = process.platform === "win32" ? ".zip" : ".tar.gz";
const assetName = `${info.file}${ext}`;
const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${assetName}`;
const binDir = path.join(__dirname, "bin");
const binName = process.platform === "win32" ? `${PACKAGE}.exe` : PACKAGE;
const binPath = path.join(binDir, binName);
fs.mkdirSync(binDir, { recursive: true });
console.log(`testwall: downloading ${url}`);
try {
if (ext === ".tar.gz") {
execSync(`curl -fsSL "${url}" | tar xz -C "${binDir}" && chmod +x "${binPath}"`, {
stdio: "inherit",
});
} else {
const zipPath = path.join(binDir, "testwall.zip");
execSync(`curl -fsSL -o "${zipPath}" "${url}"`, { stdio: "inherit" });
execSync(`unzip -o "${zipPath}" -d "${binDir}"`, { stdio: "inherit" });
fs.unlinkSync(zipPath);
}
} catch (err) {
console.error(`testwall: failed to download binary from ${url}`);
console.error("You can install from source with: cargo install testwall");
process.exit(1);
}
}
function main() {
const platformKey = getPlatformKey();
const optionalBin = tryOptionalPackage(platformKey);
if (optionalBin) {
const binDir = path.join(__dirname, "bin");
const binName = process.platform === "win32" ? `${PACKAGE}.exe` : PACKAGE;
const binPath = path.join(binDir, binName);
fs.mkdirSync(binDir, { recursive: true });
fs.copyFileSync(optionalBin, binPath);
fs.chmodSync(binPath, 0o755);
return;
}
downloadBinary(platformKey);
}
main();