@kubb/core 1.1.5 → 1.1.7

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.
@@ -1,9 +1,9 @@
1
1
  import crypto from 'node:crypto'
2
2
 
3
- import { write, read } from '../../utils/index.ts'
3
+ import { read, write } from '../../utils/index.ts'
4
4
 
5
- import type { QueueTask, Queue } from '../../utils/index.ts'
6
- import type { CacheStore, UUID, Status, File } from './types.ts'
5
+ import type { Queue, QueueTask } from '../../utils/index.ts'
6
+ import type { CacheStore, File, Status, UUID } from './types.ts'
7
7
 
8
8
  export class FileManager {
9
9
  private cache: Map<CacheStore['id'], CacheStore> = new Map()
@@ -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
 
@@ -1,12 +1,12 @@
1
1
  import pathParser from 'node:path'
2
2
 
3
- import { createImportDeclaration, createExportDeclaration, print } from '@kubb/ts-codegen'
3
+ import { createExportDeclaration, createImportDeclaration, print } from '@kubb/ts-codegen'
4
4
 
5
5
  import { TreeNode } from '../../utils/index.ts'
6
6
 
7
- import type { PathMode, TreeNodeOptions } from '../../utils/index.ts'
8
- import type { Path } from '../../types.ts'
9
7
  import type ts from 'typescript'
8
+ import type { Path } from '../../types.ts'
9
+ import type { PathMode, TreeNodeOptions } from '../../utils/index.ts'
10
10
  import type { File } from './types.ts'
11
11
 
12
12
  export function writeIndexes(root: string, options: TreeNodeOptions) {
@@ -2,18 +2,17 @@
2
2
  /* eslint-disable no-await-in-loop */
3
3
  /* eslint-disable no-restricted-syntax */
4
4
 
5
- import { PluginError } from './PluginError.ts'
6
- import { ParallelPluginError } from './ParallelPluginError.ts'
7
-
8
5
  import { definePlugin } from '../../plugin.ts'
9
- import { FileManager } from '../fileManager/FileManager.ts'
10
- import { Queue } from '../../utils/Queue.ts'
11
6
  import { isPromise } from '../../utils/isPromise.ts'
7
+ import { Queue } from '../../utils/Queue.ts'
8
+ import { FileManager } from '../fileManager/FileManager.ts'
9
+ import { ParallelPluginError } from './ParallelPluginError.ts'
10
+ import { PluginError } from './PluginError.ts'
12
11
 
13
- import type { QueueTask } from '../../utils/Queue.ts'
14
- import type { Argument0, Strategy, Executer, OnExecute, ParseResult, SafeParseResult } from './types.ts'
15
- import type { KubbConfig, KubbPlugin, PluginLifecycleHooks, PluginLifecycle, MaybePromise, ResolvePathParams, ResolveNameParams } from '../../types.ts'
16
12
  import type { CorePluginOptions } from '../../plugin.ts'
13
+ import type { KubbConfig, KubbPlugin, MaybePromise, PluginLifecycle, PluginLifecycleHooks, ResolveNameParams, ResolvePathParams } from '../../types.ts'
14
+ import type { QueueTask } from '../../utils/Queue.ts'
15
+ import type { Argument0, Executer, OnExecute, ParseResult, SafeParseResult, Strategy } from './types.ts'
17
16
 
18
17
  // inspired by: https://github.com/rollup/rollup/blob/master/src/utils/PluginDriver.ts#
19
18
 
@@ -165,9 +164,9 @@ export class PluginManager {
165
164
 
166
165
  for (const plugin of this.getSortedPlugins(hookName)) {
167
166
  if (skipped && skipped.has(plugin)) continue
168
- promise = promise.then(async (result) => {
169
- if (result != null) {
170
- return result
167
+ promise = promise.then(async (parseResult) => {
168
+ if (parseResult?.result != null) {
169
+ return parseResult
171
170
  }
172
171
  const value = await this.execute<H>({
173
172
  strategy: 'hookFirst',
@@ -179,7 +178,7 @@ export class PluginManager {
179
178
  return Promise.resolve({
180
179
  plugin,
181
180
  result: value,
182
- } as typeof result)
181
+ } as typeof parseResult)
183
182
  })
184
183
  }
185
184
 
@@ -199,28 +198,32 @@ export class PluginManager {
199
198
  parameters: Parameters<PluginLifecycle[H]>
200
199
  skipped?: ReadonlySet<KubbPlugin> | null
201
200
  }): SafeParseResult<H> {
202
- let result: SafeParseResult<H> = null as unknown as SafeParseResult<H>
201
+ let parseResult: SafeParseResult<H> = null as unknown as SafeParseResult<H>
203
202
 
204
203
  for (const plugin of this.getSortedPlugins(hookName)) {
205
204
  if (skipped && skipped.has(plugin)) continue
206
205
 
207
- result = {
206
+ parseResult = {
208
207
  result: this.executeSync<H>({
209
208
  strategy: 'hookFirst',
210
209
  hookName,
211
210
  parameters,
212
211
  plugin,
213
212
  }),
213
+ plugin,
214
214
  } as SafeParseResult<H>
215
215
 
216
- if (result != null) {
216
+ if (parseResult?.result != null) {
217
217
  break
218
218
  }
219
219
  }
220
- return result as SafeParseResult<H>
220
+ return parseResult
221
221
  }
222
222
 
223
- // parallel
223
+ /**
224
+ *
225
+ * Parallel, runs all plugins
226
+ */
224
227
  async hookParallel<H extends PluginLifecycleHooks, TOuput = void>({
225
228
  hookName,
226
229
  parameters,
@@ -231,21 +234,21 @@ export class PluginManager {
231
234
  const parallelPromises: Promise<TOuput>[] = []
232
235
 
233
236
  for (const plugin of this.getSortedPlugins(hookName)) {
234
- if ((plugin[hookName] as { sequential?: boolean })?.sequential) {
235
- await Promise.all(parallelPromises)
236
- parallelPromises.length = 0
237
- await this.execute({
238
- strategy: 'hookParallel',
239
- hookName,
240
- parameters,
241
- plugin,
242
- })
243
- } else {
244
- const promise: Promise<TOuput> | null = this.execute({ strategy: 'hookParallel', hookName, parameters, plugin })
245
-
246
- if (promise) {
247
- parallelPromises.push(promise)
248
- }
237
+ // TODO implement sequential with `buildStart` as an object({ sequential: boolean; handler: PluginContext["buildStart"] })
238
+ // if ((plugin[hookName] as { sequential?: boolean })?.sequential) {
239
+ // await Promise.all(parallelPromises)
240
+ // parallelPromises.length = 0
241
+ // await this.execute({
242
+ // strategy: 'hookParallel',
243
+ // hookName,
244
+ // parameters,
245
+ // plugin,
246
+ // })
247
+ // }
248
+ const promise: Promise<TOuput> | null = this.execute({ strategy: 'hookParallel', hookName, parameters, plugin })
249
+
250
+ if (promise) {
251
+ parallelPromises.push(promise)
249
252
  }
250
253
  }
251
254
  const results = await Promise.allSettled(parallelPromises)
@@ -258,7 +261,10 @@ export class PluginManager {
258
261
  return results.filter((result) => result.status === 'fulfilled').map((result) => (result as PromiseFulfilledResult<Awaited<TOuput>>).value)
259
262
  }
260
263
 
261
- // chains, reduces returned value, handling the reduced value as the first hook argument
264
+ /**
265
+ *
266
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
267
+ */
262
268
  hookReduceArg0<H extends PluginLifecycleHooks>({
263
269
  hookName,
264
270
  parameters,
@@ -273,11 +279,11 @@ export class PluginManager {
273
279
  let promise: Promise<Argument0<H>> = Promise.resolve(argument0)
274
280
  for (const plugin of this.getSortedPlugins(hookName)) {
275
281
  promise = promise
276
- .then((argument0) => {
282
+ .then((arg0) => {
277
283
  const value = this.execute({
278
284
  strategy: 'hookReduceArg0',
279
285
  hookName,
280
- parameters: [argument0, ...rest] as Parameters<PluginLifecycle[H]>,
286
+ parameters: [arg0, ...rest] as Parameters<PluginLifecycle[H]>,
281
287
  plugin,
282
288
  })
283
289
  return value
@@ -287,8 +293,9 @@ export class PluginManager {
287
293
  return promise
288
294
  }
289
295
 
290
- // chains
291
-
296
+ /**
297
+ * Chains plugins
298
+ */
292
299
  hookSeq<H extends PluginLifecycleHooks>({ hookName, parameters }: { hookName: H; parameters?: Parameters<PluginLifecycle[H]> }) {
293
300
  let promise: Promise<void | null> = Promise.resolve()
294
301
  for (const plugin of this.getSortedPlugins(hookName)) {
@@ -310,7 +317,7 @@ export class PluginManager {
310
317
  return plugins
311
318
  }
312
319
 
313
- private getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin {
320
+ public getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin {
314
321
  const plugins = [...this.plugins]
315
322
 
316
323
  const pluginByPluginName = plugins.find((item) => item.name === pluginName && item[hookName])
@@ -1,5 +1,6 @@
1
- import type { PluginManager } from './PluginManager.ts'
2
1
  import type { KubbPlugin, PluginLifecycle, PluginLifecycleHooks } from '../../types.ts'
2
+ import type { PluginManager } from './PluginManager.ts'
3
+
3
4
  /**
4
5
  * Get the type of the first argument in a function.
5
6
  * @example Arg0<(a: string, b: number) => void> -> string
package/src/plugin.ts CHANGED
@@ -2,9 +2,9 @@ import pathParser from 'node:path'
2
2
 
3
3
  import { createPluginCache, getStackTrace, transformReservedWord } from './utils/index.ts'
4
4
 
5
- import type { Executer } from './managers/index.ts'
6
5
  import type { FileManager } from './managers/fileManager/FileManager.ts'
7
- import type { PluginContext, KubbPlugin, PluginFactoryOptions, PluginLifecycleHooks } from './types.ts'
6
+ import type { Executer } from './managers/index.ts'
7
+ import type { KubbPlugin, PluginContext, PluginFactoryOptions, PluginLifecycleHooks } from './types.ts'
8
8
 
9
9
  type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (
10
10
  options: T['options']
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-empty-interface */
2
+ import type { File, FileManager } from './managers/fileManager/index.ts'
2
3
  import type { PluginManager, SafeParseResult } from './managers/index.ts'
3
- import type { FileManager, File } from './managers/fileManager/index.ts'
4
4
  import type { Cache } from './utils/cache.ts'
5
5
 
6
6
  export interface Register {}
@@ -9,7 +9,7 @@ export function getStackTrace(belowFn?: Function): NodeJS.CallSite[] {
9
9
  Error.prepareStackTrace = function prepareStackTrace(dummyObject, v8StackTrace) {
10
10
  return v8StackTrace
11
11
  }
12
- Error.captureStackTrace(dummyObject, belowFn || getStackTrace)
12
+ Error.captureStackTrace(dummyObject as object, belowFn || getStackTrace)
13
13
 
14
14
  const v8StackTrace = dummyObject.stack
15
15
  Error.prepareStackTrace = v8Handler
package/src/utils/read.ts CHANGED
@@ -1,5 +1,5 @@
1
- import pathParser from 'node:path'
2
1
  import { promises as fs } from 'node:fs'
2
+ import pathParser from 'node:path'
3
3
 
4
4
  function slash(path: string) {
5
5
  const isExtendedLengthPath = /^\\\\\?\\/.test(path)