@kubb/core 1.1.10 → 1.1.12

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.
Files changed (42) hide show
  1. package/dist/index.cjs +44 -16
  2. package/dist/index.d.ts +18 -13
  3. package/dist/index.js +44 -17
  4. package/package.json +3 -4
  5. package/src/build.ts +0 -106
  6. package/src/config.ts +0 -15
  7. package/src/generators/Generator.ts +0 -23
  8. package/src/generators/SchemaGenerator.ts +0 -8
  9. package/src/generators/index.ts +0 -2
  10. package/src/index.ts +0 -12
  11. package/src/managers/fileManager/FileManager.ts +0 -127
  12. package/src/managers/fileManager/index.ts +0 -3
  13. package/src/managers/fileManager/types.ts +0 -40
  14. package/src/managers/fileManager/utils.ts +0 -167
  15. package/src/managers/index.ts +0 -2
  16. package/src/managers/pluginManager/ParallelPluginError.ts +0 -15
  17. package/src/managers/pluginManager/PluginError.ts +0 -11
  18. package/src/managers/pluginManager/PluginManager.ts +0 -472
  19. package/src/managers/pluginManager/index.ts +0 -5
  20. package/src/managers/pluginManager/types.ts +0 -25
  21. package/src/managers/pluginManager/validate.ts +0 -21
  22. package/src/plugin.ts +0 -110
  23. package/src/types.ts +0 -253
  24. package/src/utils/Queue.ts +0 -46
  25. package/src/utils/TreeNode.ts +0 -122
  26. package/src/utils/cache.ts +0 -33
  27. package/src/utils/clean.ts +0 -5
  28. package/src/utils/getEncodedText.ts +0 -3
  29. package/src/utils/getStackTrace.ts +0 -20
  30. package/src/utils/getUniqueName.ts +0 -9
  31. package/src/utils/index.ts +0 -18
  32. package/src/utils/isPromise.ts +0 -5
  33. package/src/utils/isURL.ts +0 -11
  34. package/src/utils/jsdoc.ts +0 -13
  35. package/src/utils/nameSorter.ts +0 -9
  36. package/src/utils/objectToParameters.ts +0 -28
  37. package/src/utils/read.ts +0 -45
  38. package/src/utils/renderTemplate.ts +0 -11
  39. package/src/utils/timeout.ts +0 -7
  40. package/src/utils/transformReservedWord.ts +0 -97
  41. package/src/utils/uniqueId.ts +0 -5
  42. package/src/utils/write.ts +0 -25
package/dist/index.cjs CHANGED
@@ -42,13 +42,13 @@ async function write(data, path) {
42
42
  }
43
43
 
44
44
  // src/utils/cache.ts
