45 lines
1.2 KiB
TypeScript
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;
|
|
}
|
|
}
|