@kubb/core 1.1.9 → 1.1.11

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
@@ -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.9",
3
+ "version": "1.1.11",
4
4
  "description": "Generator core",
5
5
  "keywords": [
6
6
  "typescript",
@@ -45,7 +45,7 @@
45
45
  "graceful-fs": "^4.2.11",
46
46
  "fs-extra": "^11.1.1",
47
47
  "rimraf": "^5.0.1",
48
- "@kubb/ts-codegen": "1.1.9"
48
+ "@kubb/ts-codegen": "1.1.11"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/fs-extra": "^11.0.1",
@@ -70,7 +70,7 @@
70
70
  "build": "tsup",
71
71
  "clean": "rimraf ./dist",
72
72
  "lint": "ESLINT_USE_FLAT_CONFIG=true eslint \"**/*.{ts,tsx}\"",
73
- "lint-fix": "eslint \"**/*.{ts,tsx}\" --quiet --fix",
73
+ "lint-fix": "bun run lint --quiet --fix",
74
74
  "release": "pnpm publish --no-git-check",
75
75
  "start": "tsup --watch",
76
76
  "test": "vitest --passWithNoTests",
package/src/build.ts CHANGED
@@ -9,8 +9,8 @@ import type { OnExecute } from './managers/pluginManager/index.ts'
9
9
  import type { BuildOutput, KubbPlugin, LogLevel, PluginContext, TransformResult } from './types.ts'
10
10
  import type { QueueTask } from './utils/index.ts'
11
11
 
12
- export type Logger = {
13
- log: (message: string, logLevel: LogLevel) => void
12
+ export type Logger<TParams = Record<string, any>> = {
13
+ log: (message: string | null, options: { logLevel: LogLevel; params?: TParams }) => void
14
14
  spinner?: Ora
15
15
  }
16
16
  type BuildOptions = {
@@ -80,14 +80,14 @@ export async function build(options: BuildOptions): Promise<BuildOutput> {
80
80
  return
81
81
  }
82
82
 
83
- const { strategy, hookName, plugin } = executer
83
+ const { hookName, plugin } = executer
84
84
 
85
- if (config.logLevel === 'info' && logger?.spinner) {
86
- logger.spinner.text = `[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name} \n`
85
+ if (config.logLevel === 'info' && logger) {
86
+ logger.log(null, { logLevel: config.logLevel, params: { hookName, pluginName: plugin.name } })
87
87
  }
88
88
  }
89
89
 
90
- const pluginManager = new PluginManager(config, { task: queueTask as QueueTask, onExecute })
90
+ const pluginManager = new PluginManager(config, { task: queueTask as QueueTask<File>, onExecute })
91
91
  const { plugins, fileManager } = pluginManager
92
92
 
93
93
  await pluginManager.hookParallel<'validate', true>({
@@ -8,11 +8,11 @@ import type { CacheStore, File, Status, UUID } from './types.ts'
8
8
  export class FileManager {
9
9
  private cache: Map<CacheStore['id'], CacheStore> = new Map()
10
10
 
11
- private task?: QueueTask<unknown>
11
+ private task?: QueueTask<File>
12
12
 
13
13
  private queue?: Queue
14
14
 
15
- constructor(options?: { queue: Queue; task?: QueueTask<unknown> }) {
15
+ constructor(options?: { queue: Queue; task?: QueueTask<File> }) {
16
16
  if (options) {
17
17
  this.task = options.task
18
18
  this.queue = options.queue
@@ -53,7 +53,7 @@ export class FileManager {
53
53
  }
54
54
 
55
55
  async add(file: File) {
56
- const cacheItem = { id: crypto.randomUUID(), file, status: 'new' as Status }
56
+ const cacheItem: CacheStore = { id: crypto.randomUUID(), file, status: 'new' as Status }
57
57
 
58
58
  this.cache.set(cacheItem.id, cacheItem)
59
59
 
@@ -82,7 +82,7 @@ export function combineFiles(files: Array<File | null>): File[] {
82
82
  const prev = acc[prevIndex]
83
83
  acc[prevIndex] = {
84
84
  ...curr,
85
- source: `${prev.source}\n${curr.source}`,
85
+ source: prev.source && curr.source ? `${prev.source}\n${curr.source}` : "'",
86
86
  imports: [...(prev.imports || []), ...(curr.imports || [])],
87
87
  exports: [...(prev.exports || []), ...(curr.exports || [])],
88
88
  }
@@ -4,6 +4,7 @@ import { definePlugin } from '../../plugin.ts'
4
4
  import { isPromise } from '../../utils/isPromise.ts'
5
5
  import { Queue } from '../../utils/Queue.ts'
6
6
  import { FileManager } from '../fileManager/FileManager.ts'
7
+ import type { File } from '../fileManager/types.ts'
7
8
  import { ParallelPluginError } from './ParallelPluginError.ts'
8
9
  import { PluginError } from './PluginError.ts'
9
10
 
@@ -29,7 +30,7 @@ const hookNames: {
29
30
  }
30
31
  export const hooks = Object.keys(hookNames) as [PluginLifecycleHooks]
31
32
 
32
- type Options = { task: QueueTask; onExecute?: OnExecute<PluginLifecycleHooks> }
33
+ type Options = { task: QueueTask<File>; onExecute?: OnExecute<PluginLifecycleHooks> }
33
34
 
34
35
  export class PluginManager {
35
36
  public plugins: KubbPlugin[]
@@ -47,7 +48,7 @@ export class PluginManager {
47
48
  public executed: Executer[] = []
48
49
 
49
50
  constructor(config: KubbConfig, options: Options) {
50
- this.onExecute = options.onExecute
51
+ this.onExecute = options.onExecute?.bind(this)
51
52
  this.queue = new Queue(10)
52
53
 
53
54
  this.fileManager = new FileManager({ task: options.task, queue: this.queue })
@@ -257,6 +258,7 @@ export class PluginManager {
257
258
  const errors = results.filter((result) => result.status === 'rejected').map((result) => (result as PromiseRejectedResult).reason) as PluginError[]
258
259
 
259
260
  if (errors.length) {
261
+ console.log(errors)
260
262
  throw new ParallelPluginError('Error', { errors, pluginManager: this })
261
263
  }
262
264
 
@@ -332,7 +334,7 @@ export class PluginManager {
332
334
  }
333
335
 
334
336
  private addExecuter(executer: Executer | undefined) {
335
- this.onExecute?.call(this, executer)
337
+ this.onExecute?.call(this, executer, this)
336
338
 
337
339
  if (executer) {
338
340
  this.executed.push(executer)
@@ -15,7 +15,11 @@ export type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
15
15
  plugin: KubbPlugin
16
16
  }
17
17
 
18
- export type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (this: PluginManager, executer: Executer<H> | undefined) => void
18
+ export type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (
19
+ this: PluginManager,
20
+ executer: Executer<H> | undefined,
21
+ pluginManager: PluginManager
22
+ ) => void
19
23
 
20
24
  export type ParseResult<H extends PluginLifecycleHooks> = PluginLifecycle[H]
21
25
 
package/src/plugin.ts CHANGED
@@ -64,7 +64,8 @@ export const definePlugin = createPlugin<CorePluginOptions>((options) => {
64
64
  }
65
65
  return 0
66
66
  })
67
- const pluginName = plugins?.[0].name
67
+
68
+ const pluginName = plugins?.[0]?.name
68
69
 
69
70
  return Promise.all(
70
71
  files.map((file) => {
@@ -1,5 +1,5 @@
1
1
  export type QueueTask<T = unknown> = {
2
- (...args: unknown[]): Promise<T>
2
+ (...args: unknown[]): Promise<T> | Promise<void>
3
3
  }
4
4
 
5
5
  interface QueueItem {
@@ -1,33 +1,33 @@
1
- export interface Cache<T extends object = object> {
2
- delete(id: keyof T): boolean
3
- get(id: keyof T): T[keyof T] | null
4
- has(id: keyof T): boolean
5
- set(id: keyof T, value: unknown): void
1
+ export interface Cache<TStore extends object = object> {
2
+ delete(id: keyof TStore): boolean
3
+ get(id: keyof TStore): TStore[keyof TStore] | null
4
+ has(id: keyof TStore): boolean
5
+ set(id: keyof TStore, value: unknown): void
6
6
  }
7
7
 
8
- export function createPluginCache<T extends Record<string, [number, unknown]>>(cache: T): Cache<T> {
8
+ export function createPluginCache<TStore extends Record<string, [number, unknown]>>(Store: TStore): Cache<TStore> {
9
9
  return {
10
- delete(id: keyof T) {
11
- return delete cache[id]
10
+ set(id, value): void {
11
+ Store[id] = [0, value] as TStore[keyof TStore]
12
12
  },
13
- get(id) {
14
- const item = cache[id]
13
+ get(id): TStore[keyof TStore] | null {
14
+ const item = Store[id]
15
15
  if (!item) {
16
16
  return null
17
17
  }
18
18
  item[0] = 0
19
- return item[1] as T[keyof T]
19
+ return item[1] as TStore[keyof TStore]
20
20
  },
21
- has(id) {
22
- const item = cache[id]
21
+ has(id): boolean {
22
+ const item = Store[id]
23
23
  if (!item) {
24
24
  return false
25
25
  }
26
26
  item[0] = 0
27
27
  return true
28
28
  },
29
- set(id, value) {
30
- cache[id] = [0, value] as T[keyof T]
29
+ delete(id: keyof TStore): boolean {
30
+ return delete Store[id]
31
31
  },
32
32
  }
33
33
  }
@@ -16,3 +16,4 @@ export * from './TreeNode.ts'
16
16
  export * from './transformReservedWord.ts'
17
17
  export * from './getStackTrace.ts'
18
18
  export * from './uniqueId.ts'
19
+ export * from './throttle.ts'
@@ -0,0 +1,30 @@
1
+ export const throttle = <R, A extends any[]>(fn: (...args: A) => R, delay: number): [(...args: A) => R | undefined, () => void] => {
2
+ let wait = false
3
+ let timeout: NodeJS.Timeout
4
+ let cancelled = false
5
+
6
+ return [
7
+ (...args: A) => {
8
+ if (cancelled) {
9
+ return undefined
10
+ }
11
+ if (wait) {
12
+ return undefined
13
+ }
14
+
15
+ const val = fn(...args)
16
+
17
+ wait = true
18
+
19
+ timeout = setTimeout(() => {
20
+ wait = false
21
+ }, delay)
22
+
23
+ return val
24
+ },
25
+ () => {
26
+ cancelled = true
27
+ clearTimeout(timeout)
28
+ },
29
+ ]
30
+ }