@h3ravel/shared 0.27.7 → 0.28.1

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,292 @@ 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
+ * Decorator to mark class properties as internal
48
+ *
49
+ * @param target
50
+ * @param propertyKey
51
+ */
52
+ const internal = (target, propertyKey) => {
53
+ if (!target[INTERNAL_METHODS]) target[INTERNAL_METHODS] = /* @__PURE__ */ new Set();
54
+ target[INTERNAL_METHODS].add(propertyKey);
55
+ };
56
+ /**
57
+ * Checks if a property is decorated with the &#64;internal decorator
58
+ *
59
+ * @param instance
60
+ * @param prop
61
+ * @returns
62
+ */
63
+ const isInternal = (instance, prop) => {
64
+ return Object.getPrototypeOf(instance)[INTERNAL_METHODS]?.has(prop) ?? false;
65
+ };
66
+
67
+ //#endregion
68
+ //#region src/Mixins/MixinSystem.ts
69
+ /**
70
+ * Helper to mix multiple classes into one, this allows extending multiple classes by any single class
71
+ *
72
+ * @param bases
73
+ * @returns
74
+ */
75
+ const mix = (...bases) => {
76
+ class Base {
77
+ constructor(...args) {
78
+ let instance = this;
79
+ for (const constructor of bases) {
80
+ const result = Reflect.construct(constructor, args, new.target);
81
+ if (result && (typeof result === "object" || typeof result === "function")) {
82
+ if (result !== instance) {
83
+ Object.assign(result, instance);
84
+ instance = result;
85
+ }
86
+ }
87
+ }
88
+ return instance;
46
89
  }
47
- return parsed;
48
90
  }
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;
91
+ for (let i = 0; i < bases.length; i++) {
92
+ const currentBase = bases[i];
93
+ const nextBase = bases[i + 1];
94
+ Object.getOwnPropertyNames(currentBase.prototype).forEach((prop) => {
95
+ if (prop !== "constructor") Object.defineProperty(Base.prototype, prop, Object.getOwnPropertyDescriptor(currentBase.prototype, prop));
96
+ });
97
+ Object.getOwnPropertyNames(currentBase).forEach((prop) => {
98
+ if (![
99
+ "prototype",
100
+ "name",
101
+ "length"
102
+ ].includes(prop)) Object.defineProperty(Base, prop, Object.getOwnPropertyDescriptor(currentBase, prop));
103
+ });
104
+ if (nextBase) {
105
+ Object.setPrototypeOf(currentBase.prototype, nextBase.prototype);
106
+ Object.setPrototypeOf(currentBase, nextBase);
107
+ }
72
108
  }
109
+ Object.setPrototypeOf(Base.prototype, bases[0].prototype);
110
+ Object.setPrototypeOf(Base, bases[0]);
111
+ return Base;
73
112
  };
74
113
 
75
114
  //#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;
