@h3ravel/core 1.13.0 → 1.15.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.d.cts CHANGED
@@ -24,7 +24,7 @@ declare class Container implements IContainer {
24
24
  /**
25
25
  * Bind a singleton service to the container
26
26
  */
27
- singleton<T extends UseKey>(key: T | (new (..._args: any[]) => Bindings[T]), factory: () => Bindings[T]): void;
27
+ singleton<T extends UseKey>(key: T | (new (..._args: any[]) => Bindings[T]), factory: (app: this) => Bindings[T]): void;
28
28
  /**
29
29
  * Resolve a service from the container
30
30
  */
@@ -128,13 +128,17 @@ declare class Application extends Container implements IApplication {
128
128
  *
129
129
  * @param providers All regitererable service providers
130
130
  * @param filtered A list of service provider name strings we do not want to register at all cost
131
+ * @param autoRegisterProviders If set to false, service providers will not be auto discovered and registered.
132
+ *
131
133
  * @returns
132
134
  */
133
- quickStartup(providers: Array<AServiceProvider>, filtered?: string[]): Promise<void>;
135
+ quickStartup(providers: Array<AServiceProvider>, filtered?: string[], autoRegisterProviders?: boolean): Promise<void>;
134
136
  /**
135
137
  * Dynamically register all configured providers
138
+ *
139
+ * @param autoRegister If set to false, service providers will not be auto discovered and registered.
136
140
  */
137
- registerConfiguredProviders(): Promise<void>;
141
+ registerConfiguredProviders(autoRegister?: boolean): Promise<void>;
138
142
  /**
139
143
  * Register service providers
140
144
  *
@@ -266,6 +270,58 @@ declare class ConsoleCommand {
266
270
  argument(key: string, def?: any): any;
267
271
  arguments(): Record<string, any>;
268
272
  loadBaseFlags(): void;
273
+ /**
274
+ * Check if the command is quiet
275
+ *
276
+ * @returns
277
+ */
278
+ isQuiet(): any;
279
+ /**
280
+ * Check if the command is silent
281
+ *
282
+ * @returns
283
+ */
284
+ isSilent(): any;
285
+ /**
286
+ * Check if the command is non interactive
287
+ *
288
+ * @returns
289
+ */
290
+ isNonInteractive(): boolean;
291
+ /**
292
+ * Get the verbosity of the command
293
+ *
294
+ * @returns
295
+ */
296
+ getVerbosity(): number;
297
+ /**
298
+ * Log an info message
299
+ */
300
+ info(message: string): this;
301
+ /**
302
+ * Log a warning message
303
+ */
304
+ warn(message: string): this;
305
+ /**
306
+ * Log a line message
307
+ */
308
+ line(message: string): this;
309
+ /**
310
+ * Log a new line
311
+ */
312
+ newLine(count?: number): this;
313
+ /**
314
+ * Log a success message
315
+ */
316
+ success(message: string): this;
317
+ /**
318
+ * Log an error message
319
+ */
320
+ error(message: string): this;
321
+ /**
322
+ * Log a debug message
323
+ */
324
+ debug(message: string | string[]): this;
269
325
  }
270
326
  //#endregion
271
327
  //#region src/Contracts/ServiceProviderConstructor.d.ts
@@ -352,6 +408,13 @@ declare class ProviderRegistry {
352
408
  private static providers;
353
409
  private static priorityMap;
354
410
  private static filteredProviders;
411
+ private static sortable;
412
+ /**
413
+ * Set wether providers should be sorted or not.
414
+ *
415
+ * @returns
416
+ */
417
+ static setSortable(sort?: boolean): void;
355
418
  /**
356
419
  * Get a unique identifier for the Provider.
357
420
  *
@@ -394,6 +457,10 @@ declare class ProviderRegistry {
394
457
  * @returns
395
458
  */
396
459
  static sort(providers: ProviderCtor[]): ProviderCtor[];
460
+ /**
461
+ * Sort service providers
462
+ */
463
+ static doSort(): void;
397
464
  /**
398
465
  * Log the service providers in a table
399
466
  *
@@ -413,6 +480,20 @@ declare class ProviderRegistry {
413
480
  * @returns
414
481
  */
415
482
  static has(provider: ProviderCtor): boolean;
483
+ /**
484
+ * Automatically search for and discover service providers in packages.
485
+ *
486
+ * @param autoRegister
487
+ * @returns
488
+ */
489
+ static discoverProviders(autoRegister?: boolean): Promise<ProviderCtor[]>;
490
+ /**
491
+ * Get the content of the package.json file
492
+ *
493
+ * @param manifestPath
494
+ * @returns
495
+ */
496
+ private static getManifest;
416
497
  }
417
498
  //#endregion
418
499
  //#region src/Providers/CoreServiceProvider.d.ts
package/dist/index.d.ts CHANGED
@@ -25,7 +25,7 @@ declare class Container implements IContainer {
25
25
  /**
26
26
  * Bind a singleton service to the container
27
27
  */
28
- singleton<T extends UseKey>(key: T | (new (..._args: any[]) => Bindings[T]), factory: () => Bindings[T]): void;
28
+ singleton<T extends UseKey>(key: T | (new (..._args: any[]) => Bindings[T]), factory: (app: this) => Bindings[T]): void;
29
29
  /**
30
30
  * Resolve a service from the container
31
31
  */
@@ -129,13 +129,17 @@ declare class Application extends Container implements IApplication {
129
129
  *
130
130
  * @param providers All regitererable service providers
131
131
  * @param filtered A list of service provider name strings we do not want to register at all cost
132
+ * @param autoRegisterProviders If set to false, service providers will not be auto discovered and registered.
133
+ *
132
134
  * @returns
133
135
  */
134
- quickStartup(providers: Array<AServiceProvider>, filtered?: string[]): Promise<void>;
136
+ quickStartup(providers: Array<AServiceProvider>, filtered?: string[], autoRegisterProviders?: boolean): Promise<void>;
135
137
  /**
136
138
  * Dynamically register all configured providers
139
+ *
140
+ * @param autoRegister If set to false, service providers will not be auto discovered and registered.
137
141
  */
138
- registerConfiguredProviders(): Promise<void>;
142
+ registerConfiguredProviders(autoRegister?: boolean): Promise<void>;
139
143
  /**
140
144
  * Register service providers
141
145
  *
@@ -267,6 +271,58 @@ declare class ConsoleCommand {
267
271
  argument(key: string, def?: any): any;
268
272
  arguments(): Record<string, any>;
269
273
  loadBaseFlags(): void;
274
+ /**
275
+ * Check if the command is quiet
276
+ *
277
+ * @returns
278
+ */
279
+ isQuiet(): any;
280
+ /**
281
+ * Check if the command is silent
282
+ *
283
+ * @returns
284
+ */
285
+ isSilent(): any;
286
+ /**
287
+ * Check if the command is non interactive
288
+ *
289
+ * @returns
290
+ */
291
+ isNonInteractive(): boolean;
292
+ /**
293
+ * Get the verbosity of the command
294
+ *
295
+ * @returns
296
+ */
297
+ getVerbosity(): number;
298
+ /**
299
+ * Log an info message
300
+ */
301
+ info(message: string): this;
302
+ /**
303
+ * Log a warning message
304
+ */
305
+ warn(message: string): this;
306
+ /**
307
+ * Log a line message
308
+ */
309
+ line(message: string): this;
310
+ /**
311
+ * Log a new line
312
+ */
313
+ newLine(count?: number): this;
314
+ /**
315
+ * Log a success message
316
+ */
317
+ success(message: string): this;
318
+ /**
319
+ * Log an error message
320
+ */
321
+ error(message: string): this;
322
+ /**
323
+ * Log a debug message
324
+ */
325
+ debug(message: string | string[]): this;
270
326
  }
271
327
  //#endregion
272
328
  //#region src/Contracts/ServiceProviderConstructor.d.ts
@@ -353,6 +409,13 @@ declare class ProviderRegistry {
353
409
  private static providers;
354
410
  private static priorityMap;
355
411
  private static filteredProviders;
412
+ private static sortable;
413
+ /**
414
+ * Set wether providers should be sorted or not.
415
+ *
416
+ * @returns
417
+ */
418
+ static setSortable(sort?: boolean): void;
356
419
  /**
357
420
  * Get a unique identifier for the Provider.
358
421
  *
@@ -395,6 +458,10 @@ declare class ProviderRegistry {
395
458
  * @returns
396
459
  */
397
460
  static sort(providers: ProviderCtor[]): ProviderCtor[];
461
+ /**
462
+ * Sort service providers
463
+ */
464
+ static doSort(): void;
398
465
  /**
399
466
  * Log the service providers in a table
400
467
  *
@@ -414,6 +481,20 @@ declare class ProviderRegistry {
414
481
  * @returns
415
482
  */
416
483
  static has(provider: ProviderCtor): boolean;
484
+ /**
485
+ * Automatically search for and discover service providers in packages.
486
+ *
487
+ * @param autoRegister
488
+ * @returns
489
+ */
490
+ static discoverProviders(autoRegister?: boolean): Promise<ProviderCtor[]>;
491
+ /**
492
+ * Get the content of the package.json file
493
+ *
494
+ * @param manifestPath
495
+ * @returns
496
+ */
497
+ private static getManifest;
417
498
  }
418
499
  //#endregion
419
500
  //#region src/Providers/CoreServiceProvider.d.ts
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import "reflect-metadata";
2
2
  import { FileSystem, Logger, PathLoader } from "@h3ravel/shared";
3
- import { Str, dd, dump, str } from "@h3ravel/support";
3
+ import fg from "fast-glob";
4
4
  import path from "node:path";
5
+ import { Str, dd, dump, str } from "@h3ravel/support";
5
6
  import { detect } from "detect-port";
6
7
  import dotenv from "dotenv";
7
8
  import dotenvExpand from "dotenv-expand";
@@ -26,7 +27,7 @@ var Container = class {
26
27
  */
27
28
  singleton(key, factory) {
28
29
  this.bindings.set(key, () => {
29
- if (!this.singletons.has(key)) this.singletons.set(key, factory());
30
+ if (!this.singletons.has(key)) this.singletons.set(key, factory(this));
30
31
  return this.singletons.get(key);
31
32
  });
32
33
  }
@@ -96,6 +97,15 @@ var ProviderRegistry = class {
96
97
  static providers = /* @__PURE__ */ new Map();
97
98
  static priorityMap = /* @__PURE__ */ new Map();
98
99
  static filteredProviders = [];
100
+ static sortable = true;
101
+ /**
102
+ * Set wether providers should be sorted or not.
103
+ *
104
+ * @returns
105
+ */
106
+ static setSortable(sort = true) {
107
+ this.sortable = sort;
108
+ }
99
109
  /**
100
110
  * Get a unique identifier for the Provider.
101
111
  *
@@ -116,7 +126,8 @@ var ProviderRegistry = class {
116
126
  * @returns
117
127
  */
118
128
  static register(...providers) {
119
- for (const provider of this.sort(providers.concat(...this.providers.values()))) {
129
+ const list = this.sortable ? this.sort(providers.concat(...this.providers.values())) : providers.concat(...this.providers.values());
130
+ for (const provider of list) {
120
131
  const key = this.getKey(provider);
121
132
  this.providers.set(key, provider);
122
133
  }
@@ -128,7 +139,8 @@ var ProviderRegistry = class {
128
139
  * @returns
129
140
  */
130
141
  static registerMany(providers) {
131
- for (const provider of this.sort(providers.concat(...this.providers.values()))) {
142
+ const list = this.sortable ? this.sort(providers.concat(...this.providers.values())) : providers.concat(...this.providers.values());
143
+ for (const provider of list) {
132
144
  const key = this.getKey(provider);
133
145
  this.providers.set(key, provider);
134
146
  }
@@ -194,6 +206,18 @@ var ProviderRegistry = class {
194
206
  });
195
207
  }
196
208
  /**
209
+ * Sort service providers
210
+ */
211
+ static doSort() {
212
+ const raw = this.sort(Array.from(this.providers.values()));
213
+ const providers = /* @__PURE__ */ new Map();
214
+ for (const provider of raw) {
215
+ const key = this.getKey(provider);
216
+ providers.set(key, provider);
217
+ }
218
+ this.providers = providers;
219
+ }
220
+ /**
197
221
  * Log the service providers in a table
198
222
  *
199
223
  * @param priorityMap
@@ -224,6 +248,45 @@ var ProviderRegistry = class {
224
248
  static has(provider) {
225
249
  return this.providers.has(this.getKey(provider));
226
250
  }
251
+ /**
252
+ * Automatically search for and discover service providers in packages.
253
+ *
254
+ * @param autoRegister
255
+ * @returns
256
+ */
257
+ static async discoverProviders(autoRegister = true) {
258
+ const manifests = await fg([
259
+ "node_modules/@h3ravel/*/package.json",
260
+ "node_modules/@h3ravel-community/*/package.json",
261
+ "node_modules/h3ravel-*/package.json"
262
+ ]);
263
+ const providers = [];
264
+ for (const manifestPath of manifests) {
265
+ const pkg = await this.getManifest(path.resolve(manifestPath));
266
+ if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await import(path.resolve(path.dirname(manifestPath), "dist/index.js")))[name])));
267
+ }
268
+ if (autoRegister) for (const provider of providers) {
269
+ const key = this.getKey(provider);
270
+ this.providers.set(key, provider);
271
+ }
272
+ return providers;
273
+ }
274
+ /**
275
+ * Get the content of the package.json file
276
+ *
277
+ * @param manifestPath
278
+ * @returns
279
+ */
280
+ static async getManifest(manifestPath) {
281
+ let pkg;
282
+ try {
283
+ pkg = (await import(manifestPath)).default;
284
+ } catch {
285
+ const { createRequire } = await import("module");
286
+ pkg = createRequire(import.meta.url)(manifestPath);
287
+ }
288
+ return pkg;
289
+ }
227
290
  };
228
291
 
229
292
  //#endregion
@@ -345,20 +408,27 @@ var Application = class Application extends Container {
345
408
  *
346
409
  * @param providers All regitererable service providers
347
410
  * @param filtered A list of service provider name strings we do not want to register at all cost
411
+ * @param autoRegisterProviders If set to false, service providers will not be auto discovered and registered.
412
+ *
348
413
  * @returns
349
414
  */
350
- async quickStartup(providers, filtered = []) {
415
+ async quickStartup(providers, filtered = [], autoRegisterProviders = true) {
351
416
  this.registerProviders(providers, filtered);
352
- await this.registerConfiguredProviders();
417
+ await this.registerConfiguredProviders(autoRegisterProviders);
353
418
  return this.boot();
354
419
  }
355
420
  /**
356
421
  * Dynamically register all configured providers
422
+ *
423
+ * @param autoRegister If set to false, service providers will not be auto discovered and registered.
357
424
  */
358
- async registerConfiguredProviders() {
425
+ async registerConfiguredProviders(autoRegister = true) {
359
426
  const providers = await this.getAllProviders();
427
+ ProviderRegistry.setSortable(false);
360
428
  ProviderRegistry.setFiltered(this.filteredProviders);
361
429
  ProviderRegistry.registerMany(providers);
430
+ if (autoRegister) await ProviderRegistry.discoverProviders();
431
+ ProviderRegistry.doSort();
362
432
  for (const ProviderClass of ProviderRegistry.all()) {
363
433
  if (!ProviderClass) continue;
364
434
  const provider = new ProviderClass(this);
@@ -549,6 +619,11 @@ var ConsoleCommand = class {
549
619
  this.input.options = options;
550
620
  this.input.arguments = regArgs.map((e, i) => ({ [e.name()]: args[i] })).reduce((e, x) => Object.assign(e, x), {});
551
621
  this.loadBaseFlags();
622
+ Logger.configure({
623
+ verbosity: this.option("verbose"),
624
+ silent: this.option("silent"),
625
+ quiet: this.option("quiet")
626
+ });
552
627
  }
553
628
  getSignature() {
554
629
  return this.signature;
@@ -571,10 +646,91 @@ var ConsoleCommand = class {
571
646
  return this.input.arguments;
572
647
  }
573
648
  loadBaseFlags() {
574
- this.input.options.lock = this.program.getOptionValue("lock") ?? false;
575
649
  this.input.options.quiet = this.program.getOptionValue("quiet") ?? false;
576
650
  this.input.options.silent = this.program.getOptionValue("silent") ?? false;
577
- this.input.options.verbose = this.program.getOptionValue("verbose") ?? 0;
651
+ this.input.options.verbose = Number(this.program.getOptionValue("verbose") ?? 0);
652
+ this.input.options.interaction = this.program.getOptionValue("interaction") ?? false;
653
+ }
654
+ /**
655
+ * Check if the command is quiet
656
+ *
657
+ * @returns
658
+ */
659
+ isQuiet() {
660
+ return this.option("quiet");
661
+ }
662
+ /**
663
+ * Check if the command is silent
664
+ *
665
+ * @returns
666
+ */
667
+ isSilent() {
668
+ return this.option("silent");
669
+ }
670
+ /**
671
+ * Check if the command is non interactive
672
+ *
673
+ * @returns
674
+ */
675
+ isNonInteractive() {
676
+ return this.option("interaction") === false;
677
+ }
678
+ /**
679
+ * Get the verbosity of the command
680
+ *
681
+ * @returns
682
+ */
683
+ getVerbosity() {
684
+ return Number(this.option("verbose"));
685
+ }
686
+ /**
687
+ * Log an info message
688
+ */
689
+ info(message) {
690
+ Logger.info(message);
691
+ return this;
692
+ }
693
+ /**
694
+ * Log a warning message
695
+ */
696
+ warn(message) {
697
+ Logger.warn(message);
698
+ return this;
699
+ }
700
+ /**
701
+ * Log a line message
702
+ */
703
+ line(message) {
704
+ Logger.log(message, "white");
705
+ return this;
706
+ }
707
+ /**
708
+ * Log a new line
709
+ */
710
+ newLine(count = 1) {
711
+ if (Number(this.getVerbosity()) >= 3 || !this.isSilent() && !this.isQuiet()) for (let i = 0; i < count; i++) console.log("");
712
+ return this;
713
+ }
714
+ /**
715
+ * Log a success message
716
+ */
717
+ success(message) {
718
+ Logger.success(message);
719
+ return this;
720
+ }
721
+ /**
722
+ * Log an error message
723
+ */
724
+ error(message) {
725
+ Logger.error(message);
726
+ return this;
727
+ }
728
+ /**
729
+ * Log a debug message
730
+ */
731
+ debug(message) {
732
+ Logger.debug(message);
733
+ return this;
578
734
  }
579
735
  };
580
736