@ts-dev-tools/core
Version:
TS dev tools Core
54 lines (53 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CmdService = void 0;
const node_child_process_1 = require("node:child_process");
const node_fs_1 = require("node:fs");
class CmdService {
constructor() { }
static execCmd(args, cwd, silent = false) {
if (!args.length) {
throw new Error("Command args must not be empty");
}
if (cwd) {
if (!(0, node_fs_1.existsSync)(cwd) || !(0, node_fs_1.statSync)(cwd).isDirectory()) {
throw new Error(`Directory "${cwd}" does not exist`);
}
}
let cmd;
if (Array.isArray(args)) {
cmd = args.shift() || "";
}
else {
cmd = args;
args = [];
}
return new Promise((resolve, reject) => {
const child = (0, node_child_process_1.spawn)(cmd, args, {
stdio: silent ? "pipe" : "inherit",
shell: true,
windowsVerbatimArguments: true,
cwd,
});
let output = "";
let error = "";
child.on("exit", (code) => {
if (code) {
return reject(error);
}
resolve(output);
});
if (child.stdout) {
child.stdout.on("data", (data) => {
output += `\n${data}`;
});
}
if (child.stderr) {
child.stderr.on("data", (data) => {
error += `\n${data}`;
});
}
});
}
}
exports.CmdService = CmdService;