115
+ //#region src/Mixins/TraitSystem.ts
116
+ const crcTable = [];
117
+ for (let n = 0; n < 256; n++) {
118
+ let c = n;
119
+ for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
120
+ crcTable[n] = c;
121
+ }
122
+ const crc32 = (str) => {
123
+ let crc = -1;
124
+ for (let i = 0; i < str.length; i++) crc = crc >>> 8 ^ crcTable[(crc ^ str.charCodeAt(i)) & 255];
125
+ return (crc ^ -1) >>> 0;
126
+ };
127
+ const isCons = (fn) => typeof fn === "function" && !!fn.prototype && !!fn.prototype.constructor;
128
+ const isTypeFactory = (fn) => typeof fn === "function" && !fn.prototype && fn.length === 0;
129
+ function trait(...args) {
130
+ const factory = args.length === 2 ? args[1] : args[0];
131
+ const superTraits = args.length === 2 ? args[0] : void 0;
132
+ return {
133
+ id: crc32(factory.toString()),
134
+ symbol: Symbol("trait"),
135
+ factory,
136
+ superTraits
137
+ };
138
+ }
139
+ const extendProperties = (cons, field, value) => Object.defineProperty(cons, field, {
140
+ value,
141
+ enumerable: false,
142
+ writable: false
143
+ });
144
+ const rawTrait = (x) => isTypeFactory(x) ? x() : x;
145
+ const deriveTrait = (trait$, baseClz, derived) => {
146
+ const trait$1 = rawTrait(trait$);
147
+ let clz = baseClz;
148
+ if (!derived.has(trait$1.id)) {
149
+ derived.set(trait$1.id, true);
150
+ if (trait$1.superTraits !== void 0) for (const superTrait of reverseTraitList(trait$1.superTraits)) clz = deriveTrait(superTrait, clz, derived);
151
+ clz = trait$1.factory(clz);
152
+ extendProperties(clz, "id", crc32(trait$1.factory.toString()));
153
+ extendProperties(clz, trait$1.symbol, true);
154
+ }
155
+ return clz;
156
+ };
157
+ const reverseTraitList = (traits) => traits.slice().reverse();
158
+ function use(...traits) {
159
+ if (traits.length === 0) throw new Error("invalid number of parameters (expected one or more traits)");
160
+ let clz;
161
+ let lot;
162
+ const last = traits[traits.length - 1];
163
+ if (isCons(last) && !isTypeFactory(last)) {
164
+ clz = last;
165
+ lot = traits.slice(0, -1);
166
+ } else {
167
+ clz = class ROOT {};
168
+ lot = traits;
169
+ }
170
+ const derived = /* @__PURE__ */ new Map();
171
+ for (const trait$1 of reverseTraitList(lot)) clz = deriveTrait(trait$1, clz, derived);
172
+ return clz;
173
+ }
174
+ function uses(instance, trait$1) {
175
+ if (typeof instance !== "object" || instance === null) return false;
176
+ let obj = instance;
177
+ if (isCons(trait$1) && !isTypeFactory(trait$1)) return instance instanceof trait$1;
178
+ const idTrait = (isTypeFactory(trait$1) ? trait$1() : trait$1)["id"];
179
+ while (obj) {
180
+ if (Object.hasOwn(obj, "constructor")) {
181
+ if ((obj.constructor["id"] ?? 0) === idTrait) return true;
100
182
  }
183
+ obj = Object.getPrototypeOf(obj);
101
184
  }
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;
185
+ return false;
186
+ }
187
+
188
+ //#endregion
189
+ //#region src/Mixins/UseFinalizable.ts
190
+ /**
191
+ * the central class instance registry
192
+ */
193
+ const registry = new FinalizationRegistry((fn) => {
194
+ if (typeof fn === "function" && !fn.finalized) {
195
+ fn.finalized = true;
196
+ fn();
197
+ }
198
+ });
199
+ /**
200
+ * the API trait "Finalizable<T>"
201
+ */
202
+ const Finalizable = trait((base) => class Finalizable$1 extends base {
203
+ constructor(...args) {
204
+ super(...args);
205
+ const fn1 = this.$finalize;
206
+ if (typeof fn1 !== "function") throw new Error("trait Finalizable requires a $finalize method to be defined");
207
+ const fn2 = () => {
208
+ fn1(this);
209
+ };
210
+ fn2.finalized = false;
211
+ registry.register(this, fn2, this);
212
+ }
213
+ });
214
+
215
+ //#endregion
216
+ //#region src/Mixins/UseMagic.ts
217
+ /**
218
+ * Wraps an object in a Proxy to emulate PHP magic methods.
219
+ *
220
+ * Supported:
221
+ * - __call(method, args)
222
+ * - __get(property)
223
+ * - __set(property, value)
224
+ * - __isset(property)
225
+ * - __unset(property)
226
+ *
227
+ * Called automatically by Magic's constructor.
228
+ *
229
+ * Return in any class constructor to use
230
+ *
231
+ * @param target
232
+ * @returns
233
+ */
234
+ function makeMagic(target) {
235
+ return new Proxy(target, {
236
+ get(obj, prop, receiver) {
237
+ if (typeof prop === "string") {
238
+ if (prop in obj) return Reflect.get(obj, prop, receiver);
239
+ if (obj.__call) return (...args) => obj.__call(prop, args);
240
+ if (obj.__get) return obj.__get(prop);
241
+ }
242
+ },
243
+ set(obj, prop, value) {
244
+ if (typeof prop === "string" && obj.__set) {
245
+ obj.__set(prop, value);
246
+ return true;
247
+ }
248
+ return Reflect.set(obj, prop, value);
249
+ },
250
+ has(obj, prop) {
251
+ if (typeof prop === "string" && obj.__isset) return obj.__isset(prop);
252
+ return Reflect.has(obj, prop);
253
+ },
254
+ deleteProperty(obj, prop) {
255
+ if (typeof prop === "string" && obj.__unset) {
256
+ obj.__unset(prop);
257
+ return true;
258
+ }
259
+ return Reflect.deleteProperty(obj, prop);
260
+ }
261
+ });
262
+ }
263
+ /**
264
+ * Wraps a class constructor in a Proxy to emulate static PHP magic methods.
265
+ *
266
+ * Supported:
267
+ * - __callStatic(method, args)
268
+ * - static __get(property)
269
+ * - static __set(property, value)
270
+ * - static __isset(property)
271
+ * - static __unset(property)
272
+ *
273
+ * @param cls
274
+ * @returns
275
+ */
276
+ function makeStaticMagic(cls) {
277
+ return new Proxy(cls, {
278
+ get(target, prop) {
279
+ if (typeof prop === "string") {
280
+ if (prop in target) return target[prop];
281
+ if (target.__callStatic) return (...args) => target.__callStatic(prop, args);
282
+ if (target.__get) return target.__get(prop);
283
+ }
284
+ },
285
+ set(target, prop, value) {
286
+ if (typeof prop === "string" && target.__set) {
287
+ target.__set(prop, value);
288
+ return true;
289
+ }
290
+ return Reflect.set(target, prop, value);
291
+ },
292
+ has(target, prop) {
293
+ if (typeof prop === "string" && target.__isset) return target.__isset(prop);
294
+ return Reflect.has(target, prop);
295
+ },
296
+ deleteProperty(target, prop) {
297
+ if (typeof prop === "string" && target.__unset) {
298
+ target.__unset(prop);
299
+ return true;
300
+ }
301
+ return Reflect.deleteProperty(target, prop);
302
+ }
303
+ });
304
+ }
305
+ /**
306
+ * Base class that enables PHP-style magic methods automatically.
307
+ *
308
+ * Any subclass may implement:
309
+ * - __call
310
+ * - __get
311
+ * - __set
312
+ * - __isset
313
+ * - __unset
314
+ *
315
+ * The constructor returns a Proxy transparently.
316
+ */
317
+ var Magic = class {
318
+ constructor() {
319
+ return makeMagic(this);
119
320
  }
120
321
  };
