rawx
Version:
process daemon with utilities
323 lines • 13.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/***
* license.kind
* Original: https://github.com/lilzeta
* Flux this tag if/whenever you feel like
*/
// External modules
const format_ = require("util").format;
const Base = require("../util/base");
const some_colors = require("./some_colors");
const no_colors = {
label: "",
default: "",
forky: "",
accent: "",
errata: "",
fleck: "",
};
const Ops = (() => {
// There is only 1 of these closures globally
// stores a singleton instance of _Ops_Gen_Inner
let ops_gen_cached;
// Call ops() with no args to get this ops_cache
// ops_cache is instanced by the first `new Ops()`
let ops_cache;
// Notice, after a whole lot of class code this function
// => returns ops_gen which is afterwards exported as the default module
// Some singleton variables for simple_clean which cleans output
// Where each output call is a buffer chunk of STD_OUT
let global_nope_next_nl = false;
// note: buffer boundaries aren't always line by line or command by command
let global_nope_reg_repl;
const regex = {
// newline spam reduction regex (reduce consecucutive newlines to 2)
// 2 consecucutive means 1 blank line
cons_newlines_g: /\n[\s\t]*\n/g,
starts_with_cons_newlines: /^[\s\t]*\n[\s\t]*\n[\s\t\n]*/,
is_only_newlines_or_ws: /^[\s\t\n]*\n[\s\t\n]*$/g,
mult_trailing_newlines_or_ws: /[\s\t\n]*\n[\s\t]*\n$/,
// no start buffer space
trim_ws_head: /^[\s\t]*/,
// no start newline space
trim_ws_after_newline: /\n[\s\t]*/,
};
class _Ops_Gen_Inner extends Base {
// set_logging_etc_c_proc: (args: Set_Proc_Logger_Args) => void;
constructor(conf) {
super();
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "debug", {
enumerable: true,
configurable: true,
writable: true,
value: 2
});
// our local default basis
Object.defineProperty(this, "colors", {
enumerable: true,
configurable: true,
writable: true,
value: {
label: some_colors.LAVENDER,
default: some_colors.TECHNICOLOR_GREEN,
forky: some_colors.PURPLE,
accent: some_colors.NEON_YELLOW,
errata: some_colors.H_RED,
fleck: some_colors.LAVENDER,
}
});
// log is colors[default]
Object.defineProperty(this, "log", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "accent", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "forky", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "errata", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "format", {
enumerable: true,
configurable: true,
writable: true,
value: (...args) => {
const is_num = (arg) => typeof arg === "number" || typeof arg === "bigint";
const is_a_format_o = (arg) => Array.isArray(arg) || typeof arg === "object";
const arg_f = (arg) => {
if (arg === undefined)
return "`undef`";
if (typeof arg === "string")
return this.simple_clean(arg);
if (is_num(arg))
return "" + arg;
if (is_a_format_o(arg))
return format_("%o", arg);
else
return format_(arg);
};
let jiggler;
const f_args = args.reduce((propogater, arg) => {
(jiggler = arg_f(arg)).length && propogater.push(jiggler);
return propogater;
}, []);
return f_args.join(" ");
}
});
// private because color is escaped!
// use default="" to not change color of passthrough
// This would be important if sub-server has desired color
Object.defineProperty(this, "_logger_rescaffold", {
enumerable: true,
configurable: true,
writable: true,
value: (scaf_args) => {
const { debug = this.debug, io } = scaf_args;
return (min_level, ...args) => {
if (min_level > debug)
return;
// can't use o.log at beginning of constructors
if (debug > 10)
console.log(`_l local called w/type: ${typeof args[0]}`);
if (args.length > 0) {
// WIP flags for work area notation - console.log(get_flag(dis));
io(this.format(...args));
}
};
}
}); // kind
// curry the color type, to a new factory w/pure passthrough, inner is the config curry
Object.defineProperty(this, "boom", {
enumerable: true,
configurable: true,
writable: true,
value: ({ color_target = "default" }) => {
const factory = ({ debug, colors }) => {
const color = colors[color_target];
let io;
if (color.length)
io = (s) => console.log(`%c ${s}`, "L:206", `color: ${color}`);
else
io = (s) => console.log(s);
return this._logger_rescaffold({
debug,
color,
io,
});
};
return factory;
}
}); // ~ kind ~
// these colorizers are rescaffold factory shortcut factories
Object.defineProperty(this, "log_default_industrial", {
enumerable: true,
configurable: true,
writable: true,
value: this.boom({
color_target: "default",
})
}); // kind
Object.defineProperty(this, "log_forky_industrial", {
enumerable: true,
configurable: true,
writable: true,
value: this.boom({
color_target: "forky",
})
}); // kind
Object.defineProperty(this, "log_accent_industrial", {
enumerable: true,
configurable: true,
writable: true,
value: this.boom({
color_target: "accent",
})
}); // kind
Object.defineProperty(this, "log_errata_industrial", {
enumerable: true,
configurable: true,
writable: true,
value: this.boom({
color_target: "errata",
})
}); // kind
// Basically Passthrough \n\n spammers blitzd
Object.defineProperty(this, "simple_clean", {
enumerable: true,
configurable: true,
writable: true,
value: (data = "") => {
// data is sometimes null - TODO what?
if (!(data === null || data === void 0 ? void 0 : data.length))
return "";
data = String(data);
if (global_nope_reg_repl === null || global_nope_reg_repl === void 0 ? void 0 : global_nope_reg_repl.length) {
global_nope_reg_repl.forEach(({ reg, replace = "" }) => {
data = data.replace(reg, replace);
});
}
// use empty for cons newlines allow one \n
if (regex.is_only_newlines_or_ws.exec(data)) {
if (global_nope_next_nl)
return "";
global_nope_next_nl = true;
return "\n";
}
if (global_nope_next_nl && regex.starts_with_cons_newlines.exec(data)) {
if (global_nope_next_nl)
data = data.replace(regex.starts_with_cons_newlines, "\n");
// because there's etc in data
global_nope_next_nl = false;
}
if (regex.mult_trailing_newlines_or_ws.exec(data)) {
global_nope_next_nl = true;
data = data.replace(regex.mult_trailing_newlines_or_ws, "\n\n");
}
data = data.replace(regex.trim_ws_head, "");
return data.replace(regex.trim_ws_after_newline, "\n");
}
});
if (conf) {
let { colors, debug, log_ignore_reg_repl } = conf;
if (log_ignore_reg_repl)
global_nope_reg_repl = log_ignore_reg_repl;
// union over default basis w/our constructor args colors (a new default)
if (colors) {
if (colors === "no") {
this.colors = no_colors;
}
else {
this.colors = Object.assign(Object.assign({}, this.colors), colors);
}
}
if (this.defi(debug))
this.debug = debug;
}
}
// as in clone the basis _fns with conf
ops({ colors: colors_conf = {}, debug: debug_conf } = {}) {
let recolored_basis;
if (colors_conf === "no") {
recolored_basis = no_colors;
}
else {
recolored_basis = Object.assign(Object.assign({}, this.colors), colors_conf);
}
// console.log(`recolored_basis: `);
// console.log(recolored_basis);
const debug = (this.defi(debug_conf) ? debug_conf : this.debug);
return {
debug,
colors: recolored_basis,
// label,
// sub_proc_prefix: pre,
log: this.log_default_industrial({
colors: recolored_basis,
debug,
}),
accent: this.log_accent_industrial({
colors: recolored_basis,
debug,
}),
forky: this.log_forky_industrial({
colors: recolored_basis,
debug,
}),
errata: this.log_errata_industrial({
colors: recolored_basis,
debug,
}),
// aliases of Core super methods
defi: this.defi,
empty: this.empty,
truncate: this.truncate,
keys: this.keys,
entries: this.entries,
wait: this.wait,
pretty: this.pretty,
puff: this.puff,
fuzzy_true: this.fuzzy_true,
fuzzy_false: this.fuzzy_false,
// if_in_get_index: this.if_in_get_index,
simple_clean: this.simple_clean,
};
}
}
// a spoofed class, anonymous and strange, facade for _Ops_Gen_Inner cache
return class IO_Facade {
constructor(conf) {
if (ops_gen_cached) {
if (!conf)
return ops_cache;
return ops_gen_cached.ops(conf);
}
// This happens once
ops_gen_cached = new _Ops_Gen_Inner(conf);
ops_cache = ops_gen_cached.ops(conf);
return ops_cache;
}
}; // It's not Ops_Gen implements, but it is still an Ops_Gen interface
})();
module.exports = Ops;
//# sourceMappingURL=ops.js.map