filmout_manager/dist/shell/index.js

74 lines
2.6 KiB
JavaScript
Raw Normal View History

"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;
if (!this.silent)
this.log = (0, log_1.createLog)(bin);
}
async execute() {
return new Promise((resolve, reject) => {
this.child = (0, child_process_1.spawn)(this.bin, this.args);
if (!this.silent)
this.log.info(`Shell: ${this.bin} ${this.args.join(' ')}`);
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) {
let lines = data.toString().split(os_1.EOL).filter(el => el.trim() !== '');
for (let line of lines) {
this.stdio(line);
}
}
});
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) {
if (!this.silent)
this.log.info(`Complete: ${this.bin} ${this.args.join(' ')}`);
return resolve(code);
}
else {
if (!this.silent)
this.log.error(`Error executing: ${this.bin} ${this.args.join(' ')}`);
return reject(code);
}
});
});
}
kill() {
if (!this.silent)
this.log.warn(`Killing: ${this.bin} ${this.args.join(' ')}`);
//this.child.stdin.pause();
this.child.kill();
}
}
exports.Shell = Shell;
module.exports = { Shell };
//# sourceMappingURL=index.js.map