rawx
Version:
process daemon with utilities
443 lines • 19 kB
JavaScript
"use strict";
/***
* license.kind
* Original: https://github.com/lilzeta
* Flux this tag if/whenever you feel like
*/
// module.exports = O_Generator;
Object.defineProperty(exports, "__esModule", { value: true });
// the TLDR, ops is logging operations + util consolidation
// Where import { Ops } from "rawx"
// calling `new Ops(conf)` returns a rebased collection of methods aka an `O`
// where utilized conf is rebased to { ...the_first_conf, ...this_conf }
// External
const format_ = require("node:util").format;
const Base = require("../util/base");
const some_colors = require("./some_colors");
// \033 in hex
const ESCAPE = "\x1B";
// AKA the `SET no color` escape code, in utf8
const NO = ESCAPE + "[0m";
const default_colors = {
label: ESCAPE + some_colors.LAVENDER,
default: ESCAPE + some_colors.TECHNICOLOR_GREEN,
forky: ESCAPE + some_colors.PURPLE,
accent: ESCAPE + some_colors.D_BLUE,
errata: ESCAPE + some_colors.H_RED,
fleck: ESCAPE + some_colors.LAVENDER,
};
const no_colors = {
label: "",
default: "",
forky: "",
accent: "",
errata: "",
fleck: "",
};
const O_Generator = (() => {
// 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;
// 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]*/,
};
// Instantiated once/only-first new Ops(...) call
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, updated only once
Object.defineProperty(this, "colors", {
enumerable: true,
configurable: true,
writable: true,
value: default_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, "k", {
enumerable: true,
configurable: true,
writable: true,
value: Object.keys
});
Object.defineProperty(this, "e", {
enumerable: true,
configurable: true,
writable: true,
value: Object.entries
});
// Partial in case of operation over conf. input
Object.defineProperty(this, "escape_colors", {
enumerable: true,
configurable: true,
writable: true,
value: (conf_colors = {}) => {
let escaped = {};
for (const [k, color] of this.e(conf_colors)) {
if (color === null || color === void 0 ? void 0 : color.length)
escaped[k] = ESCAPE + color;
else
escaped[k] = ""; // aka white or passthrough
}
return escaped;
}
});
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, pre } = scaf_args;
const post = this.post({ is_defi_IO: pre });
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) {
pre === null || pre === void 0 ? void 0 : pre();
// WIP flags for work area notation - console.log(get_flag(dis));
console.log(this.format(...args));
post === null || post === void 0 ? void 0 : post();
}
};
}
}); // 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, pre }) => {
const color = colors[color_target];
return this._logger_rescaffold(Object.assign({ debug,
color }, this.puff("pre", pre)));
};
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
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
// 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: color_conf, debug, log_ignore_reg_repl } = conf;
// Add custom log cleanup
if (log_ignore_reg_repl)
global_nope_reg_repl = log_ignore_reg_repl;
// union args over the default w/args.colors as a new default
if (color_conf) {
if (color_conf === "no") {
this.colors = no_colors;
}
else {
this.colors = Object.assign(Object.assign({}, this.colors), this.escape_colors(color_conf));
}
}
if (this.defi(debug))
this.debug = debug;
}
// This is mostly logging to test our new generator instanced properly
setTimeout(() => {
// where args.debug > 6
ops_cache.log(6, "L:154", `Core utilities setup completed`);
}, 100);
}
// as in clone the basis _fns with conf
ops({ colors: colors_arg = {}, debug: debug_conf, label = "" } = {}) {
let conf_colors;
if (colors_arg === "no") {
conf_colors = no_colors;
}
else {
conf_colors = Object.assign(Object.assign({}, this.colors), this.escape_colors(colors_arg));
}
// console.log(`recolored_basis: `);
// console.log(recolored_basis);
const debug = this.defi(debug_conf) ? debug_conf : this.debug;
return {
debug,
colors: conf_colors,
// label,
log: this.log_default_industrial({
colors: conf_colors,
debug,
pre: this.prefix({
label,
color: conf_colors["label"],
fleck: conf_colors["fleck"],
main_color: conf_colors["default"],
}),
}),
accent: this.log_accent_industrial({
colors: conf_colors,
debug,
pre: this.prefix({
label,
color: conf_colors["label"],
fleck: conf_colors["fleck"],
main_color: conf_colors["accent"],
}),
}),
forky: this.log_forky_industrial({
colors: conf_colors,
debug,
pre: this.prefix({
label,
color: conf_colors["label"],
fleck: conf_colors["fleck"],
main_color: conf_colors["forky"],
}),
}),
errata: this.log_errata_industrial({
colors: conf_colors,
debug,
pre: this.prefix({
label,
color: conf_colors["label"],
fleck: conf_colors["fleck"],
main_color: conf_colors["errata"],
}),
}),
// aliases of Core (super) methods
defi: this.defi,
empty: this.empty,
keys: this.keys,
entries: this.entries,
truncate: this.truncate,
wait: this.wait,
pretty: this.pretty,
simple_clean: this.simple_clean,
puff: this.puff,
fuzzy_true: this.fuzzy_true,
fuzzy_false: this.fuzzy_false,
// if_in_get_index: this.if_in_get_index,
};
}
}
// 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
})();
// Where import { Ops } from "rawx"
// calling `new Ops(conf)` returns a rebased collection of methods aka a `: IO`
// rebasing conf to { ...the_first_conf, ...this_conf }
module.exports = O_Generator;
//# sourceMappingURL=ops.js.map