Add TarVersion.name field

This refactoring commit records the name of the tar program in the new
TarVersion.name field and makes extractTarZst() use the new field
instead of the hardcoded name "tar". Code behavior remains unchanged
because currently TarVersion.name is always "tar".

This is the first step toward supporting a tar program under a different
executable name.
This commit is contained in:
Chuan-kai Lin 2025-02-14 12:08:07 -08:00
parent 1c15a48f3f
commit c4a8587f45

View file

@ -15,6 +15,7 @@ const MIN_REQUIRED_BSD_TAR_VERSION = "3.4.3";
const MIN_REQUIRED_GNU_TAR_VERSION = "1.31";
export type TarVersion = {
name: string;
type: "gnu" | "bsd";
version: string;
};
@ -39,14 +40,14 @@ async function getTarVersion(): Promise<TarVersion> {
throw new Error("Failed to parse output of tar --version.");
}
return { type: "gnu", version: match[1] };
return { name: "tar", type: "gnu", version: match[1] };
} else if (stdout.includes("bsdtar")) {
const match = stdout.match(/bsdtar ([0-9.]+)/);
if (!match || !match[1]) {
throw new Error("Failed to parse output of tar --version.");
}
return { type: "bsd", version: match[1] };
return { name: "tar", type: "bsd", version: match[1] };
} else {
throw new Error("Unknown tar version");
}
@ -162,10 +163,10 @@ export async function extractTarZst(
args.push("-f", tar instanceof stream.Readable ? "-" : tar, "-C", dest);
process.stdout.write(`[command]tar ${args.join(" ")}\n`);
process.stdout.write(`[command]${tarVersion.name} ${args.join(" ")}\n`);
await new Promise<void>((resolve, reject) => {
const tarProcess = spawn("tar", args, { stdio: "pipe" });
const tarProcess = spawn(tarVersion.name, args, { stdio: "pipe" });
let stdout = "";
tarProcess.stdout?.on("data", (data: Buffer) => {
@ -196,7 +197,7 @@ export async function extractTarZst(
if (code !== 0) {
reject(
new CommandInvocationError(
"tar",
tarVersion.name,
args,
code ?? undefined,
stdout,