322
+ const UseMagic = trait((Base) => {
323
+ return class Magic$1 extends Base {
324
+ constructor(...args) {
325
+ super(...args);
326
+ return makeMagic(this);
327
+ }
328
+ };
329
+ });
121
330
 
122
331
  //#endregion
123
332
  //#region src/Utils/Logger.ts
@@ -195,6 +404,17 @@ var Logger = class Logger {
195
404
  * @returns
196
405
  */
197
406
  static textFormat(txt, color, preserveCol = false) {
407
+ if (txt instanceof Error) {
408
+ const err = txt;
409
+ const code = err.code ?? err.statusCode ? ` (${err.code ?? err.statusCode})` : "";
410
+ const output = [];
411
+ if (err.message) output.push(this.textFormat(`${err.constructor.name}${code}: ${err.message}`, chalk.default.bgRed, preserveCol));
412
+ if (err.stack) output.push(" " + chalk.default.white(err.stack.replace(`${err.name}: ${err.message}`, "").trim()));
413
+ return output.join("\n");
414
+ }
415
+ if (Array.isArray(txt)) return txt.map((e) => this.textFormat(e, color, preserveCol)).join("\n");
416
+ if (typeof txt === "object") return this.textFormat(Object.values(txt), color, preserveCol);
417
+ if (typeof txt !== "string") return color(txt);
198
418
  const str = String(txt);
199
419
  if (preserveCol) return str;
200
420
  const [first, ...rest] = str.split(":");
@@ -291,9 +511,129 @@ var Logger = class Logger {
291
511
  if (typeof config === "string") {
292
512
  const conf = [[config, joiner]];
293
513
  return this.parse(conf, "", log, sc);
294
- } else if (config) return this.parse(config, String(joiner), log, sc);
514
+ } else if (Array.isArray(config)) return this.parse(config, String(joiner), log, sc);
515
+ else if (log && !this.shouldSuppressOutput("line")) return console.log(this.textFormat(config, Logger.chalker(["blue"])));
295
516
  return this;
296
517
  });
