2024-04-26 17:39:08 +00:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.Shell = void 0;
|
|
|
|
const child_process_1 = require("child_process");
|
|
|
|
const log_1 = require("../log");
|
|
|
|
const os_1 = require("os");
|
|
|
|
class Shell {
|
|
|
|
constructor(args, stdio = null, stderr = null, after = null, silent = false) {
|
|
|
|
this.lines = [];
|
|
|
|
this.stdio = null;
|
|
|
|
this.stderr = null;
|
|
|
|
this.after = null;
|
|
|
|
this.silent = false;
|
|
|
|
const bin = args.shift();
|
|
|
|
this.bin = bin;
|
|
|
|
this.args = args;
|
|
|
|
this.stdio = stdio;
|
|
|
|
this.stderr = stderr;
|
|
|
|
this.silent = silent;
|
|
|
|
this.after = after;
|
2024-04-27 17:30:02 +00:00
|
|
|
if (!this.silent)
|
|
|
|
this.log = (0, log_1.createLog)(bin);
|
2024-04-26 17:39:08 +00:00
|
|
|
}
|
|
|
|
async execute() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.child = (0, child_process_1.spawn)(this.bin, this.args);
|
2024-04-27 17:30:02 +00:00
|
|
|
if (!this.silent)
|
|
|
|
this.log.info(`Shell: ${this.bin} ${this.args.join(' ')}`);
|
2024-04-26 17:39:08 +00:00
|
|
|
this.child.stdout.on('data', (data) => {
|
|
|
|
if (!this.silent)
|
|
|
|
this.log.info(data.toString());
|
|
|
|
if (this.after !== null)
|
|
|
|
this.lines.push(data);
|
|
|
|
if (this.stdio !== null) {
|
2024-04-27 17:30:02 +00:00
|
|
|
let lines = data.toString().split(os_1.EOL).filter(el => el.trim() !== '');
|
|
|
|
for (let line of lines) {
|
|
|
|
this.stdio(line);
|
|
|
|
}
|
2024-04-26 17:39:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.child.stderr.on('data', (data) => {
|
|
|
|
if (!this.silent)
|
|
|
|
this.log.warn(data.toString());
|
|
|
|
if (this.stderr !== null) {
|
|
|
|
this.stderr(data.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.child.on('close', (code) => {
|
|
|
|
if (this.after !== null) {
|
|
|
|
this.after(this.lines.join(os_1.EOL));
|
|
|
|
}
|
|
|
|
if (code === 0) {
|
2024-04-27 17:30:02 +00:00
|
|
|
if (!this.silent)
|
|
|
|
this.log.info(`Complete: ${this.bin} ${this.args.join(' ')}`);
|
2024-04-26 17:39:08 +00:00
|
|
|
return resolve(code);
|
|
|
|
}
|
|
|
|
else {
|
2024-04-27 17:30:02 +00:00
|
|
|
if (!this.silent)
|
|
|
|
this.log.error(`Error executing: ${this.bin} ${this.args.join(' ')}`);
|
2024-04-26 17:39:08 +00:00
|
|
|
return reject(code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
kill() {
|
2024-04-27 17:30:02 +00:00
|
|
|
if (!this.silent)
|
|
|
|
this.log.warn(`Killing: ${this.bin} ${this.args.join(' ')}`);
|
2024-04-26 17:39:08 +00:00
|
|
|
//this.child.stdin.pause();
|
|
|
|
this.child.kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.Shell = Shell;
|
|
|
|
module.exports = { Shell };
|
|
|
|
//# sourceMappingURL=index.js.map
|