@nu-art/commando
Version:
49 lines (48 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleShell = void 0;
const child_process_1 = require("child_process");
const ts_common_1 = require("@nu-art/ts-common");
const CliError_1 = require("../core/CliError");
class SimpleShell extends ts_common_1.Logger {
constructor() {
super(...arguments);
this._debug = false;
this.cliOptions = { shell: '/bin/bash' };
/**
* Executes the accumulated commands in the command list.
* @returns {Promise<string>} A promise that resolves with the standard output of the executed command.
*/
this.execute = async (command) => {
if (this._debug)
this.logDebug(`executing: `, `"""\n${command}\n"""`);
return new Promise((resolve, reject) => {
(0, child_process_1.exec)(command, this.cliOptions, (error, stdout, stderr) => {
if (error) {
return reject(new CliError_1.CliError(`executing:\n${command}\n`, stdout, stderr, error));
}
if (stdout)
this.logInfo(stdout);
if (stderr)
this.logError(stderr);
resolve({ stdout, stderr });
});
});
};
}
debug(debug) {
this._debug = debug !== null && debug !== void 0 ? debug : !this._debug;
return this;
}
setShell(shell) {
(this.cliOptions || (this.cliOptions = {})).shell = shell;
}
setOptions(options) {
this.cliOptions = options;
}
setUID(uid) {
this.setTag(uid);
return this;
}
}
exports.SimpleShell = SimpleShell;