518
+ /**
519
+ * A simple console like output logger
520
+ *
521
+ * @returns
522
+ */
523
+ static console() {
524
+ return Console;
525
+ }
526
+ };
527
+
528
+ //#endregion
529
+ //#region src/Utils/Console.ts
530
+ var Console = class {
531
+ static log = (...args) => Logger.log(args.map((e) => [e, "white"]));
532
+ static debug = (...args) => Logger.debug(args, false, true);
533
+ static warn = (...args) => args.map((e) => Logger.warn(e, false, true));
534
+ static info = (...args) => args.map((e) => Logger.info(e, false, true));
535
+ static error = (...args) => args.map((e) => Logger.error(e, false), true);
536
+ };
537
+
538
+ //#endregion
539
+ //#region src/Utils/EnvParser.ts
540
+ var EnvParser = class {
541
+ static parse(initial) {
542
+ const parsed = { ...initial };
543
+ for (const key in parsed) {
544
+ const value = parsed[key];
545
+ parsed[key] = this.parseValue(value);
546
+ }
547
+ return parsed;
548
+ }
549
+ static parseValue(value) {
550
+ /**
551
+ * Null/undefined stay untouched
552
+ */
553
+ if (value === null || value === void 0) return value;
554
+ /**
555
+ * Convert string "true"/"false" to boolean
556
+ */
557
+ if (value === "true") return true;
558
+ if (value === "false") return false;
559
+ /**
560
+ * Convert string numbers to number
561
+ */
562
+ if (!isNaN(value) && value.trim() !== "") return Number(value);
563
+ /**
564
+ * Convert string "null" and "undefined"
565
+ */
566
+ if (value === "null") return null;
567
+ if (value === "undefined") return void 0;
568
+ /**
569
+ * Otherwise return as-is (string)
570
+ */
571
+ return value;
572
+ }
573
+ };
574
+
575
+ //#endregion
576
+ //#region src/Utils/FileSystem.ts
577
+ var FileSystem = class {
578
+ static findModulePkg(moduleId, cwd) {
579
+ const parts = moduleId.replace(/\\/g, "/").split("/");
580
+ let packageName = "";
581
+ if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
582
+ packageName += parts.shift();
583
+ const packageJson = path.default.join(cwd ?? process.cwd(), "node_modules", packageName);
584
+ const resolved = this.resolveFileUp("package", ["json"], packageJson);
585
+ if (!resolved) return;
586
+ return path.default.join(path.default.dirname(resolved), parts.join("/"));
587
+ }
588
+ /**
589
+ * Check if file exists
590
+ *
591
+ * @param path
592
+ * @returns
593
+ */
594
+ static async fileExists(path$2) {
595
+ try {
596
+ await (0, fs_promises.access)(path$2);
597
+ return true;
598
+ } catch {
599
+ return false;
600
+ }
601
+ }
602
+ /**
603
+ * Recursively find files starting from given cwd
604
+ *
605
+ * @param name
606
+ * @param extensions
607
+ * @param cwd
608
+ *
609
+ * @returns
610
+ */
611
+ static resolveFileUp(name, extensions, cwd) {
612
+ cwd ??= process.cwd();
613
+ return (0, escalade_sync.default)(cwd, (dir, filesNames) => {
614
+ if (typeof extensions === "function") return extensions(dir, filesNames);
615
+ const candidates = new Set(extensions.map((ext) => `${name}.${ext}`));
616
+ for (const filename of filesNames) if (candidates.has(filename)) return filename;
617
+ return false;
618
+ }) ?? void 0;
619
+ }
620
+ /**
621
+ * Recursively find files starting from given cwd
622
+ *
623
+ * @param name
624
+ * @param extensions
625
+ * @param cwd
626
+ *
627
+ * @returns
628
+ */
629
+ static resolveModulePath(moduleId, pathName, cwd) {
630
+ pathName = Array.isArray(pathName) ? pathName : [pathName];
631
+ const module$1 = this.findModulePkg(moduleId, cwd) ?? "";
632
+ for (const name of pathName) {
633
+ const file = path.default.join(module$1, name);
634
+ if ((0, fs.existsSync)(file)) return file;
635
+ }
636
+ }
297
637
  };