45
- function createPluginCache(cache) {
45
+ function createPluginCache(Store) {
46
46
  return {
47
- delete(id) {
48
- return delete cache[id];
47
+ set(id, value) {
48
+ Store[id] = [0, value];
49
49
  },
50
50
  get(id) {
51
- const item = cache[id];
51
+ const item = Store[id];
52
52
  if (!item) {
53
53
  return null;
54
54
  }
@@ -56,15 +56,15 @@ function createPluginCache(cache) {
56
56
  return item[1];
57
57
  },
58
58
  has(id) {
59
- const item = cache[id];
59
+ const item = Store[id];
60
60
  if (!item) {
61
61
  return false;
62
62
  }
63
63
  item[0] = 0;
64
64
  return true;
65
65
  },
66
- set(id, value) {
67
- cache[id] = [0, value];
66
+ delete(id) {
67
+ return delete Store[id];
68
68
  }
69
69
  };
70
70
  }
@@ -413,6 +413,33 @@ function getStackTrace(belowFn) {
413
413
  // src/utils/uniqueId.ts
414
414
  var uniqueId = ((counter) => (str = "") => `${str}${++counter}`)(0);
415
415
 
416
+ // src/utils/throttle.ts
417
+ var throttle = (fn, delay) => {
418
+ let wait = false;
419
+ let timeout2;
420
+ let cancelled = false;
421
+ return [
422
+ (...args) => {
423
+ if (cancelled) {
424
+ return void 0;
425
+ }
426
+ if (wait) {
427
+ return void 0;
428
+ }
429
+ const val = fn(...args);
430
+ wait = true;
431
+ timeout2 = setTimeout(() => {
432
+ wait = false;
433
+ }, delay);
434
+ return val;
435
+ },
436
+ () => {
437
+ cancelled = true;
438
+ clearTimeout(timeout2);
439
+ }
440
+ ];
441
+ };
442
+
416
443
  // src/managers/fileManager/FileManager.ts
417
444
  var FileManager = class {
418
445
  cache = /* @__PURE__ */ new Map();
@@ -559,8 +586,8 @@ function combineFiles(files) {
559
586
  const prev = acc[prevIndex];
560
587
  acc[prevIndex] = {
561
588
  ...curr,
562
- source: `${prev.source}
563
- ${curr.source}`,
589
+ source: prev.source && curr.source ? `${prev.source}
590
+ ${curr.source}` : "'",
564
591
  imports: [...prev.imports || [], ...curr.imports || []],
565
592
  exports: [...prev.exports || [], ...curr.exports || []]
566
593
  };
@@ -662,7 +689,7 @@ var definePlugin = createPlugin((options) => {
662
689
  }
663
690
  return 0;
664
691
  });
665
- const pluginName = plugins?.[0].name;
692
+ const pluginName = plugins?.[0]?.name;
666
693
  return Promise.all(
667
694
  files.map((file) => {
668
695
  const fileWithMeta = {
@@ -742,7 +769,7 @@ var PluginManager = class {
742
769
  executer;
743
770
  executed = [];
744
771
  constructor(config, options) {
745
- this.onExecute = options.onExecute;
772
+ this.onExecute = options.onExecute?.bind(this);
746
773
  this.queue = new Queue(10);
747
774
  this.fileManager = new FileManager({ task: options.task, queue: this.queue });
748
775
  this.core = definePlugin({
@@ -899,6 +926,7 @@ var PluginManager = class {
899
926
  const results = await Promise.allSettled(parallelPromises);
900
927
  const errors = results.filter((result) => result.status === "rejected").map((result) => result.reason);
901
928
  if (errors.length) {
929
+ console.log(errors);
902
930
  throw new ParallelPluginError("Error", { errors, pluginManager: this });
903
931
  }
904
932
  return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
@@ -957,7 +985,7 @@ var PluginManager = class {
957
985
  return pluginByPluginName;
958
986
  }
959
987
  addExecuter(executer) {
960
- this.onExecute?.call(this, executer);
988
+ this.onExecute?.call(this, executer, this);
961
989
  if (executer) {
962
990
  this.executed.push(executer);
963
991
  }
@@ -1127,10 +1155,9 @@ async function build(options) {
1127
1155
  if (!executer) {
1128
1156
  return;
1129
1157
  }
1130
- const { strategy, hookName, plugin } = executer;
1131
- if (config.logLevel === "info" && logger?.spinner) {
1132
- logger.spinner.text = `[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name}
1133
- `;
1158
+ const { hookName, plugin } = executer;
1159
+ if (config.logLevel === "info" && logger) {
1160
+ logger.log(null, { logLevel: config.logLevel, params: { hookName, pluginName: plugin.name } });
1134
1161
  }
1135
1162
  };
1136
1163
  const pluginManager = new PluginManager(config, { task: queueTask, onExecute });
@@ -1205,6 +1232,7 @@ exports.nameSorter = nameSorter;
1205
1232
  exports.objectToParameters = objectToParameters;
1206
1233
  exports.read = read;
1207
1234
  exports.renderTemplate = renderTemplate;
1235
+ exports.throttle = throttle;
1208
1236
  exports.timeout = timeout;
1209
1237
  exports.transformReservedWord = transformReservedWord;
1210
1238
  exports.uniqueId = uniqueId;
package/dist/index.d.ts CHANGED
@@ -5,13 +5,13 @@ declare function isPromise<T>(result: MaybePromise<T>): result is Promise<T>;
5
5
 
6
6
  declare function write(data: string, path: string): Promise<void>;
7
7
 
8
- interface Cache<T extends object = object> {
9
- delete(id: keyof T): boolean;
10
- get(id: keyof T): T[keyof T] | null;
11
- has(id: keyof T): boolean;
12
- set(id: keyof T, value: unknown): void;
8
+ interface Cache<TStore extends object = object> {
9
+ delete(id: keyof TStore): boolean;
10
+ get(id: keyof TStore): TStore[keyof TStore] | null;
11
+ has(id: keyof TStore): boolean;
12
+ set(id: keyof TStore, value: unknown): void;
13
13
  }
14
- declare function createPluginCache<T extends Record<string, [number, unknown]>>(cache: T): Cache<T>;
14
+ declare function createPluginCache<TStore extends Record<string, [number, unknown]>>(Store: TStore): Cache<TStore>;
15
15
 
16
16
  declare function getRelativePath(rootDir?: string | null, filePath?: string | null, platform?: 'windows' | 'mac' | 'linux'): string;
17
17
  type PathMode = 'file' | 'directory';
@@ -43,7 +43,7 @@ declare function getUniqueName(originalName: string, data: Record<string, number
43
43
  declare function timeout(ms: number): Promise<unknown>;
44
44
 
45
45
  type QueueTask<T = unknown> = {
46
- (...args: unknown[]): Promise<T>;
46
+ (...args: unknown[]): Promise<T> | Promise<void>;
47
47
  };
48
48
  declare class Queue {
49
49
  private readonly queue;
@@ -80,6 +80,8 @@ declare function getStackTrace(belowFn?: Function): NodeJS.CallSite[];
80
80
 
81
81
  declare const uniqueId: (str?: string) => string;
82
82
 
83
+ declare const throttle: <R, A extends any[]>(fn: (...args: A) => R, delay: number) => [(...args: A) => R | undefined, () => void];
84
+
83
85
  type Import = {
84
86
  name: string | string[];
85
87
  path: string;
@@ -126,7 +128,7 @@ declare class FileManager {
126
128
  private queue?;
127
129
  constructor(options?: {
128
130
  queue: Queue;
129
- task?: QueueTask<unknown>;
131
+ task?: QueueTask<File>;
130
132
  });
131
133
  private getCache;
132
134
  getCacheByPath(path: string | undefined): CacheStore | undefined;
@@ -156,7 +158,7 @@ type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
156
158
  hookName: H;
157
159
  plugin: KubbPlugin;
158
160
  };
159
- type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (this: PluginManager, executer: Executer<H> | undefined) => void;
161
+ type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (this: PluginManager, executer: Executer<H> | undefined, pluginManager: PluginManager) => void;
160
162
  type ParseResult<H extends PluginLifecycleHooks> = PluginLifecycle[H];
161
163
  type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseResult<H>>> = {
162
164
  result: Result;
@@ -165,7 +167,7 @@ type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseRe
165
167
 
166
168
  declare const hooks: [keyof PluginLifecycle<PluginFactoryOptions<unknown, false, any, Record<string, any>>>];
167
169
  type Options$1 = {
168
- task: QueueTask;
170
+ task: QueueTask<File>;
169
171
  onExecute?: OnExecute<PluginLifecycleHooks>;
170
172
  };
171
173
  declare class PluginManager {
@@ -507,8 +509,11 @@ type OptionalPath = Path | null | undefined;
507
509
  type FileName = string | null | undefined;
508
510
  type LogLevel = 'error' | 'info' | 'silent';
509
511
 
510
- type Logger = {
511
- log: (message: string, logLevel: LogLevel) => void;
512
+ type Logger<TParams = Record<string, any>> = {
513
+ log: (message: string | null, options: {
514
+ logLevel: LogLevel;
515
+ params?: TParams;
516
+ }) => void;
512
517
  spinner?: Ora;
513
518
  };
514
519
  type BuildOptions = {
@@ -555,4 +560,4 @@ declare abstract class SchemaGenerator<TOptions extends object, TInput, TOutput>
555
560
  abstract build(schema: TInput, name: string, description?: string): TOutput;
556
561
  }
557
562
 
558
- 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, uniqueId, validatePlugins, write, writeIndexes };
563
+ 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, throttle, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes };
package/dist/index.js CHANGED
@@ -32,13 +32,13 @@ async function write(data, path) {
32
32
  }
33
33
 
34
34
  // src/utils/cache.ts
35
- function createPluginCache(cache) {
35
+ function createPluginCache(Store) {
36
36
  return {
37
- delete(id) {
38
- return delete cache[id];
37
+ set(id, value) {
38
+ Store[id] = [0, value];
39
39
  },
40
40
  get(id) {
41
- const item = cache[id];
41
+ const item = Store[id];
42
42
  if (!item) {
43
43
  return null;
44
44
  }
@@ -46,15 +46,15 @@ function createPluginCache(cache) {
46
46
  return item[1];
47
47
  },
48
48
  has(id) {
49
- const item = cache[id];
49
+ const item = Store[id];
50
50
  if (!item) {
51
51
  return false;
52
52
  }
53
53
  item[0] = 0;
54
54
  return true;
55
55
  },
56
- set(id, value) {
57
- cache[id] = [0, value];
56
+ delete(id) {
57
+ return delete Store[id];
58
58
  }
59
59
  };
60
60
  }
@@ -403,6 +403,33 @@ function getStackTrace(belowFn) {
403
403
  // src/utils/uniqueId.ts
404
404
  var uniqueId = ((counter) => (str = "") => `${str}${++counter}`)(0);
405
405
 
406
+ // src/utils/throttle.ts
407
+ var throttle = (fn, delay) => {
408
+ let wait = false;
409
+ let timeout2;
410
+ let cancelled = false;
411
+ return [
412
+ (...args) => {
413
+ if (cancelled) {
414
+ return void 0;
415
+ }
416
+ if (wait) {
417
+ return void 0;
418
+ }
419
+ const val = fn(...args);
420
+ wait = true;
421
+ timeout2 = setTimeout(() => {
422
+ wait = false;
423
+ }, delay);
424
+ return val;
425
+ },
426
+ () => {
427
+ cancelled = true;
428
+ clearTimeout(timeout2);
429
+ }
430
+ ];
431
+ };
432
+
406
433
  // src/managers/fileManager/FileManager.ts
407
434
  var FileManager = class {
408
435
  cache = /* @__PURE__ */ new Map();
@@ -549,8 +576,8 @@ function combineFiles(files) {
549
576
  const prev = acc[prevIndex];
550
577
  acc[prevIndex] = {
551
578
  ...curr,
552
- source: `${prev.source}
553
- ${curr.source}`,
579
+ source: prev.source && curr.source ? `${prev.source}
580
+ ${curr.source}` : "'",
554
581
  imports: [...prev.imports || [], ...curr.imports || []],
555
582
  exports: [...prev.exports || [], ...curr.exports || []]
556
583
  };
@@ -652,7 +679,7 @@ var definePlugin = createPlugin((options) => {
652
679
  }
653
680
  return 0;
654
681
  });
655
- const pluginName = plugins?.[0].name;
682
+ const pluginName = plugins?.[0]?.name;
656
683
  return Promise.all(
657
684
  files.map((file) => {
658
685
  const fileWithMeta = {
@@ -732,7 +759,7 @@ var PluginManager = class {
732
759
  executer;
733
760
  executed = [];
734
761
  constructor(config, options) {
735
- this.onExecute = options.onExecute;
762
+ this.onExecute = options.onExecute?.bind(this);
736
763
  this.queue = new Queue(10);
737
764
  this.fileManager = new FileManager({ task: options.task, queue: this.queue });
738
765
  this.core = definePlugin({
@@ -889,6 +916,7 @@ var PluginManager = class {
889
916
  const results = await Promise.allSettled(parallelPromises);
890
917
  const errors = results.filter((result) => result.status === "rejected").map((result) => result.reason);
891
918
  if (errors.length) {
919
+ console.log(errors);
892
920
  throw new ParallelPluginError("Error", { errors, pluginManager: this });
893
921
  }
894
922
  return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
@@ -947,7 +975,7 @@ var PluginManager = class {
947
975
  return pluginByPluginName;
948
976
  }
949
977
  addExecuter(executer) {
950
- this.onExecute?.call(this, executer);
978
+ this.onExecute?.call(this, executer, this);
951
979
  if (executer) {
952
980
  this.executed.push(executer);
953
981
  }
@@ -1117,10 +1145,9 @@ async function build(options) {
1117
1145
  if (!executer) {
1118
1146
  return;
1119
1147
  }
1120
- const { strategy, hookName, plugin } = executer;
1121
- if (config.logLevel === "info" && logger?.spinner) {
1122
- logger.spinner.text = `[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name}
1123
- `;
1148
+ const { hookName, plugin } = executer;
1149
+ if (config.logLevel === "info" && logger) {
1150
+ logger.log(null, { logLevel: config.logLevel, params: { hookName, pluginName: plugin.name } });
1124
1151
  }
1125
1152
  };
1126
1153
  const pluginManager = new PluginManager(config, { task: queueTask, onExecute });
@@ -1164,4 +1191,4 @@ var SchemaGenerator = class extends Generator {
1164
1191
  // src/index.ts
1165
1192
  var src_default = build;
1166
1193
 
1167
- export { FileManager, Generator, ParallelPluginError, PluginError, PluginManager, Queue, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes };
1194
+ export { FileManager, Generator, ParallelPluginError, PluginError, PluginManager, Queue, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, throttle, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/core",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
4
4
  "description": "Generator core",
5
5
  "keywords": [
6
6
  "typescript",
@@ -34,7 +34,6 @@
34
34
  },
35
35
  "files": [
36
36
  "dist",
37
- "src",
38
37
  "schemas",
39
38
  "!/**/**.test.**",
40
39
  "!/**/__tests__/**"
@@ -42,10 +41,10 @@
42
41
  "dependencies": {
43
42
  "change-case": "^4.1.2",
44
43
  "directory-tree": "^3.5.1",
45
- "graceful-fs": "^4.2.11",
46
44
  "fs-extra": "^11.1.1",
45
+ "graceful-fs": "^4.2.11",
47
46
  "rimraf": "^5.0.1",
48
- "@kubb/ts-codegen": "1.1.10"
47
+ "@kubb/ts-codegen": "1.1.12"
49
48
  },
50
49
  "devDependencies": {
51
50
  "@types/fs-extra": "^11.0.1",
package/src/build.ts DELETED
@@ -1,106 +0,0 @@
1
- import { getFileSource } from './managers/fileManager/index.ts'
2
- import { PluginManager } from './managers/pluginManager/index.ts'
3
- import { clean, isURL, read } from './utils/index.ts'
4
- import { isPromise } from './utils/isPromise.ts'
5
-
6
- import type { Ora } from 'ora'
7
- import type { File } from './managers/fileManager/index.ts'
8
- import type { OnExecute } from './managers/pluginManager/index.ts'
9
- import type { BuildOutput, KubbPlugin, LogLevel, PluginContext, TransformResult } from './types.ts'
10
- import type { QueueTask } from './utils/index.ts'
11
-
12
- export type Logger = {
13
- log: (message: string, logLevel: LogLevel) => void
14
- spinner?: Ora
15
- }
16
- type BuildOptions = {
17
- config: PluginContext['config']
18
- logger?: Logger
19
- }
20
-
21
- async function transformReducer(
22
- this: PluginContext,
23
- _previousCode: string,
24
- result: TransformResult | Promise<TransformResult>,
25
-
26
- _plugin: KubbPlugin
27
- ): Promise<string | null> {
28
- return result
29
- }
30
-
31
- export async function build(options: BuildOptions): Promise<BuildOutput> {
32
- const { config, logger } = options
33
-
34
- try {
35
- if (!isURL(config.input.path)) {
36
- await read(config.input.path)
37
- }
38
- } catch (e: any) {
39
- throw new Error('Cannot read file/URL defined in `input.path` or set with --input in the CLI of your Kubb config', { cause: e })
40
- }
41
-
42
- if (config.output.clean) {
43
- await clean(config.output.path)
44
- }
45
-
46
- const queueTask = async (id: string, file: File) => {
47
- const { path } = file
48
-
49
- let code: string | null = getFileSource(file)
50
-
51
- const { result: loadedResult } = await pluginManager.hookFirst({
52
- hookName: 'load',
53
- parameters: [path],
54
- })
55
- if (loadedResult && isPromise(loadedResult)) {
56
- code = await loadedResult
57
- }
58
- if (loadedResult && !isPromise(loadedResult)) {
59
- code = loadedResult
60
- }
61
-
62
- if (code) {
63
- const transformedCode = await pluginManager.hookReduceArg0({
64
- hookName: 'transform',
65
- parameters: [code, path],
66
- reduce: transformReducer,
67
- })
68
-
69
- if (config.output.write || config.output.write === undefined) {
70
- await pluginManager.hookParallel({
71
- hookName: 'writeFile',
72
- parameters: [transformedCode, path],
73
- })
74
- }
75
- }
76
- }
77
-
78
- const onExecute: OnExecute = (executer) => {
79
- if (!executer) {
80
- return
81
- }
82
-
83
- const { strategy, hookName, plugin } = executer
84
-
85
- if (config.logLevel === 'info' && logger?.spinner) {
86
- logger.spinner.text = `[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name} \n`
87
- }
88
- }
89
-
90
- const pluginManager = new PluginManager(config, { task: queueTask as QueueTask, onExecute })
91
- const { plugins, fileManager } = pluginManager
92
-
93
- await pluginManager.hookParallel<'validate', true>({
94
- hookName: 'validate',
95
- parameters: [plugins],
96
- })
97
-
98
- await pluginManager.hookParallel({
99
- hookName: 'buildStart',
100
- parameters: [config],
101
- })
102
-
103
- await pluginManager.hookParallel({ hookName: 'buildEnd' })
104
-
105
- return { files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })), pluginManager }
106
- }
package/src/config.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { CLIOptions, KubbUserConfig, MaybePromise } from './types.ts'
2
-
3
- /**
4
- * Type helper to make it easier to use kubb.config.js
5
- * accepts a direct {@link KubbConfig} object, or a function that returns it.
6
- * The function receives a {@link ConfigEnv} object that exposes two properties:
7
- */
8
- export const defineConfig = (
9
- options:
10
- | MaybePromise<KubbUserConfig>
11
- | ((
12
- /** The options derived from the CLI flags */
13
- cliOptions: CLIOptions
14
- ) => MaybePromise<KubbUserConfig>)
15
- ) => options
@@ -1,23 +0,0 @@
1
- /**
2
- * Abstract class that contains the building blocks for plugins to create their own Generator
3
- * @link idea based on https://github.com/colinhacks/zod/blob/master/src/types.ts#L137
4
- */
5
- export abstract class Generator<TOptions extends object = object> {
6
- private _options: TOptions = {} as TOptions
7
-
8
- constructor(options: TOptions = {} as TOptions) {
9
- if (options) {
10
- this._options = {
11
- ...this._options,
12
- ...options,
13
- }
14
- }
15
- return this
16
- }
17
-
18
- get options() {
19
- return this._options
20
- }
21
-
22
- abstract build(...params: unknown[]): unknown
23
- }
@@ -1,8 +0,0 @@
1
- import { Generator } from './Generator.ts'
2
-
3
- /**
4
- * Abstract class that contains the building blocks for plugins to create their own SchemaGenerator
5
- */
6
- export abstract class SchemaGenerator<TOptions extends object, TInput, TOutput> extends Generator<TOptions> {
7
- abstract build(schema: TInput, name: string, description?: string): TOutput
8
- }
@@ -1,2 +0,0 @@
1
- export * from './SchemaGenerator.ts'
2
- export * from './Generator.ts'
package/src/index.ts DELETED
@@ -1,12 +0,0 @@
1
- import { build } from './build.ts'
2
-
3
- export * from './config.ts'
4
- export * from './build.ts'
5
- export * from './types.ts'
6
- export { CorePluginOptions, createPlugin, name } from './plugin.ts'
7
-
8
- export * from './utils/index.ts'
9
- export * from './managers/index.ts'
10
- export * from './generators/index.ts'
11
-
12
- export default build
@@ -1,127 +0,0 @@
1
- import crypto from 'node:crypto'
2
-
3
- import { read, write } from '../../utils/index.ts'
4
-
5
- import type { Queue, QueueTask } from '../../utils/index.ts'
6
- import type { CacheStore, File, Status, UUID } from './types.ts'
7
-
8
- export class FileManager {
9
- private cache: Map<CacheStore['id'], CacheStore> = new Map()
10
-
11
- private task?: QueueTask<unknown>
12
-
13
- private queue?: Queue
14
-
15
- constructor(options?: { queue: Queue; task?: QueueTask<unknown> }) {
16
- if (options) {
17
- this.task = options.task
18
- this.queue = options.queue
19
- }
20
- }
21
-
22
- private getCache(id: UUID) {
23
- return this.cache.get(id)
24
- }
25
-
26
- getCacheByPath(path: string | undefined): CacheStore | undefined {
27
- let cache
28
-
29
- this.cache.forEach((item) => {
30
- if (item.file.path === path) {
31
- cache = item
32
- }
33
- })
34
- return cache as unknown as CacheStore
35
- }
36
-
37
- get files() {
38
- const files: File[] = []
39
- this.cache.forEach((item) => {
40
- files.push(item.file)
41
- })
42
-
43
- return files
44
- }
45
-
46
- get cachedFiles() {
47
- const files: CacheStore[] = []
48
- this.cache.forEach((item) => {
49
- files.push(item)
50
- })
51
-
52
- return files
53
- }
54
-
55
- async add(file: File) {
56
- const cacheItem = { id: crypto.randomUUID(), file, status: 'new' as Status }
57
-
58
- this.cache.set(cacheItem.id, cacheItem)
59
-
60
- if (this.queue) {
61
- await this.queue.run(async () => {
62
- await this.task?.(cacheItem.id, file)
63
- })
64
- }
65
-
66
- return file
67
- }
68
-
69
- addOrAppend(file: File) {
70
- if (!file.path.endsWith(file.fileName)) {
71
- // console.warn(`Path ${file.path}(file.path) should end with the fileName ${file.fileName}(file.filename)`)
72
- }
73
-
74
- const previousCache = this.getCacheByPath(file.path)
75
-
76
- if (previousCache) {
77
- // empty source will also return true when using includes
78
- const sourceAlreadyExists = file.source && previousCache.file.source.includes(file.source)
79
-
80
- if (sourceAlreadyExists) {
81
- return Promise.resolve(file)
82
- }
83
-
84
- this.cache.delete(previousCache.id)
85
-
86
- return this.add({
87
- ...file,
88
- source: `${previousCache.file.source}\n${file.source}`,
89
- imports: [...(previousCache.file.imports || []), ...(file.imports || [])],
90
- exports: [...(previousCache.file.exports || []), ...(file.exports || [])],
91
- })
92
- }
93
- return this.add(file)
94
- }
95
-
96
- setStatus(id: UUID, status: Status) {
97
- const cacheItem = this.getCache(id)
98
- if (!cacheItem) {
99
- return
100
- }
101
-
102
- cacheItem.status = status
103
- this.cache.set(id, cacheItem)
104
- }
105
-
106
- get(id: UUID) {
107
- const cacheItem = this.getCache(id)
108
- return cacheItem?.file
109
- }
110
-
111
- remove(id: UUID) {
112
- const cacheItem = this.getCache(id)
113
- if (!cacheItem) {
114
- return
115
- }
116
-
117
- this.setStatus(id, 'removed')
118
- }
119
-
120
- async write(...params: Parameters<typeof write>) {
121
- return write(...params)
122
- }
123
-
124
- async read(...params: Parameters<typeof read>) {
125
- return read(...params)
126
- }
127
- }