File size: 2,311 Bytes
e81f4e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import { spawn } from "node:child_process";
export function runPnpm(args, options = {}) {
const env = options.env || process.env;
const npmExecPath = env.npm_execpath || process.env.npm_execpath;
if (npmExecPath) {
if (/\.(?:c?js|mjs)$/i.test(npmExecPath)) {
return runCommand(process.execPath, [npmExecPath, ...args], options);
}
return runCommand(npmExecPath, args, options);
}
return runCommand(
process.platform === "win32" ? "pnpm.cmd" : "pnpm",
args,
options,
);
}
export function runCommand(command, args, options = {}) {
return spawnCommand(command, args, {
...options,
stdio: options.stdio || "inherit",
});
}
export function outputCommand(command, args, options = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: options.cwd || process.cwd(),
env: options.env || process.env,
stdio: ["ignore", "pipe", options.allowFailure ? "ignore" : "inherit"],
});
let stdout = "";
child.stdout.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
stdout += chunk;
});
child.on("error", reject);
child.on("exit", (code, signal) => {
if (code === 0 || options.allowFailure) {
resolve(stdout);
return;
}
reject(commandFailure(command, args, code, signal));
});
});
}
export async function commandAvailable(command) {
try {
await outputCommand(command, ["--version"], { allowFailure: true });
return true;
} catch (error) {
return !isMissingExecutableError(error);
}
}
function spawnCommand(command, args, options) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: options.cwd || process.cwd(),
env: options.env || process.env,
stdio: options.stdio,
});
child.on("error", reject);
child.on("exit", (code, signal) => {
if (code === 0 || options.allowFailure) {
resolve({ code, signal });
return;
}
reject(commandFailure(command, args, code, signal));
});
});
}
function commandFailure(command, args, code, signal) {
const suffix = signal ? `signal ${signal}` : `exit code ${code}`;
return new Error(`${command} ${args.join(" ")} failed with ${suffix}`);
}
function isMissingExecutableError(error) {
return (
error &&
typeof error === "object" &&
"code" in error &&
error.code === "ENOENT"
);
}
|