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