UNPKG

rawx

Version:

process daemon with utilities

284 lines 12.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /*** * license.kind * Original: https://github.com/lilzeta * Flux this tag if/whenever you feel like */ const { join, resolve } = require("path"); const Ops = require("../../ops/ops"); const files_tree = (() => { // externals const { readdir, stat } = require("fs/promises"); // Just a quicky with defaults let o = new Ops(); // export type Trunk = readonly [Trunk_Paths, number | undefined]; // export type Trunks = readonly [readonly [str, number | undefined, Trunk_Paths]]; // as in console.log when the glass cracks const get_jukes = (match) => { if (!match) return undefined; // o.accent(1, "L:65", `match`); // o.accent(1, "L:66", match); const _m = map_to_reg_arr(match); // o.accent(1, "L:68", `_m`); // o.accent(1, "L:69", _m); /** * in the parity that delivers * the mostest not not propererist juke * true => terminate branch search * */ /** * inc_match, true => deny * null propogated => false * has include, no match => !false * has include, any match => true => !true * */ const inc = (s, reg_arr, i = 0) => { if (i >= reg_arr.length) return false; return reg_arr[i].exec(s) || inc(s, reg_arr, i + 1); }; /** * ex_match, true => deny * null propogated => false * has exclude => no match => false * has exclude => any match => true * */ const exl = (s, reg_arr, i = 0) => { var _a; if (i >= reg_arr.length) return false; return (_a = reg_arr[i].exec(s)) !== null && _a !== void 0 ? _a : exl(s, reg_arr, i + 1); }; const include = _m.include ? (s) => !inc(s, _m.include) : undefined; const exclude = _m.exclude ? (s) => exl(s, _m.exclude) && true : undefined; const include_dir = _m.include_dir ? (s) => !inc(s, _m.include_dir) : undefined; const exclude_dir = _m.exclude_dir ? (s) => exl(s, _m.exclude_dir) && true : undefined; return { include, exclude, include_dir, exclude_dir }; }; // backasswards in reverse is not a fish const puff = (prop_name, q) => { if (q !== undefined) { return { [prop_name]: str_match_to_reg(q) }; } return undefined; }; // strange one, yet extremely satisfying const map_to_reg_arr = (match) => { return Object.assign(Object.assign(Object.assign(Object.assign({}, puff("include", match.include)), puff("exclude", match.exclude)), puff("include_dir", match.include_dir)), puff("exclude_dir", match.exclude_dir)); }; const str_match_to_reg = (item, flattened = []) => { if (typeof item === "string") { return [str_to_reg(item)]; } if (Array.isArray(item)) { const sub_map = item.map((sub) => { if (typeof sub === "string") { return str_to_reg(sub); } else return sub; }); return sub_map; } if (typeof item !== "object") { throw new Error(`inrecognized match item: ${item}`); } // if (RegExp.prototype === (item as RegExp).prototype)??? if (item.exec) // a bit hack flattened.push(item); return flattened; }; // WIP, prob find some util module to do this, ick const str_to_reg = (s) => { // \s is space const prep = s.replace(" ", "\\s"); // escape extension . const s_joined = prep.split(".").join("\\."); // replace wild with any count of word-char/hyphen const spl_wild = s_joined.split("*").join("[\\w-]*"); // o.accent(1, "L:156", `spl_wild`); // o.accent(1, "L:157", spl_wild); return new RegExp(spl_wild); }; return class File_Tree_Class { constructor(args) { Object.defineProperty(this, "_trunks", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(this, "max_depth", { enumerable: true, configurable: true, writable: true, value: 8 }); Object.defineProperty(this, "root_resolved_paths", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_reg", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "reg_args", { enumerable: true, configurable: true, writable: true, value: void 0 }); // These are all in a way syncro, TODO async ->{...sq} issues // One Quick Squirrel... Object.defineProperty(this, "setup_tree", { enumerable: true, configurable: true, writable: true, value: () => __awaiter(this, void 0, void 0, function* () { const once_per_trunk = (i = 0) => __awaiter(this, void 0, void 0, function* () { const r_path_len = this.root_resolved_paths.length; if (i >= r_path_len) return []; const sq = { depth: 0, pwd: this.root_resolved_paths[i], path_hasher: {}, }; const returned_trunk = yield this.watch_sub_path(sq); const rest_of_trunks = yield once_per_trunk(i + 1); return [returned_trunk, ...rest_of_trunks]; }); this._trunks = yield once_per_trunk(); o.accent(8, "L:205", `this.trunks: `); o.accent(8, "L:206", this._trunks); }) }); Object.defineProperty(this, "trunks", { enumerable: true, configurable: true, writable: true, value: () => { return this._trunks; } }); Object.defineProperty(this, "sub_paths_iterator", { enumerable: true, configurable: true, writable: true, value: (sub_paths, sq) => __awaiter(this, void 0, void 0, function* () { const sq_pwd_enters = sq.pwd; const sq_i_enters = sq.depth; const for_each_path = (i = 0) => __awaiter(this, void 0, void 0, function* () { if (i >= sub_paths.length) return []; // o.errata(1, "L:253", `sub_paths_iterator - sub_paths[${i}]`); // o.errata(1, "L:254", sub_paths[i]); sq.pwd = join(sq_pwd_enters, sub_paths[i]); sq.depth = sq_i_enters + 1; const inner_paths = yield this.watch_sub_path(sq); const next_br_returns = yield for_each_path(i + 1); return [...inner_paths, ...next_br_returns]; }); const ret_paths = yield for_each_path(); // sq.pwd = sq_enters; return ret_paths; }) }); this.reg_args = args.match; if (o.defi(args.max_depth)) this.max_depth = args.max_depth; o.forky(10, "L:169", `args.root_paths:`); o.forky(10, "L:170", args.root_paths); // try to resolve base paths for a quick throw on misconfigured this.root_resolved_paths = args.root_paths.reduce((propogater, root_path) => { try { propogater.push(resolve(root_path)); } catch (err) { throw new Error(`a path sent as a root or absolute path did not resolve: ${root_path}`); } return propogater; }, []); this._reg = get_jukes(args.match); // o.accent(10, "L:186", `this._reg: `); // o.accent(10, "L:187", this._reg); } // TODO catch, this function sees every non root path first watch_sub_path(sq) { var _a, _b, _c, _d, _e, _f, _g, _h; return __awaiter(this, void 0, void 0, function* () { // o.accent(1, "L:215", `watch_sub_path sq: `); // o.accent(1, "L:216", sq); if (sq.depth > this.max_depth) { return []; } const hash = encodeURIComponent(sq.pwd); if (o.defi(sq.path_hasher[hash])) { return []; } sq.path_hasher = Object.assign(sq.path_hasher, [hash]); const stat_info = yield stat(sq.pwd); if (stat_info.isDirectory()) { // o.accent(1, "L:227", `this._reg?.include_dir?.(sq.pwd):`); // o.accent(1, "L:228", this._reg?.include_dir?.(sq.pwd)); // o.accent(1, "L:229", `this._reg?.exclude_dir?.(sq.pwd):`); // o.accent(1, "L:230", this._reg?.exclude_dir?.(sq.pwd)); if ((_b = (_a = this._reg) === null || _a === void 0 ? void 0 : _a.include_dir) === null || _b === void 0 ? void 0 : _b.call(_a, sq.pwd)) return []; if ((_d = (_c = this._reg) === null || _c === void 0 ? void 0 : _c.exclude_dir) === null || _d === void 0 ? void 0 : _d.call(_c, sq.pwd)) return []; if (sq.depth > this.max_depth) { if (ABYSS_DEBUG) { o.log(2, "L:235", `the members of this deep dir have been ignored: ${sq.pwd}`); } return []; } return yield this.read_dir(sq); } else { // non-dir done&leaf if ((_f = (_e = this._reg) === null || _e === void 0 ? void 0 : _e.include) === null || _f === void 0 ? void 0 : _f.call(_e, sq.pwd)) return []; if ((_h = (_g = this._reg) === null || _g === void 0 ? void 0 : _g.exclude) === null || _h === void 0 ? void 0 : _h.call(_g, sq.pwd)) return []; return [sq.pwd]; } }); } // TODO catch read_dir(sq) { return __awaiter(this, void 0, void 0, function* () { const files = yield readdir(sq.pwd); const full_subpaths = []; for (const filename of files) full_subpaths.push(filename); return yield this.sub_paths_iterator(full_subpaths, sq); }); } }; // const files_tree: Files_Tree <= return class File_Tree_Class (with a closure) })(); // TODO WIP removal of String_Keyed, no exports of // interface String_Keyed { // [s: str]: any; // } // TODO configurable for const ABYSS_DEBUG = true; module.exports = files_tree; //# sourceMappingURL=files_tree.js.map