photosite/dist/shell/index.js

68 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-01-06 05:25:07 +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;
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);
this.log.info(`Shell: ${this.bin} ${this.args.join(' ')}`);
2024-12-09 20:15:27 +00:00
this.child.stdout.on('data', (output) => {
const data = output.toString();
2024-01-06 05:25:07 +00:00
if (!this.silent)
this.log.info(data);
if (this.after !== null)
this.lines.push(data);
if (this.stdio !== null) {
this.stdio(data);
}
});
2024-12-09 20:15:27 +00:00
this.child.stderr.on('data', (output) => {
const data = output.toString();
2024-01-06 05:25:07 +00:00
if (!this.silent)
this.log.warn(data);
if (this.stderr !== null) {
this.stderr(data);
}
});
this.child.on('close', (code) => {
if (this.after !== null) {
this.after(this.lines.join(os_1.EOL));
}
if (code === 0) {
this.log.info(`Complete: ${this.bin} ${this.args.join(' ')}`);
return resolve(code);
}
else {
this.log.error(`Error executing: ${this.bin} ${this.args.join(' ')}`);
return reject(code);
}
});
});
}
kill() {
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