rawx
Version:
process daemon with utilities
175 lines • 8.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// const ESCAPE = "\x1B";
// AKA SET no color escape code (in utf mode)
const NO = `\x1B[0m`;
// Proc_Util_Facade behaves as would exposed inner _Proc_Util
class Proc_Util_Facade {
constructor(args) {
return proc_util_creator(args);
}
}
// Now we expose _Watch through Watch_Facade as if we created it w/vanilla
const Proc_Util = Proc_Util_Facade;
const proc_util_creator = (args) => {
let o;
class _Proc_Util {
constructor(args) {
// There isn't an elegant module for this, it's half Ops/half proc runtime
// Used after each subproc starts, may move this to another class
Object.defineProperty(this, "setup_subproc", {
enumerable: true,
configurable: true,
writable: true,
value: ({ sub_proc, silence, on_close }) => {
var _a, _b, _c;
let jiggler;
const colors = o.colors;
// No <-> deco on normal stdout/stderr - color pass-through
if (!silence) {
let sub_pass_through;
// no label for proc std_out
if ((_a = o.colors.default) === null || _a === void 0 ? void 0 : _a.length)
sub_pass_through = () => this.stdout(colors.default);
let post = this.post({ is_defi_IO: sub_pass_through });
sub_proc.stdout.on("data", (data) => {
if ((jiggler = o.simple_clean(data)).length) {
sub_pass_through === null || sub_pass_through === void 0 ? void 0 : sub_pass_through();
this.stdout(jiggler);
post === null || post === void 0 ? void 0 : post();
}
});
}
if (silence !== "all" && o.debug) {
let err_sub_pass_through;
// no label for proc std_out
if ((_b = colors.errata) === null || _b === void 0 ? void 0 : _b.length)
err_sub_pass_through = () => this.stdout(colors.errata);
let err_post = this.post({ is_defi_IO: err_sub_pass_through });
sub_proc.stderr.on("data", (data) => {
if ((jiggler = o.simple_clean(data)).length) {
err_sub_pass_through === null || err_sub_pass_through === void 0 ? void 0 : err_sub_pass_through();
console.log(jiggler);
err_post === null || err_post === void 0 ? void 0 : err_post();
}
});
// WIP - _TODO_ (Multiple subpass labels affixed!)
let forky_sub_pass_through;
// no label for proc std_out
if ((_c = colors.forky) === null || _c === void 0 ? void 0 : _c.length)
forky_sub_pass_through = this.prefix({
// label: o.label,
color: colors["label"],
fleck: colors["fleck"],
main_color: colors["forky"],
});
let forky_post = this.post({ is_defi_IO: forky_sub_pass_through });
sub_proc.on("close", (code) => {
// With named label
if (o.debug) {
forky_sub_pass_through === null || forky_sub_pass_through === void 0 ? void 0 : forky_sub_pass_through();
o.forky(2, "L:101", `</child-process> \\\\_ closed w/code: ${code}`);
forky_post === null || forky_post === void 0 ? void 0 : forky_post();
}
on_close(sub_proc.pid);
});
}
}
}); // kind
// in case anyone wants a subproc output like inherited
Object.defineProperty(this, "basic_proc_stdio", {
enumerable: true,
configurable: true,
writable: true,
value: (proc, silencer) => {
if (silencer === "all")
return;
if (!silencer)
proc.stdout.on("data", (data) => {
process.stdout._write(data, "utf8", () => { });
});
proc.stderr.on("data", (data) => {
process.stderr._write(data, "utf8", () => { });
});
proc.stderr.on("close", () => {
const out = `>>>process was closed`;
process.stderr._write(out, "utf8", () => { });
});
}
});
Object.defineProperty(this, "prefix", {
enumerable: true,
configurable: true,
writable: true,
value: ({ label = "", color, fleck, main_color }) => {
let _color;
if (!label.length) {
if (main_color === null || main_color === void 0 ? void 0 : main_color.length)
return () => this.stdout(main_color);
// else should already be NO color
return undefined;
}
let pre = `<${label}>`;
if (color === null || color === void 0 ? void 0 : color.length) {
_color = color;
pre = (color === null || color === void 0 ? void 0 : color.length) ? color + pre : pre;
}
pre += " ";
// color fleck="" -> also no fleck
if (fleck === null || fleck === void 0 ? void 0 : fleck.length) {
if (fleck !== _color) {
pre += fleck;
}
_color = fleck;
pre += "- ";
}
if (_color !== main_color) {
if (main_color.length)
pre += main_color;
else
pre += NO;
}
// return () => this.stdout(pre + "___");
return () => this.stdout(pre);
}
});
Object.defineProperty(this, "post", {
enumerable: true,
configurable: true,
writable: true,
value: ({ is_defi_IO }) => {
if (is_defi_IO)
return () => this.stdout(NO);
// => else return undefined
}
});
// Console.log with no newline
Object.defineProperty(this, "stdout", {
enumerable: true,
configurable: true,
writable: true,
value: (some_str) => {
if (some_str === null || some_str === void 0 ? void 0 : some_str.length)
process.stdout._write(some_str, "utf8", () => { });
}
}); // kind
o = args.inherit_ops;
}
is_fn_proc(proc) {
return proc.type === "exec_fn" || proc.type === "fn" || false;
}
is_repeater_proc(proc) {
var _a;
return ((proc.type === "spawn" || proc.type === "execFile") &&
Array.isArray((_a = proc.construct.args) === null || _a === void 0 ? void 0 : _a[0]));
}
}
return new _Proc_Util(args);
};
// // The following 4 may move out of this
// prefix: LabelWrap; // subproc ops...
// post: PostWrap;
// sub_proc_prefix: IO;
// stdout: Std_IO;
module.exports = Proc_Util;
//# sourceMappingURL=proc_util.js.map