@h3ravel/shared 0.27.7 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -6,12 +6,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
8
  var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
15
19
  }
16
20
  return to;
17
21
  };
@@ -21,13 +25,14 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
25
  }) : target, mod));
22
26
 
23
27
  //#endregion
28
+ let chalk = require("chalk");
29
+ chalk = __toESM(chalk);
24
30
  let fs_promises = require("fs/promises");
25
31
  let escalade_sync = require("escalade/sync");
26
32
  escalade_sync = __toESM(escalade_sync);
33
+ let fs = require("fs");
27
34
  let path = require("path");
28
35
  path = __toESM(path);
29
- let chalk = require("chalk");
30
- chalk = __toESM(chalk);
31
36
  let inquirer_autocomplete_standalone = require("inquirer-autocomplete-standalone");
32
37
  inquirer_autocomplete_standalone = __toESM(inquirer_autocomplete_standalone);
33
38
  let __inquirer_prompts = require("@inquirer/prompts");
@@ -36,88 +41,272 @@ crypto = __toESM(crypto);
36
41
  let preferred_pm = require("preferred-pm");
37
42
  preferred_pm = __toESM(preferred_pm);
38
43
 
39
- //#region src/Utils/EnvParser.ts
40
- var EnvParser = class {
41
- static parse(initial) {
42
- const parsed = { ...initial };
43
- for (const key in parsed) {
44
- const value = parsed[key];
45
- parsed[key] = this.parseValue(value);
44
+ //#region src/Container.ts
45
+ const INTERNAL_METHODS = Symbol("internal_methods");
46
+
47
+ //#endregion
48
+ //#region src/Mixins/MixinSystem.ts
49
+ /**
50
+ * Helper to mix multiple classes into one, this allows extending multiple classes by any single class
51
+ *
52
+ * @param bases
53
+ * @returns
54
+ */
55
+ const mix = (...bases) => {
56
+ class Base {
57
+ constructor(...args) {
58
+ let instance = this;
59
+ for (const constructor of bases) {
60
+ const result = Reflect.construct(constructor, args, new.target);
61
+ if (result && (typeof result === "object" || typeof result === "function")) {
62
+ if (result !== instance) {
63
+ Object.assign(result, instance);
64
+ instance = result;
65
+ }
66
+ }
67
+ }
68
+ return instance;
46
69
  }
47
- return parsed;
48
70
  }
49
- static parseValue(value) {
50
- /**
51
- * Null/undefined stay untouched
52
- */
53
- if (value === null || value === void 0) return value;
54
- /**
55
- * Convert string "true"/"false" to boolean
56
- */
57
- if (value === "true") return true;
58
- if (value === "false") return false;
59
- /**
60
- * Convert string numbers to number
61
- */
62
- if (!isNaN(value) && value.trim() !== "") return Number(value);
63
- /**
64
- * Convert string "null" and "undefined"
65
- */
66
- if (value === "null") return null;
67
- if (value === "undefined") return void 0;
68
- /**
69
- * Otherwise return as-is (string)
70
- */
71
- return value;
71
+ for (let i = 0; i < bases.length; i++) {
72
+ const currentBase = bases[i];
73
+ const nextBase = bases[i + 1];
74
+ Object.getOwnPropertyNames(currentBase.prototype).forEach((prop) => {
75
+ if (prop !== "constructor") Object.defineProperty(Base.prototype, prop, Object.getOwnPropertyDescriptor(currentBase.prototype, prop));
76
+ });
77
+ Object.getOwnPropertyNames(currentBase).forEach((prop) => {
78
+ if (![
79
+ "prototype",
80
+ "name",
81
+ "length"
82
+ ].includes(prop)) Object.defineProperty(Base, prop, Object.getOwnPropertyDescriptor(currentBase, prop));
83
+ });
84
+ if (nextBase) {
85
+ Object.setPrototypeOf(currentBase.prototype, nextBase.prototype);
86
+ Object.setPrototypeOf(currentBase, nextBase);
87
+ }
72
88
  }
89
+ Object.setPrototypeOf(Base.prototype, bases[0].prototype);
90
+ Object.setPrototypeOf(Base, bases[0]);
91
+ return Base;
73
92
  };
74
93
 
75
94
  //#endregion
76
- //#region src/Utils/FileSystem.ts
77
- var FileSystem = class {
78
- static findModulePkg(moduleId, cwd) {
79
- const parts = moduleId.replace(/\\/g, "/").split("/");
80
- let packageName = "";
81
- if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
82
- packageName += parts.shift();
83
- const packageJson = path.default.join(cwd ?? process.cwd(), "node_modules", packageName);
84
- const resolved = this.resolveFileUp("package", ["json"], packageJson);
85
- if (!resolved) return;
86
- return path.default.join(path.default.dirname(resolved), parts.join("/"));
87
- }
88
- /**
89
- * Check if file exists
90
- *
91
- * @param path
92
- * @returns
93
- */
94
- static async fileExists(path$2) {
95
- try {
96
- await (0, fs_promises.access)(path$2);
97
- return true;
98
- } catch {
99
- return false;
95
+ //#region src/Mixins/TraitSystem.ts
96
+ const crcTable = [];
97
+ for (let n = 0; n < 256; n++) {
98
+ let c = n;
99
+ for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
100
+ crcTable[n] = c;
101
+ }
102
+ const crc32 = (str) => {
103
+ let crc = -1;
104
+ for (let i = 0; i < str.length; i++) crc = crc >>> 8 ^ crcTable[(crc ^ str.charCodeAt(i)) & 255];
105
+ return (crc ^ -1) >>> 0;
106
+ };
107
+ const isCons = (fn) => typeof fn === "function" && !!fn.prototype && !!fn.prototype.constructor;
108
+ const isTypeFactory = (fn) => typeof fn === "function" && !fn.prototype && fn.length === 0;
109
+ function trait(...args) {
110
+ const factory = args.length === 2 ? args[1] : args[0];
111
+ const superTraits = args.length === 2 ? args[0] : void 0;
112
+ return {
113
+ id: crc32(factory.toString()),
114
+ symbol: Symbol("trait"),
115
+ factory,
116
+ superTraits
117
+ };
118
+ }
119
+ const extendProperties = (cons, field, value) => Object.defineProperty(cons, field, {
120
+ value,
121
+ enumerable: false,
122
+ writable: false
123
+ });
124
+ const rawTrait = (x) => isTypeFactory(x) ? x() : x;
125
+ const deriveTrait = (trait$, baseClz, derived) => {
126
+ const trait$1 = rawTrait(trait$);
127
+ let clz = baseClz;
128
+ if (!derived.has(trait$1.id)) {
129
+ derived.set(trait$1.id, true);
130
+ if (trait$1.superTraits !== void 0) for (const superTrait of reverseTraitList(trait$1.superTraits)) clz = deriveTrait(superTrait, clz, derived);
131
+ clz = trait$1.factory(clz);
132
+ extendProperties(clz, "id", crc32(trait$1.factory.toString()));
133
+ extendProperties(clz, trait$1.symbol, true);
134
+ }
135
+ return clz;
136
+ };
137
+ const reverseTraitList = (traits) => traits.slice().reverse();
138
+ function use(...traits) {
139
+ if (traits.length === 0) throw new Error("invalid number of parameters (expected one or more traits)");
140
+ let clz;
141
+ let lot;
142
+ const last = traits[traits.length - 1];
143
+ if (isCons(last) && !isTypeFactory(last)) {
144
+ clz = last;
145
+ lot = traits.slice(0, -1);
146
+ } else {
147
+ clz = class ROOT {};
148
+ lot = traits;
149
+ }
150
+ const derived = /* @__PURE__ */ new Map();
151
+ for (const trait$1 of reverseTraitList(lot)) clz = deriveTrait(trait$1, clz, derived);
152
+ return clz;
153
+ }
154
+ function uses(instance, trait$1) {
155
+ if (typeof instance !== "object" || instance === null) return false;
156
+ let obj = instance;
157
+ if (isCons(trait$1) && !isTypeFactory(trait$1)) return instance instanceof trait$1;
158
+ const idTrait = (isTypeFactory(trait$1) ? trait$1() : trait$1)["id"];
159
+ while (obj) {
160
+ if (Object.hasOwn(obj, "constructor")) {
161
+ if ((obj.constructor["id"] ?? 0) === idTrait) return true;
100
162
  }
163
+ obj = Object.getPrototypeOf(obj);
101
164
  }
102
- /**
103
- * Recursively find files starting from given cwd
104
- *
105
- * @param name
106
- * @param extensions
107
- * @param cwd
108
- *
109
- * @returns
110
- */
111
- static resolveFileUp(name, extensions, cwd) {
112
- cwd ??= process.cwd();
113
- return (0, escalade_sync.default)(cwd, (dir, filesNames) => {
114
- if (typeof extensions === "function") return extensions(dir, filesNames);
115
- const candidates = new Set(extensions.map((ext) => `${name}.${ext}`));
116
- for (const filename of filesNames) if (candidates.has(filename)) return filename;
117
- return false;
118
- }) ?? void 0;
165
+ return false;
166
+ }
167
+
168
+ //#endregion
169
+ //#region src/Mixins/UseFinalizable.ts
170
+ /**
171
+ * the central class instance registry
172
+ */
173
+ const registry = new FinalizationRegistry((fn) => {
174
+ if (typeof fn === "function" && !fn.finalized) {
175
+ fn.finalized = true;
176
+ fn();
177
+ }
178
+ });
179
+ /**
180
+ * the API trait "Finalizable<T>"
181
+ */
182
+ const Finalizable = trait((base) => class Finalizable$1 extends base {
183
+ constructor(...args) {
184
+ super(...args);
185
+ const fn1 = this.$finalize;
186
+ if (typeof fn1 !== "function") throw new Error("trait Finalizable requires a $finalize method to be defined");
187
+ const fn2 = () => {
188
+ fn1(this);
189
+ };
190
+ fn2.finalized = false;
191
+ registry.register(this, fn2, this);
192
+ }
193
+ });
194
+
195
+ //#endregion
196
+ //#region src/Mixins/UseMagic.ts
197
+ /**
198
+ * Wraps an object in a Proxy to emulate PHP magic methods.
199
+ *
200
+ * Supported:
201
+ * - __call(method, args)
202
+ * - __get(property)
203
+ * - __set(property, value)
204
+ * - __isset(property)
205
+ * - __unset(property)
206
+ *
207
+ * Called automatically by Magic's constructor.
208
+ *
209
+ * Return in any class constructor to use
210
+ *
211
+ * @param target
212
+ * @returns
213
+ */
214
+ function makeMagic(target) {
215
+ return new Proxy(target, {
216
+ get(obj, prop, receiver) {
217
+ if (typeof prop === "string") {
218
+ if (prop in obj) return Reflect.get(obj, prop, receiver);
219
+ if (obj.__call) return (...args) => obj.__call(prop, args);
220
+ if (obj.__get) return obj.__get(prop);
221
+ }
222
+ },
223
+ set(obj, prop, value) {
224
+ if (typeof prop === "string" && obj.__set) {
225
+ obj.__set(prop, value);
226
+ return true;
227
+ }
228
+ return Reflect.set(obj, prop, value);
229
+ },
230
+ has(obj, prop) {
231
+ if (typeof prop === "string" && obj.__isset) return obj.__isset(prop);
232
+ return Reflect.has(obj, prop);
233
+ },
234
+ deleteProperty(obj, prop) {
235
+ if (typeof prop === "string" && obj.__unset) {
236
+ obj.__unset(prop);
237
+ return true;
238
+ }
239
+ return Reflect.deleteProperty(obj, prop);
240
+ }
241
+ });
242
+ }
243
+ /**
244
+ * Wraps a class constructor in a Proxy to emulate static PHP magic methods.
245
+ *
246
+ * Supported:
247
+ * - __callStatic(method, args)
248
+ * - static __get(property)
249
+ * - static __set(property, value)
250
+ * - static __isset(property)
251
+ * - static __unset(property)
252
+ *
253
+ * @param cls
254
+ * @returns
255
+ */
256
+ function makeStaticMagic(cls) {
257
+ return new Proxy(cls, {
258
+ get(target, prop) {
259
+ if (typeof prop === "string") {
260
+ if (prop in target) return target[prop];
261
+ if (target.__callStatic) return (...args) => target.__callStatic(prop, args);
262
+ if (target.__get) return target.__get(prop);
263
+ }
264
+ },
265
+ set(target, prop, value) {
266
+ if (typeof prop === "string" && target.__set) {
267
+ target.__set(prop, value);
268
+ return true;
269
+ }
270
+ return Reflect.set(target, prop, value);
271
+ },
272
+ has(target, prop) {
273
+ if (typeof prop === "string" && target.__isset) return target.__isset(prop);
274
+ return Reflect.has(target, prop);
275
+ },
276
+ deleteProperty(target, prop) {
277
+ if (typeof prop === "string" && target.__unset) {
278
+ target.__unset(prop);
279
+ return true;
280
+ }
281
+ return Reflect.deleteProperty(target, prop);
282
+ }
283
+ });
284
+ }
285
+ /**
286
+ * Base class that enables PHP-style magic methods automatically.
287
+ *
288
+ * Any subclass may implement:
289
+ * - __call
290
+ * - __get
291
+ * - __set
292
+ * - __isset
293
+ * - __unset
294
+ *
295
+ * The constructor returns a Proxy transparently.
296
+ */
297
+ var Magic = class {
298
+ constructor() {
299
+ return makeMagic(this);
119
300
  }
120
301
  };
302
+ const UseMagic = trait((Base) => {
303
+ return class Magic$1 extends Base {
304
+ constructor(...args) {
305
+ super(...args);
306
+ return makeMagic(this);
307
+ }
308
+ };
309
+ });
121
310
 
122
311
  //#endregion
123
312
  //#region src/Utils/Logger.ts
@@ -195,6 +384,17 @@ var Logger = class Logger {
195
384
  * @returns
196
385
  */
197
386
  static textFormat(txt, color, preserveCol = false) {
387
+ if (txt instanceof Error) {
388
+ const err = txt;
389
+ const code = err.code ?? err.statusCode ? ` (${err.code ?? err.statusCode})` : "";
390
+ const output = [];
391
+ if (err.message) output.push(this.textFormat(`${err.constructor.name}${code}: ${err.message}`, chalk.default.bgRed, preserveCol));
392
+ if (err.stack) output.push(" " + chalk.default.white(err.stack.replace(`${err.name}: ${err.message}`, "").trim()));
393
+ return output.join("\n");
394
+ }
395
+ if (Array.isArray(txt)) return txt.map((e) => this.textFormat(e, color, preserveCol)).join("\n");
396
+ if (typeof txt === "object") return this.textFormat(Object.values(txt), color, preserveCol);
397
+ if (typeof txt !== "string") return color(txt);
198
398
  const str = String(txt);
199
399
  if (preserveCol) return str;
200
400
  const [first, ...rest] = str.split(":");
@@ -291,9 +491,129 @@ var Logger = class Logger {
291
491
  if (typeof config === "string") {
292
492
  const conf = [[config, joiner]];
293
493
  return this.parse(conf, "", log, sc);
294
- } else if (config) return this.parse(config, String(joiner), log, sc);
494
+ } else if (Array.isArray(config)) return this.parse(config, String(joiner), log, sc);
495
+ else if (log && !this.shouldSuppressOutput("line")) return console.log(this.textFormat(config, Logger.chalker(["blue"])));
295
496
  return this;
296
497
  });
498
+ /**
499
+ * A simple console like output logger
500
+ *
501
+ * @returns
502
+ */
503
+ static console() {
504
+ return Console;
505
+ }
506
+ };
507
+
508
+ //#endregion
509
+ //#region src/Utils/Console.ts
510
+ var Console = class {
511
+ static log = (...args) => Logger.log(args.map((e) => [e, "white"]));
512
+ static debug = (...args) => Logger.debug(args, false, true);
513
+ static warn = (...args) => args.map((e) => Logger.warn(e, false, true));
514
+ static info = (...args) => args.map((e) => Logger.info(e, false, true));
515
+ static error = (...args) => args.map((e) => Logger.error(e, false), true);
516
+ };
517
+
518
+ //#endregion
519
+ //#region src/Utils/EnvParser.ts
520
+ var EnvParser = class {
521
+ static parse(initial) {
522
+ const parsed = { ...initial };
523
+ for (const key in parsed) {
524
+ const value = parsed[key];
525
+ parsed[key] = this.parseValue(value);
526
+ }
527
+ return parsed;
528
+ }
529
+ static parseValue(value) {
530
+ /**
531
+ * Null/undefined stay untouched
532
+ */
533
+ if (value === null || value === void 0) return value;
534
+ /**
535
+ * Convert string "true"/"false" to boolean
536
+ */
537
+ if (value === "true") return true;
538
+ if (value === "false") return false;
539
+ /**
540
+ * Convert string numbers to number
541
+ */
542
+ if (!isNaN(value) && value.trim() !== "") return Number(value);
543
+ /**
544
+ * Convert string "null" and "undefined"
545
+ */
546
+ if (value === "null") return null;
547
+ if (value === "undefined") return void 0;
548
+ /**
549
+ * Otherwise return as-is (string)
550
+ */
551
+ return value;
552
+ }
553
+ };
554
+
555
+ //#endregion
556
+ //#region src/Utils/FileSystem.ts
557
+ var FileSystem = class {
558
+ static findModulePkg(moduleId, cwd) {
559
+ const parts = moduleId.replace(/\\/g, "/").split("/");
560
+ let packageName = "";
561
+ if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
562
+ packageName += parts.shift();
563
+ const packageJson = path.default.join(cwd ?? process.cwd(), "node_modules", packageName);
564
+ const resolved = this.resolveFileUp("package", ["json"], packageJson);
565
+ if (!resolved) return;
566
+ return path.default.join(path.default.dirname(resolved), parts.join("/"));
567
+ }
568
+ /**
569
+ * Check if file exists
570
+ *
571
+ * @param path
572
+ * @returns
573
+ */
574
+ static async fileExists(path$2) {
575
+ try {
576
+ await (0, fs_promises.access)(path$2);
577
+ return true;
578
+ } catch {
579
+ return false;
580
+ }
581
+ }
582
+ /**
583
+ * Recursively find files starting from given cwd
584
+ *
585
+ * @param name
586
+ * @param extensions
587
+ * @param cwd
588
+ *
589
+ * @returns
590
+ */
591
+ static resolveFileUp(name, extensions, cwd) {
592
+ cwd ??= process.cwd();
593
+ return (0, escalade_sync.default)(cwd, (dir, filesNames) => {
594
+ if (typeof extensions === "function") return extensions(dir, filesNames);
595
+ const candidates = new Set(extensions.map((ext) => `${name}.${ext}`));
596
+ for (const filename of filesNames) if (candidates.has(filename)) return filename;
597
+ return false;
598
+ }) ?? void 0;
599
+ }
600
+ /**
601
+ * Recursively find files starting from given cwd
602
+ *
603
+ * @param name
604
+ * @param extensions
605
+ * @param cwd
606
+ *
607
+ * @returns
608
+ */
609
+ static resolveModulePath(moduleId, pathName, cwd) {
610
+ pathName = Array.isArray(pathName) ? pathName : [pathName];
611
+ const module$1 = this.findModulePkg(moduleId, cwd) ?? "";
612
+ for (const name of pathName) {
613
+ const file = path.default.join(module$1, name);
614
+ if ((0, fs.existsSync)(file)) return file;
615
+ }
616
+ }
297
617
  };
298
618
 
299
619
  //#endregion
@@ -307,7 +627,8 @@ var PathLoader = class {
307
627
  config: "/src/config",
308
628
  public: "/public",
309
629
  storage: "/storage",
310
- database: "/src/database"
630
+ database: "/src/database",
631
+ commands: "/src/App/Console/Commands/"
311
632
  };
312
633
  /**
313
634
  * Dynamically retrieves a path property from the class.
@@ -336,6 +657,11 @@ var PathLoader = class {
336
657
  if (base && name !== "base") this.paths[name] = path.default.join(base, path$2);
337
658
  this.paths[name] = path$2;
338
659
  }
660
+ distPath(path$2, skipExt = false) {
661
+ path$2 = path$2.replace("/src/", `/${process.env.DIST_DIR ?? "src"}/`.replace(/([^:]\/)\/+/g, "$1"));
662
+ if (!skipExt) path$2 = path$2.replace(/\.(ts|tsx|mts|cts)$/, ".js");
663
+ return path.default.normalize(path$2);
664
+ }
339
665
  };
340
666
 
341
667
  //#endregion
@@ -455,7 +781,7 @@ const mainTsconfig = {
455
781
  },
456
782
  target: "es2022",
457
783
  module: "es2022",
458
- moduleResolution: "Node",
784
+ moduleResolution: "bundler",
459
785
  esModuleInterop: true,
460
786
  strict: true,
461
787
  allowJs: true,
@@ -525,14 +851,26 @@ var TaskManager = class {
525
851
  };
526
852
 
527
853
  //#endregion
854
+ exports.Console = Console;
528
855
  exports.EnvParser = EnvParser;
529
856
  exports.FileSystem = FileSystem;
857
+ exports.Finalizable = Finalizable;
858
+ exports.INTERNAL_METHODS = INTERNAL_METHODS;
530
859
  exports.Logger = Logger;
860
+ exports.Magic = Magic;
531
861
  exports.PathLoader = PathLoader;
532
862
  exports.Prompts = Prompts;
533
863
  exports.Resolver = Resolver;
534
864
  exports.TaskManager = TaskManager;
865
+ exports.UseMagic = UseMagic;
535
866
  exports.baseTsconfig = baseTsconfig;
867
+ exports.crc32 = crc32;
536
868
  exports.mainTsconfig = mainTsconfig;
869
+ exports.makeMagic = makeMagic;
870
+ exports.makeStaticMagic = makeStaticMagic;
871
+ exports.mix = mix;
537
872
  exports.packageJsonScript = packageJsonScript;
873
+ exports.trait = trait;
874
+ exports.use = use;
875
+ exports.uses = uses;
538
876
  //# sourceMappingURL=index.cjs.map