@h3ravel/core 1.14.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,34 +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;
269
297
  /**
270
298
  * Log an info message
271
299
  */
272
- info(message: string): void;
300
+ info(message: string): this;
273
301
  /**
274
302
  * Log a warning message
275
303
  */
276
- warn(message: string): void;
304
+ warn(message: string): this;
277
305
  /**
278
306
  * Log a line message
279
307
  */
280
- line(message: string): void;
308
+ line(message: string): this;
281
309
  /**
282
310
  * Log a new line
283
311
  */
284
- newLine(count?: number): void;
312
+ newLine(count?: number): this;
285
313
  /**
286
314
  * Log a success message
287
315
  */
288
- success(message: string): void;
316
+ success(message: string): this;
289
317
  /**
290
318
  * Log an error message
291
319
  */
292
- error(message: string): void;
320
+ error(message: string): this;
293
321
  /**
294
322
  * Log a debug message
295
323
  */
296
- debug(message: string | string[]): void;
324
+ debug(message: string | string[]): this;
297
325
  }
298
326
  //#endregion
299
327
  //#region src/Contracts/ServiceProviderConstructor.d.ts
@@ -380,6 +408,13 @@ declare class ProviderRegistry {
380
408
  private static providers;
381
409
  private static priorityMap;
382
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;
383
418
  /**
384
419
  * Get a unique identifier for the Provider.
385
420
  *
@@ -422,6 +457,10 @@ declare class ProviderRegistry {
422
457
  * @returns
423
458
  */
424
459
  static sort(providers: ProviderCtor[]): ProviderCtor[];
460
+ /**
461
+ * Sort service providers
462
+ */
463
+ static doSort(): void;
425
464
  /**
426
465
  * Log the service providers in a table
427
466
  *
@@ -441,6 +480,20 @@ declare class ProviderRegistry {
441
480
  * @returns
442
481
  */
443
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;
444
497
  }
445
498
  //#endregion
446
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,34 +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;
270
298
  /**
271
299
  * Log an info message
272
300
  */
273
- info(message: string): void;
301
+ info(message: string): this;
274
302
  /**
275
303
  * Log a warning message
276
304
  */
277
- warn(message: string): void;
305
+ warn(message: string): this;
278
306
  /**
279
307
  * Log a line message
280
308
  */
281
- line(message: string): void;
309
+ line(message: string): this;
282
310
  /**
283
311
  * Log a new line
284
312
  */
285
- newLine(count?: number): void;
313
+ newLine(count?: number): this;
286
314
  /**
287
315
  * Log a success message
288
316
  */
289
- success(message: string): void;
317
+ success(message: string): this;
290
318
  /**
291
319
  * Log an error message
292
320
  */
293
- error(message: string): void;
321
+ error(message: string): this;
294
322
  /**
295
323
  * Log a debug message
296
324
  */
297
- debug(message: string | string[]): void;
325
+ debug(message: string | string[]): this;
298
326
  }
299
327
  //#endregion
300
328
  //#region src/Contracts/ServiceProviderConstructor.d.ts
@@ -381,6 +409,13 @@ declare class ProviderRegistry {
381
409
  private static providers;
382
410
  private static priorityMap;
383
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;
384
419
  /**
385
420
  * Get a unique identifier for the Provider.
386
421
  *
@@ -423,6 +458,10 @@ declare class ProviderRegistry {
423
458
  * @returns
424
459
  */
425
460
  static sort(providers: ProviderCtor[]): ProviderCtor[];
461
+ /**
462
+ * Sort service providers
463
+ */
464
+ static doSort(): void;
426
465
  /**
427
466
  * Log the service providers in a table
428
467
  *
@@ -442,6 +481,20 @@ declare class ProviderRegistry {
442
481
  * @returns
443
482
  */
444
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;
445
498
  }
446
499
  //#endregion
447
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,52 +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"));
578
685
  }
579
686
  /**
580
687
  * Log an info message
581
688
  */
582
689
  info(message) {
583
690
  Logger.info(message);
691
+ return this;
584
692
  }
585
693
  /**
586
694
  * Log a warning message
587
695
  */
588
696
  warn(message) {
589
697
  Logger.warn(message);
698
+ return this;
590
699
  }
591
700
  /**
592
701
  * Log a line message
593
702
  */
594
703
  line(message) {
595
704
  Logger.log(message, "white");
705
+ return this;
596
706
  }
597
707
  /**
598
708
  * Log a new line
599
709
  */
600
710
  newLine(count = 1) {
601
- for (let i = 0; i < count; i++) console.log("");
711
+ if (Number(this.getVerbosity()) >= 3 || !this.isSilent() && !this.isQuiet()) for (let i = 0; i < count; i++) console.log("");
712
+ return this;
602
713
  }
603
714
  /**
604
715
  * Log a success message
605
716
  */
606
717
  success(message) {
607
718
  Logger.success(message);
719
+ return this;
608
720
  }
609
721
  /**
610
722
  * Log an error message
611
723
  */
612
724
  error(message) {
613
725
  Logger.error(message);
726
+ return this;
614
727
  }
615
728
  /**
616
729
  * Log a debug message
617
730
  */
618
731
  debug(message) {
619
732
  Logger.debug(message);
733
+ return this;
620
734
  }
621
735
  };
622
736