298
638
 
299
639
  //#endregion
@@ -307,7 +647,8 @@ var PathLoader = class {
307
647
  config: "/src/config",
308
648
  public: "/public",
309
649
  storage: "/storage",
310
- database: "/src/database"
650
+ database: "/src/database",
651
+ commands: "/src/App/Console/Commands/"
311
652
  };
312
653
  /**
313
654
  * Dynamically retrieves a path property from the class.
@@ -336,6 +677,11 @@ var PathLoader = class {
336
677
  if (base && name !== "base") this.paths[name] = path.default.join(base, path$2);
337
678
  this.paths[name] = path$2;
338
679
  }
680
+ distPath(path$2, skipExt = false) {
681
+ path$2 = path$2.replace("/src/", `/${process.env.DIST_DIR ?? "src"}/`.replace(/([^:]\/)\/+/g, "$1"));
682
+ if (!skipExt) path$2 = path$2.replace(/\.(ts|tsx|mts|cts)$/, ".js");
683
+ return path.default.normalize(path$2);
684
+ }
339
685
  };
340
686
 
341
687
  //#endregion
@@ -455,7 +801,7 @@ const mainTsconfig = {
455
801
  },
456
802
  target: "es2022",
457
803
  module: "es2022",
458
- moduleResolution: "Node",
804
+ moduleResolution: "bundler",
459
805
  esModuleInterop: true,
460
806
  strict: true,
461
807
  allowJs: true,
@@ -525,14 +871,28 @@ var TaskManager = class {
525
871
  };
526
872
 
527
873
  //#endregion
874
+ exports.Console = Console;
528
875
  exports.EnvParser = EnvParser;
529
876
  exports.FileSystem = FileSystem;
877
+ exports.Finalizable = Finalizable;
878
+ exports.INTERNAL_METHODS = INTERNAL_METHODS;
530
879
  exports.Logger = Logger;
880
+ exports.Magic = Magic;
531
881
  exports.PathLoader = PathLoader;
532
882
  exports.Prompts = Prompts;
533
883
  exports.Resolver = Resolver;
534
884
  exports.TaskManager = TaskManager;
885
+ exports.UseMagic = UseMagic;
535
886
  exports.baseTsconfig = baseTsconfig;
887
+ exports.crc32 = crc32;
888
+ exports.internal = internal;
889
+ exports.isInternal = isInternal;
536
890
  exports.mainTsconfig = mainTsconfig;
891
+ exports.makeMagic = makeMagic;
892
+ exports.makeStaticMagic = makeStaticMagic;
893
+ exports.mix = mix;
537
894
  exports.packageJsonScript = packageJsonScript;
895
+ exports.trait = trait;
896
+ exports.use = use;
897
+ exports.uses = uses;
538
898
  //# sourceMappingURL=index.cjs.map