@kubb/core 1.1.4 → 1.1.6

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
@@ -523,6 +523,13 @@ var FileManager = class {
523
523
  });
524
524
  return files;
525
525
  }
526
+ get cachedFiles() {
527
+ const files = [];
528
+ this.cache.forEach((item) => {
529
+ files.push(item);
530
+ });
531
+ return files;
532
+ }
526
533
  async add(file) {
527
534
  const cacheItem = { id: crypto__default.default.randomUUID(), file, status: "new" };
528
535
  this.cache.set(cacheItem.id, cacheItem);
@@ -627,7 +634,7 @@ var PluginManager = class {
627
634
  return this.hookFirstSync({
628
635
  hookName: "resolvePath",
629
636
  parameters: [params.fileName, params.directory, params.options]
630
- });
637
+ }).result;
631
638
  };
632
639
  resolveName = (params) => {
633
640
  if (params.pluginName) {
@@ -640,7 +647,7 @@ var PluginManager = class {
640
647
  return this.hookFirstSync({
641
648
  hookName: "resolveName",
642
649
  parameters: [params.name]
643
- });
650
+ }).result;
644
651
  };
645
652
  load = async (id) => {
646
653
  return this.hookFirst({
@@ -691,15 +698,20 @@ var PluginManager = class {
691
698
  for (const plugin of this.getSortedPlugins(hookName)) {
692
699
  if (skipped && skipped.has(plugin))
693
700
  continue;
694
- promise = promise.then((result) => {
695
- if (result != null)
696
- return result;
697
- return this.execute({
701
+ promise = promise.then(async (parseResult) => {
702
+ if (parseResult?.result != null) {
703
+ return parseResult;
704
+ }
705
+ const value = await this.execute({
698
706
  strategy: "hookFirst",
699
707
  hookName,
700
708
  parameters,
701
709
  plugin
702
710
  });
711
+ return Promise.resolve({
712
+ plugin,
713
+ result: value
714
+ });
703
715
  });
704
716
  }
705
717
  return promise;
@@ -713,43 +725,38 @@ var PluginManager = class {
713
725
  parameters,
714
726
  skipped
715
727
  }) {
716
- let result = null;
728
+ let parseResult = null;
717
729
  for (const plugin of this.getSortedPlugins(hookName)) {
718
730
  if (skipped && skipped.has(plugin))
719
731
  continue;
720
- result = this.executeSync({
721
- strategy: "hookFirst",
722
- hookName,
723
- parameters,
732
+ parseResult = {
733
+ result: this.executeSync({
734
+ strategy: "hookFirst",
735
+ hookName,
736
+ parameters,
737
+ plugin
738
+ }),
724
739
  plugin
725
- });
726
- if (result != null) {
740
+ };
741
+ if (parseResult?.result != null) {
727
742
  break;
728
743
  }
729
744
  }
730
- return result;
745
+ return parseResult;
731
746
  }
732
- // parallel
747
+ /**
748
+ *
749
+ * Parallel, runs all plugins
750
+ */
733
751
  async hookParallel({
734
752
  hookName,
735
753
  parameters
736
754
  }) {
737
755
  const parallelPromises = [];
738
756
  for (const plugin of this.getSortedPlugins(hookName)) {
739
- if (plugin[hookName]?.sequential) {
740
- await Promise.all(parallelPromises);
741
- parallelPromises.length = 0;
742
- await this.execute({
743
- strategy: "hookParallel",
744
- hookName,
745
- parameters,
746
- plugin
747
- });
748
- } else {
749
- const promise = this.execute({ strategy: "hookParallel", hookName, parameters, plugin });
750
- if (promise) {
751
- parallelPromises.push(promise);
752
- }
757
+ const promise = this.execute({ strategy: "hookParallel", hookName, parameters, plugin });
758
+ if (promise) {
759
+ parallelPromises.push(promise);
753
760
  }
754
761
  }
755
762
  const results = await Promise.allSettled(parallelPromises);
@@ -759,7 +766,10 @@ var PluginManager = class {
759
766
  }
760
767
  return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
761
768
  }
762
- // chains, reduces returned value, handling the reduced value as the first hook argument
769
+ /**
770
+ *
771
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
772
+ */
763
773
  hookReduceArg0({
764
774
  hookName,
765
775
  parameters,
@@ -768,11 +778,11 @@ var PluginManager = class {
768
778
  const [argument0, ...rest] = parameters;
769
779
  let promise = Promise.resolve(argument0);
770
780
  for (const plugin of this.getSortedPlugins(hookName)) {
771
- promise = promise.then((argument02) => {
781
+ promise = promise.then((arg0) => {
772
782
  const value = this.execute({
773
783
  strategy: "hookReduceArg0",
774
784
  hookName,
775
- parameters: [argument02, ...rest],
785
+ parameters: [arg0, ...rest],
776
786
  plugin
777
787
  });
778
788
  return value;
@@ -780,7 +790,9 @@ var PluginManager = class {
780
790
  }
781
791
  return promise;
782
792
  }
783
- // chains
793
+ /**
794
+ * Chains plugins
795
+ */
784
796
  hookSeq({ hookName, parameters }) {
785
797
  let promise = Promise.resolve();
786
798
  for (const plugin of this.getSortedPlugins(hookName)) {
@@ -796,7 +808,7 @@ var PluginManager = class {
796
808
  return promise.then(noReturn);
797
809
  }
798
810
  getSortedPlugins(_hookName) {
799
- const plugins = [...this.plugins];
811
+ const plugins = [...this.plugins].filter((plugin) => plugin.name !== "core");
800
812
  return plugins;
801
813
  }
802
814
  getPlugin(hookName, pluginName) {
@@ -1060,9 +1072,6 @@ ${source}`;
1060
1072
 
1061
1073
  // src/build.ts
1062
1074
  async function transformReducer(_previousCode, result, _plugin) {
1063
- if (result === null) {
1064
- return null;
1065
- }
1066
1075
  return result;
1067
1076
  }
1068
1077
  async function build(options) {
@@ -1080,11 +1089,14 @@ async function build(options) {
1080
1089
  const queueTask = async (id, file) => {
1081
1090
  const { path } = file;
1082
1091
  let code = getFileSource(file);
1083
- const loadedResult = await pluginManager.hookFirst({
1092
+ const { result: loadedResult } = await pluginManager.hookFirst({
1084
1093
  hookName: "load",
1085
1094
  parameters: [path]
1086
1095
  });
1087
- if (loadedResult) {
1096
+ if (loadedResult && isPromise(loadedResult)) {
1097
+ code = await loadedResult;
1098
+ }
1099
+ if (loadedResult && !isPromise(loadedResult)) {
1088
1100
  code = loadedResult;
1089
1101
  }
1090
1102
  if (code) {
package/dist/index.d.ts CHANGED
@@ -124,11 +124,12 @@ declare class FileManager {
124
124
  private queue?;
125
125
  constructor(options?: {
126
126
  queue: Queue;
127
- task: QueueTask<unknown>;
127
+ task?: QueueTask<unknown>;
128
128
  });
129
129
  private getCache;
130
130
  getCacheByPath(path: string | undefined): CacheStore | undefined;
131
131
  get files(): File[];
132
+ get cachedFiles(): CacheStore[];
132
133
  add(file: File): Promise<File>;
133
134
  addOrAppend(file: File): Promise<File>;
134
135
  setStatus(id: UUID, status: Status): void;
@@ -154,22 +155,14 @@ type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
154
155
  plugin: KubbPlugin;
155
156
  };
156
157
  type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (this: PluginManager, executer: Executer<H> | undefined) => void;
157
-
158
- type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => T['nested'] extends true ? Array<KubbPlugin<T>> : KubbPlugin<T>;
159
- declare function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: KubbPluginFactory<T>): (options: T['options']) => T["nested"] extends true ? KubbPlugin<T>[] : KubbPlugin<T>;
160
- type Options$1 = {
161
- config: PluginContext['config'];
162
- fileManager: FileManager;
163
- resolvePath: PluginContext['resolvePath'];
164
- resolveName: PluginContext['resolveName'];
165
- load: PluginContext['load'];
166
- getExecuter: () => Executer<PluginLifecycleHooks> | undefined;
158
+ type ParseResult<H extends PluginLifecycleHooks> = PluginLifecycle[H];
159
+ type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseResult<H>>> = {
160
+ result: Result;
161
+ plugin: KubbPlugin;
167
162
  };
168
- type CorePluginOptions = PluginFactoryOptions<Options$1, false, PluginContext>;
169
- declare const name: "core";
170
163
 
171
164
  declare const hooks: [keyof PluginLifecycle<PluginFactoryOptions<unknown, false, any, Record<string, any>>>];
172
- type Options = {
165
+ type Options$1 = {
173
166
  task: QueueTask;
174
167
  onExecute?: OnExecute<PluginLifecycleHooks>;
175
168
  };
@@ -177,15 +170,15 @@ declare class PluginManager {
177
170
  plugins: KubbPlugin[];
178
171
  readonly fileManager: FileManager;
179
172
  private readonly onExecute?;
180
- readonly core: KubbPlugin<CorePluginOptions>;
173
+ private readonly core;
181
174
  queue: Queue;
182
175
  executer: Executer | undefined;
183
176
  executed: Executer[];
184
- constructor(config: KubbConfig, options: Options);
177
+ constructor(config: KubbConfig, options: Options$1);
185
178
  getExecuter(): Executer | undefined;
186
179
  resolvePath: (params: ResolvePathParams) => OptionalPath;
187
180
  resolveName: (params: ResolveNameParams) => string | null;
188
- load: (id: string) => Promise<TransformResult>;
181
+ load: (id: string) => Promise<SafeParseResult<"load">>;
189
182
  /**
190
183
  *
191
184
  * Run only hook for a specific plugin name
@@ -194,12 +187,12 @@ declare class PluginManager {
194
187
  pluginName: string;
195
188
  hookName: H;
196
189
  parameters: Parameters<PluginLifecycle[H]>;
197
- }): Promise<ReturnType<PluginLifecycle[H]> | null> | null;
190
+ }): Promise<ReturnType<ParseResult<H>> | null> | null;
198
191
  hookForPluginSync<H extends PluginLifecycleHooks>({ pluginName, hookName, parameters, }: {
199
192
  pluginName: string;
200
193
  hookName: H;
201
194
  parameters: Parameters<PluginLifecycle[H]>;
202
- }): ReturnType<PluginLifecycle[H]> | null;
195
+ }): ReturnType<ParseResult<H>> | null;
203
196
  /**
204
197
  *
205
198
  * Chains, first non-null result stops and returns
@@ -208,7 +201,7 @@ declare class PluginManager {
208
201
  hookName: H;
209
202
  parameters: Parameters<PluginLifecycle[H]>;
210
203
  skipped?: ReadonlySet<KubbPlugin> | null;
211
- }): Promise<ReturnType<PluginLifecycle[H]>>;
204
+ }): Promise<SafeParseResult<H>>;
212
205
  /**
213
206
  *
214
207
  * Chains, first non-null result stops and returns
@@ -217,22 +210,33 @@ declare class PluginManager {
217
210
  hookName: H;
218
211
  parameters: Parameters<PluginLifecycle[H]>;
219
212
  skipped?: ReadonlySet<KubbPlugin> | null;
220
- }): ReturnType<PluginLifecycle[H]>;
213
+ }): SafeParseResult<H>;
214
+ /**
215
+ *
216
+ * Parallel, runs all plugins
217
+ */
221
218
  hookParallel<H extends PluginLifecycleHooks, TOuput = void>({ hookName, parameters, }: {
222
219
  hookName: H;
223
220
  parameters?: Parameters<PluginLifecycle[H]> | undefined;
224
221
  }): Promise<Awaited<TOuput>[]>;
222
+ /**
223
+ *
224
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
225
+ */
225
226
  hookReduceArg0<H extends PluginLifecycleHooks>({ hookName, parameters, reduce, }: {
226
227
  hookName: H;
227
228
  parameters: Parameters<PluginLifecycle[H]>;
228
- reduce: (reduction: Argument0<H>, result: ReturnType<PluginLifecycle[H]>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>;
229
+ reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>;
229
230
  }): Promise<Argument0<H>>;
231
+ /**
232
+ * Chains plugins
233
+ */
230
234
  hookSeq<H extends PluginLifecycleHooks>({ hookName, parameters }: {
231
235
  hookName: H;
232
236
  parameters?: Parameters<PluginLifecycle[H]>;
233
237
  }): Promise<void>;
234
238
  private getSortedPlugins;
235
- private getPlugin;
239
+ getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin;
236
240
  private addExecuter;
237
241
  /**
238
242
  * Run an async plugin hook and return the result.
@@ -490,7 +494,7 @@ type PluginContext<TOptions = Record<string, any>> = {
490
494
  addFile: (...file: File[]) => Promise<File[]>;
491
495
  resolvePath: (params: ResolvePathParams<TOptions>) => OptionalPath;
492
496
  resolveName: (params: ResolveNameParams) => string | null | undefined;
493
- load: (id: string) => MaybePromise<TransformResult | void>;
497
+ load: (id: string) => Promise<SafeParseResult<'load'>>;
494
498
  };
495
499
  type TransformResult = string | null;
496
500
  /**
@@ -518,6 +522,19 @@ declare function build(options: BuildOptions): Promise<BuildOutput>;
518
522
  */
519
523
  declare const defineConfig: (options: MaybePromise<KubbUserConfig> | ((cliOptions: CLIOptions) => MaybePromise<KubbUserConfig>)) => MaybePromise<KubbUserConfig> | ((cliOptions: CLIOptions) => MaybePromise<KubbUserConfig>);
520
524
 
525
+ type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => T['nested'] extends true ? Array<KubbPlugin<T>> : KubbPlugin<T>;
526
+ declare function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: KubbPluginFactory<T>): (options: T['options']) => T["nested"] extends true ? KubbPlugin<T>[] : KubbPlugin<T>;
527
+ type Options = {
528
+ config: PluginContext['config'];
529
+ fileManager: FileManager;
530
+ resolvePath: PluginContext['resolvePath'];
531
+ resolveName: PluginContext['resolveName'];
532
+ load: PluginContext['load'];
533
+ getExecuter: () => Executer<PluginLifecycleHooks> | undefined;
534
+ };
535
+ type CorePluginOptions = PluginFactoryOptions<Options, false, PluginContext>;
536
+ declare const name: "core";
537
+
521
538
  /**
522
539
  * Abstract class that contains the building blocks for plugins to create their own Generator
523
540
  * @link idea based on https://github.com/colinhacks/zod/blob/master/src/types.ts#L137
@@ -536,4 +553,4 @@ declare abstract class SchemaGenerator<TOptions extends object, TInput, TOutput>
536
553
  abstract build(schema: TInput, name: string, description?: string): TOutput;
537
554
  }
538
555
 
539
- export { Argument0, BuildOutput, CLIOptions, Cache, CacheStore, CorePluginOptions, Executer, File, FileManager, FileName, Generator, KubbConfig, KubbJSONPlugin, KubbObjectPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, Logger, MaybePromise, OnExecute, OptionalPath, ParallelPluginError, Path, PathMode, PluginContext, PluginError, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, Queue, QueueTask, Register, ResolveNameParams, ResolvePathParams, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, TreeNodeOptions, UUID, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, validatePlugins, write, writeIndexes };
556
+ export { Argument0, BuildOutput, CLIOptions, Cache, CacheStore, CorePluginOptions, Executer, File, FileManager, FileName, Generator, KubbConfig, KubbJSONPlugin, KubbObjectPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, Logger, MaybePromise, OnExecute, OptionalPath, ParallelPluginError, ParseResult, Path, PathMode, PluginContext, PluginError, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, Queue, QueueTask, Register, ResolveNameParams, ResolvePathParams, SafeParseResult, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, TreeNodeOptions, UUID, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, validatePlugins, write, writeIndexes };
package/dist/index.js CHANGED
@@ -513,6 +513,13 @@ var FileManager = class {
513
513
  });
514
514
  return files;
515
515
  }
516
+ get cachedFiles() {
517
+ const files = [];
518
+ this.cache.forEach((item) => {
519
+ files.push(item);
520
+ });
521
+ return files;
522
+ }
516
523
  async add(file) {
517
524
  const cacheItem = { id: crypto.randomUUID(), file, status: "new" };
518
525
  this.cache.set(cacheItem.id, cacheItem);
@@ -617,7 +624,7 @@ var PluginManager = class {
617
624
  return this.hookFirstSync({
618
625
  hookName: "resolvePath",
619
626
  parameters: [params.fileName, params.directory, params.options]
620
- });
627
+ }).result;
621
628
  };
622
629
  resolveName = (params) => {
623
630
  if (params.pluginName) {
@@ -630,7 +637,7 @@ var PluginManager = class {
630
637
  return this.hookFirstSync({
631
638
  hookName: "resolveName",
632
639
  parameters: [params.name]
633
- });
640
+ }).result;
634
641
  };
635
642
  load = async (id) => {
636
643
  return this.hookFirst({
@@ -681,15 +688,20 @@ var PluginManager = class {
681
688
  for (const plugin of this.getSortedPlugins(hookName)) {
682
689
  if (skipped && skipped.has(plugin))
683
690
  continue;
684
- promise = promise.then((result) => {
685
- if (result != null)
686
- return result;
687
- return this.execute({
691
+ promise = promise.then(async (parseResult) => {
692
+ if (parseResult?.result != null) {
693
+ return parseResult;
694
+ }
695
+ const value = await this.execute({
688
696
  strategy: "hookFirst",
689
697
  hookName,
690
698
  parameters,
691
699
  plugin
692
700
  });
701
+ return Promise.resolve({
702
+ plugin,
703
+ result: value
704
+ });
693
705
  });
694
706
  }
695
707
  return promise;
@@ -703,43 +715,38 @@ var PluginManager = class {
703
715
  parameters,
704
716
  skipped
705
717
  }) {
706
- let result = null;
718
+ let parseResult = null;
707
719
  for (const plugin of this.getSortedPlugins(hookName)) {
708
720
  if (skipped && skipped.has(plugin))
709
721
  continue;
710
- result = this.executeSync({
711
- strategy: "hookFirst",
712
- hookName,
713
- parameters,
722
+ parseResult = {
723
+ result: this.executeSync({
724
+ strategy: "hookFirst",
725
+ hookName,
726
+ parameters,
727
+ plugin
728
+ }),
714
729
  plugin
715
- });
716
- if (result != null) {
730
+ };
731
+ if (parseResult?.result != null) {
717
732
  break;
718
733
  }
719
734
  }
720
- return result;
735
+ return parseResult;
721
736
  }
722
- // parallel
737
+ /**
738
+ *
739
+ * Parallel, runs all plugins
740
+ */
723
741
  async hookParallel({
724
742
  hookName,
725
743
  parameters
726
744
  }) {
727
745
  const parallelPromises = [];
728
746
  for (const plugin of this.getSortedPlugins(hookName)) {
729
- if (plugin[hookName]?.sequential) {
730
- await Promise.all(parallelPromises);
731
- parallelPromises.length = 0;
732
- await this.execute({
733
- strategy: "hookParallel",
734
- hookName,
735
- parameters,
736
- plugin
737
- });
738
- } else {
739
- const promise = this.execute({ strategy: "hookParallel", hookName, parameters, plugin });
740
- if (promise) {
741
- parallelPromises.push(promise);
742
- }
747
+ const promise = this.execute({ strategy: "hookParallel", hookName, parameters, plugin });
748
+ if (promise) {
749
+ parallelPromises.push(promise);
743
750
  }
744
751
  }
745
752
  const results = await Promise.allSettled(parallelPromises);
@@ -749,7 +756,10 @@ var PluginManager = class {
749
756
  }
750
757
  return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
751
758
  }
752
- // chains, reduces returned value, handling the reduced value as the first hook argument
759
+ /**
760
+ *
761
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
762
+ */
753
763
  hookReduceArg0({
754
764
  hookName,
755
765
  parameters,
@@ -758,11 +768,11 @@ var PluginManager = class {
758
768
  const [argument0, ...rest] = parameters;
759
769
  let promise = Promise.resolve(argument0);
760
770
  for (const plugin of this.getSortedPlugins(hookName)) {
761
- promise = promise.then((argument02) => {
771
+ promise = promise.then((arg0) => {
762
772
  const value = this.execute({
763
773
  strategy: "hookReduceArg0",
764
774
  hookName,
765
- parameters: [argument02, ...rest],
775
+ parameters: [arg0, ...rest],
766
776
  plugin
767
777
  });
768
778
  return value;
@@ -770,7 +780,9 @@ var PluginManager = class {
770
780
  }
771
781
  return promise;
772
782
  }
773
- // chains
783
+ /**
784
+ * Chains plugins
785
+ */
774
786
  hookSeq({ hookName, parameters }) {
775
787
  let promise = Promise.resolve();
776
788
  for (const plugin of this.getSortedPlugins(hookName)) {
@@ -786,7 +798,7 @@ var PluginManager = class {
786
798
  return promise.then(noReturn);
787
799
  }
788
800
  getSortedPlugins(_hookName) {
789
- const plugins = [...this.plugins];
801
+ const plugins = [...this.plugins].filter((plugin) => plugin.name !== "core");
790
802
  return plugins;
791
803
  }
792
804
  getPlugin(hookName, pluginName) {
@@ -1050,9 +1062,6 @@ ${source}`;
1050
1062
 
1051
1063
  // src/build.ts
1052
1064
  async function transformReducer(_previousCode, result, _plugin) {
1053
- if (result === null) {
1054
- return null;
1055
- }
1056
1065
  return result;
1057
1066
  }
1058
1067
  async function build(options) {
@@ -1070,11 +1079,14 @@ async function build(options) {
1070
1079
  const queueTask = async (id, file) => {
1071
1080
  const { path } = file;
1072
1081
  let code = getFileSource(file);
1073
- const loadedResult = await pluginManager.hookFirst({
1082
+ const { result: loadedResult } = await pluginManager.hookFirst({
1074
1083
  hookName: "load",
1075
1084
  parameters: [path]
1076
1085
  });
1077
- if (loadedResult) {
1086
+ if (loadedResult && isPromise(loadedResult)) {
1087
+ code = await loadedResult;
1088
+ }
1089
+ if (loadedResult && !isPromise(loadedResult)) {
1078
1090
  code = loadedResult;
1079
1091
  }
1080
1092
  if (code) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/core",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Generator core",
5
5
  "repository": {
6
6
  "type": "git",
@@ -43,7 +43,7 @@
43
43
  "change-case": "^4.1.2",
44
44
  "directory-tree": "^3.5.1",
45
45
  "rimraf": "^5.0.1",
46
- "@kubb/ts-codegen": "1.1.4"
46
+ "@kubb/ts-codegen": "1.1.6"
47
47
  },
48
48
  "devDependencies": {
49
49
  "tsup": "^6.7.0",
@@ -55,7 +55,7 @@
55
55
  "registry": "https://registry.npmjs.org/"
56
56
  },
57
57
  "engines": {
58
- "node": ">=16",
58
+ "node": ">=18",
59
59
  "pnpm": ">=8"
60
60
  },
61
61
  "scripts": {
package/src/build.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  import { PluginManager } from './managers/pluginManager/index.ts'
3
3
  import { clean, isURL, read } from './utils/index.ts'
4
4
  import { getFileSource } from './managers/fileManager/index.ts'
5
+ import { isPromise } from './utils/isPromise.ts'
5
6
 
6
7
  import type { OnExecute } from './managers/pluginManager/index.ts'
7
8
  import type { File } from './managers/fileManager/index.ts'
@@ -24,9 +25,6 @@ async function transformReducer(
24
25
  result: TransformResult | Promise<TransformResult>,
25
26
  _plugin: KubbPlugin
26
27
  ): Promise<string | null> {
27
- if (result === null) {
28
- return null
29
- }
30
28
  return result
31
29
  }
32
30
 
@@ -48,13 +46,16 @@ export async function build(options: BuildOptions): Promise<BuildOutput> {
48
46
  const queueTask = async (id: string, file: File) => {
49
47
  const { path } = file
50
48
 
51
- let code = getFileSource(file)
49
+ let code: string | null = getFileSource(file)
52
50
 
53
- const loadedResult = await pluginManager.hookFirst({
51
+ const { result: loadedResult } = await pluginManager.hookFirst({
54
52
  hookName: 'load',
55
53
  parameters: [path],
56
54
  })
57
- if (loadedResult) {
55
+ if (loadedResult && isPromise(loadedResult)) {
56
+ code = await loadedResult
57
+ }
58
+ if (loadedResult && !isPromise(loadedResult)) {
58
59
  code = loadedResult
59
60
  }
60
61
 
@@ -12,7 +12,7 @@ export class FileManager {
12
12
 
13
13
  private queue?: Queue
14
14
 
15
- constructor(options?: { queue: Queue; task: QueueTask<unknown> }) {
15
+ constructor(options?: { queue: Queue; task?: QueueTask<unknown> }) {
16
16
  if (options) {
17
17
  this.task = options.task
18
18
  this.queue = options.queue
@@ -43,6 +43,15 @@ export class FileManager {
43
43
  return files
44
44
  }
45
45
 
46
+ get cachedFiles() {
47
+ const files: CacheStore[] = []
48
+ this.cache.forEach((item) => {
49
+ files.push(item)
50
+ })
51
+
52
+ return files
53
+ }
54
+
46
55
  async add(file: File) {
47
56
  const cacheItem = { id: crypto.randomUUID(), file, status: 'new' as Status }
48
57
 
@@ -11,7 +11,7 @@ import { Queue } from '../../utils/Queue.ts'
11
11
  import { isPromise } from '../../utils/isPromise.ts'
12
12
 
13
13
  import type { QueueTask } from '../../utils/Queue.ts'
14
- import type { Argument0, Strategy, Executer, OnExecute } from './types.ts'
14
+ import type { Argument0, Strategy, Executer, OnExecute, ParseResult, SafeParseResult } from './types.ts'
15
15
  import type { KubbConfig, KubbPlugin, PluginLifecycleHooks, PluginLifecycle, MaybePromise, ResolvePathParams, ResolveNameParams } from '../../types.ts'
16
16
  import type { CorePluginOptions } from '../../plugin.ts'
17
17
 
@@ -41,7 +41,7 @@ export class PluginManager {
41
41
 
42
42
  private readonly onExecute?: OnExecute
43
43
 
44
- public readonly core: KubbPlugin<CorePluginOptions>
44
+ private readonly core: KubbPlugin<CorePluginOptions>
45
45
 
46
46
  public queue: Queue
47
47
 
@@ -82,7 +82,7 @@ export class PluginManager {
82
82
  return this.hookFirstSync({
83
83
  hookName: 'resolvePath',
84
84
  parameters: [params.fileName, params.directory, params.options],
85
- })
85
+ }).result
86
86
  }
87
87
 
88
88
  resolveName = (params: ResolveNameParams) => {
@@ -96,7 +96,7 @@ export class PluginManager {
96
96
  return this.hookFirstSync({
97
97
  hookName: 'resolveName',
98
98
  parameters: [params.name],
99
- })
99
+ }).result
100
100
  }
101
101
 
102
102
  load = async (id: string) => {
@@ -118,7 +118,7 @@ export class PluginManager {
118
118
  pluginName: string
119
119
  hookName: H
120
120
  parameters: Parameters<PluginLifecycle[H]>
121
- }): Promise<ReturnType<PluginLifecycle[H]> | null> | null {
121
+ }): Promise<ReturnType<ParseResult<H>> | null> | null {
122
122
  const plugin = this.getPlugin(hookName, pluginName)
123
123
 
124
124
  return this.execute({
@@ -137,7 +137,7 @@ export class PluginManager {
137
137
  pluginName: string
138
138
  hookName: H
139
139
  parameters: Parameters<PluginLifecycle[H]>
140
- }): ReturnType<PluginLifecycle[H]> | null {
140
+ }): ReturnType<ParseResult<H>> | null {
141
141
  const plugin = this.getPlugin(hookName, pluginName)
142
142
 
143
143
  return this.executeSync({
@@ -160,20 +160,29 @@ export class PluginManager {
160
160
  hookName: H
161
161
  parameters: Parameters<PluginLifecycle[H]>
162
162
  skipped?: ReadonlySet<KubbPlugin> | null
163
- }): Promise<ReturnType<PluginLifecycle[H]>> {
164
- let promise: Promise<ReturnType<PluginLifecycle[H]>> = Promise.resolve(null as ReturnType<PluginLifecycle[H]>)
163
+ }): Promise<SafeParseResult<H>> {
164
+ let promise: Promise<SafeParseResult<H>> = Promise.resolve(null as any)
165
+
165
166
  for (const plugin of this.getSortedPlugins(hookName)) {
166
167
  if (skipped && skipped.has(plugin)) continue
167
- promise = promise.then((result) => {
168
- if (result != null) return result
169
- return this.execute({
168
+ promise = promise.then(async (parseResult) => {
169
+ if (parseResult?.result != null) {
170
+ return parseResult
171
+ }
172
+ const value = await this.execute<H>({
170
173
  strategy: 'hookFirst',
171
174
  hookName,
172
175
  parameters,
173
176
  plugin,
174
- }) as typeof result
177
+ })
178
+
179
+ return Promise.resolve({
180
+ plugin,
181
+ result: value,
182
+ } as typeof parseResult)
175
183
  })
176
184
  }
185
+
177
186
  return promise
178
187
  }
179
188
 
@@ -189,27 +198,33 @@ export class PluginManager {
189
198
  hookName: H
190
199
  parameters: Parameters<PluginLifecycle[H]>
191
200
  skipped?: ReadonlySet<KubbPlugin> | null
192
- }): ReturnType<PluginLifecycle[H]> {
193
- let result = null
201
+ }): SafeParseResult<H> {
202
+ let parseResult: SafeParseResult<H> = null as unknown as SafeParseResult<H>
194
203
 
195
204
  for (const plugin of this.getSortedPlugins(hookName)) {
196
205
  if (skipped && skipped.has(plugin)) continue
197
206
 
198
- result = this.executeSync<H>({
199
- strategy: 'hookFirst',
200
- hookName,
201
- parameters,
207
+ parseResult = {
208
+ result: this.executeSync<H>({
209
+ strategy: 'hookFirst',
210
+ hookName,
211
+ parameters,
212
+ plugin,
213
+ }),
202
214
  plugin,
203
- })
215
+ } as SafeParseResult<H>
204
216
 
205
- if (result != null) {
217
+ if (parseResult?.result != null) {
206
218
  break
207
219
  }
208
220
  }
209
- return result as ReturnType<PluginLifecycle[H]>
221
+ return parseResult as SafeParseResult<H>
210
222
  }
211
223
 
212
- // parallel
224
+ /**
225
+ *
226
+ * Parallel, runs all plugins
227
+ */
213
228
  async hookParallel<H extends PluginLifecycleHooks, TOuput = void>({
214
229
  hookName,
215
230
  parameters,
@@ -220,21 +235,21 @@ export class PluginManager {
220
235
  const parallelPromises: Promise<TOuput>[] = []
221
236
 
222
237
  for (const plugin of this.getSortedPlugins(hookName)) {
223
- if ((plugin[hookName] as { sequential?: boolean })?.sequential) {
224
- await Promise.all(parallelPromises)
225
- parallelPromises.length = 0
226
- await this.execute({
227
- strategy: 'hookParallel',
228
- hookName,
229
- parameters,
230
- plugin,
231
- })
232
- } else {
233
- const promise: Promise<TOuput> | null = this.execute({ strategy: 'hookParallel', hookName, parameters, plugin })
234
-
235
- if (promise) {
236
- parallelPromises.push(promise)
237
- }
238
+ // TODO implement sequential with `buildStart` as an object({ sequential: boolean; handler: PluginContext["buildStart"] })
239
+ // if ((plugin[hookName] as { sequential?: boolean })?.sequential) {
240
+ // await Promise.all(parallelPromises)
241
+ // parallelPromises.length = 0
242
+ // await this.execute({
243
+ // strategy: 'hookParallel',
244
+ // hookName,
245
+ // parameters,
246
+ // plugin,
247
+ // })
248
+ // }
249
+ const promise: Promise<TOuput> | null = this.execute({ strategy: 'hookParallel', hookName, parameters, plugin })
250
+
251
+ if (promise) {
252
+ parallelPromises.push(promise)
238
253
  }
239
254
  }
240
255
  const results = await Promise.allSettled(parallelPromises)
@@ -247,7 +262,10 @@ export class PluginManager {
247
262
  return results.filter((result) => result.status === 'fulfilled').map((result) => (result as PromiseFulfilledResult<Awaited<TOuput>>).value)
248
263
  }
249
264
 
250
- // chains, reduces returned value, handling the reduced value as the first hook argument
265
+ /**
266
+ *
267
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
268
+ */
251
269
  hookReduceArg0<H extends PluginLifecycleHooks>({
252
270
  hookName,
253
271
  parameters,
@@ -255,29 +273,30 @@ export class PluginManager {
255
273
  }: {
256
274
  hookName: H
257
275
  parameters: Parameters<PluginLifecycle[H]>
258
- reduce: (reduction: Argument0<H>, result: ReturnType<PluginLifecycle[H]>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>
276
+ reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>
259
277
  }): Promise<Argument0<H>> {
260
278
  const [argument0, ...rest] = parameters
261
279
 
262
280
  let promise: Promise<Argument0<H>> = Promise.resolve(argument0)
263
281
  for (const plugin of this.getSortedPlugins(hookName)) {
264
282
  promise = promise
265
- .then((argument0) => {
283
+ .then((arg0) => {
266
284
  const value = this.execute({
267
285
  strategy: 'hookReduceArg0',
268
286
  hookName,
269
- parameters: [argument0, ...rest] as Parameters<PluginLifecycle[H]>,
287
+ parameters: [arg0, ...rest] as Parameters<PluginLifecycle[H]>,
270
288
  plugin,
271
289
  })
272
290
  return value
273
291
  })
274
- .then((result) => reduce.call(this.core.api, argument0, result as ReturnType<PluginLifecycle[H]>, plugin)) as Promise<Argument0<H>>
292
+ .then((result) => reduce.call(this.core.api, argument0, result as ReturnType<ParseResult<H>>, plugin)) as Promise<Argument0<H>>
275
293
  }
276
294
  return promise
277
295
  }
278
296
 
279
- // chains
280
-
297
+ /**
298
+ * Chains plugins
299
+ */
281
300
  hookSeq<H extends PluginLifecycleHooks>({ hookName, parameters }: { hookName: H; parameters?: Parameters<PluginLifecycle[H]> }) {
282
301
  let promise: Promise<void | null> = Promise.resolve()
283
302
  for (const plugin of this.getSortedPlugins(hookName)) {
@@ -294,12 +313,12 @@ export class PluginManager {
294
313
  }
295
314
 
296
315
  private getSortedPlugins(_hookName: keyof PluginLifecycle): KubbPlugin[] {
297
- const plugins = [...this.plugins]
316
+ const plugins = [...this.plugins].filter((plugin) => plugin.name !== 'core')
298
317
 
299
318
  return plugins
300
319
  }
301
320
 
302
- private getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin {
321
+ public getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin {
303
322
  const plugins = [...this.plugins]
304
323
 
305
324
  const pluginByPluginName = plugins.find((item) => item.name === pluginName && item[hookName])
@@ -400,7 +419,7 @@ export class PluginManager {
400
419
  hookName: H
401
420
  parameters: Parameters<PluginLifecycle[H]>
402
421
  plugin: KubbPlugin
403
- }): ReturnType<PluginLifecycle[H]> | null {
422
+ }): ReturnType<ParseResult<H>> | null {
404
423
  const hook = plugin[hookName]
405
424
 
406
425
  if (!hook) {
@@ -438,7 +457,7 @@ export class PluginManager {
438
457
  } catch (e) {
439
458
  this.catcher<H>(e as Error, plugin, hookName)
440
459
 
441
- return null as ReturnType<PluginLifecycle[H]>
460
+ return null as ReturnType<ParseResult<H>>
442
461
  }
443
462
  }
444
463
 
@@ -15,3 +15,10 @@ export type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
15
15
  }
16
16
 
17
17
  export type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (this: PluginManager, executer: Executer<H> | undefined) => void
18
+
19
+ export type ParseResult<H extends PluginLifecycleHooks> = PluginLifecycle[H]
20
+
21
+ export type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseResult<H>>> = {
22
+ result: Result
23
+ plugin: KubbPlugin
24
+ }
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-empty-interface */
2
- import type { PluginManager } from './managers/index.ts'
2
+ import type { PluginManager, SafeParseResult } from './managers/index.ts'
3
3
  import type { FileManager, File } from './managers/fileManager/index.ts'
4
4
  import type { Cache } from './utils/cache.ts'
5
5
 
@@ -237,7 +237,7 @@ export type PluginContext<TOptions = Record<string, any>> = {
237
237
  addFile: (...file: File[]) => Promise<File[]>
238
238
  resolvePath: (params: ResolvePathParams<TOptions>) => OptionalPath
239
239
  resolveName: (params: ResolveNameParams) => string | null | undefined
240
- load: (id: string) => MaybePromise<TransformResult | void>
240
+ load: (id: string) => Promise<SafeParseResult<'load'>>
241
241
  }
242
242
 
243
243
  // null will mean clear the watcher for this key