datasquirel/package-shared/utils/execute.ts
Benjamin Toby c37d105dee Updates
2025-07-09 20:30:45 +01:00

45 lines
1.2 KiB
TypeScript

import { ExecOptions, execSync, ExecSyncOptions } from "child_process";
export default function execute(
cmd: string | string[],
options?: ExecSyncOptions
): string | (string | undefined)[] | undefined {
function runCmd(cmd: string) {
try {
const res = execSync(cmd, {
encoding: "utf-8",
...options,
});
if (typeof res == "string") {
return res.trim();
} else {
return undefined;
}
} catch (error: any) {
console.log(`Execute Run Error =>`, error.message);
console.log(`Execute CMD =>`, cmd);
return undefined;
}
}
try {
if (typeof cmd == "string") {
return runCmd(cmd);
} else if (Array.isArray(cmd)) {
let resArr: (string | undefined)[] = [];
for (let i = 0; i < cmd.length; i++) {
const singleCmd = cmd[i];
const res = runCmd(singleCmd);
resArr.push(res);
}
return resArr;
}
} catch (error: any) {
console.log(`Execute Error =>`, error.message);
return undefined;
}
}