@kubb/core 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/{chunk-OYDUQYAK.cjs → chunk-3J25BT5A.cjs} +15 -41
  2. package/dist/chunk-3J25BT5A.cjs.map +1 -0
  3. package/dist/{chunk-HI4PB2AG.js → chunk-DJBEBMX6.js} +10 -36
  4. package/dist/chunk-DJBEBMX6.js.map +1 -0
  5. package/dist/chunk-GLL5G5KY.js +30 -0
  6. package/dist/chunk-GLL5G5KY.js.map +1 -0
  7. package/dist/{chunk-BRWCAKW5.cjs → chunk-LDKM6T7X.cjs} +4 -4
  8. package/dist/{chunk-65OHDKQD.cjs → chunk-N7XIQXXQ.cjs} +28 -5
  9. package/dist/chunk-N7XIQXXQ.cjs.map +1 -0
  10. package/dist/{chunk-JSJ3NRBQ.cjs → chunk-SSD2TXQZ.cjs} +51 -32
  11. package/dist/chunk-SSD2TXQZ.cjs.map +1 -0
  12. package/dist/{chunk-37XMXECQ.js → chunk-STP2UI5Q.js} +2 -2
  13. package/dist/{chunk-V6AVJEQC.js → chunk-W3NDP3K4.js} +52 -34
  14. package/dist/chunk-W3NDP3K4.js.map +1 -0
  15. package/dist/index.cjs +47 -47
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +54 -56
  18. package/dist/index.d.ts +54 -56
  19. package/dist/index.js +19 -20
  20. package/dist/index.js.map +1 -1
  21. package/dist/logger-wryz32Ex.d.cts +44 -0
  22. package/dist/logger-wryz32Ex.d.ts +44 -0
  23. package/dist/logger.cjs +5 -5
  24. package/dist/logger.d.cts +2 -32
  25. package/dist/logger.d.ts +2 -32
  26. package/dist/logger.js +1 -1
  27. package/dist/mocks.cjs +7 -13
  28. package/dist/mocks.cjs.map +1 -1
  29. package/dist/mocks.d.cts +1 -1
  30. package/dist/mocks.d.ts +1 -1
  31. package/dist/mocks.js +6 -12
  32. package/dist/mocks.js.map +1 -1
  33. package/dist/transformers.cjs +10 -5
  34. package/dist/transformers.cjs.map +1 -1
  35. package/dist/transformers.d.cts +13 -3
  36. package/dist/transformers.d.ts +13 -3
  37. package/dist/transformers.js +4 -3
  38. package/dist/transformers.js.map +1 -1
  39. package/dist/utils.cjs +4 -4
  40. package/dist/utils.js +2 -2
  41. package/package.json +3 -3
  42. package/src/FileManager.ts +2 -2
  43. package/src/PluginManager.ts +39 -39
  44. package/src/build.ts +11 -13
  45. package/src/config.ts +5 -5
  46. package/src/index.ts +3 -15
  47. package/src/kubb.ts +11 -0
  48. package/src/logger.ts +37 -37
  49. package/src/plugin.ts +5 -5
  50. package/src/transformers/casing.ts +33 -5
  51. package/src/transformers/index.ts +3 -2
  52. package/src/types.ts +23 -23
  53. package/dist/chunk-65OHDKQD.cjs.map +0 -1
  54. package/dist/chunk-HI4PB2AG.js.map +0 -1
  55. package/dist/chunk-JSJ3NRBQ.cjs.map +0 -1
  56. package/dist/chunk-MASAFU3G.js +0 -15
  57. package/dist/chunk-MASAFU3G.js.map +0 -1
  58. package/dist/chunk-OYDUQYAK.cjs.map +0 -1
  59. package/dist/chunk-V6AVJEQC.js.map +0 -1
  60. /package/dist/{chunk-37XMXECQ.js.map → chunk-LDKM6T7X.cjs.map} +0 -0
  61. /package/dist/{chunk-BRWCAKW5.cjs.map → chunk-STP2UI5Q.js.map} +0 -0
@@ -17,18 +17,18 @@ import type { KubbFile } from './FileManager.ts'
17
17
  import type { Logger } from './logger.ts'
18
18
  import type { CorePluginOptions } from './plugin.ts'
19
19
  import type {
20
+ Config,
20
21
  GetPluginFactoryOptions,
21
- KubbConfig,
22
- KubbPlugin,
23
- KubbPluginWithLifeCycle,
24
- KubbUserPlugin,
25
- KubbUserPluginWithLifeCycle,
22
+ Plugin,
26
23
  PluginFactoryOptions,
27
24
  PluginLifecycle,
28
25
  PluginLifecycleHooks,
29
26
  PluginParameter,
27
+ PluginWithLifeCycle,
30
28
  ResolveNameParams,
31
29
  ResolvePathParams,
30
+ UserPlugin,
31
+ UserPluginWithLifeCycle,
32
32
  } from './types.ts'
33
33
 
34
34
  type RequiredPluginLifecycle = Required<PluginLifecycle>
@@ -44,7 +44,7 @@ type Strategy = 'hookFirst' | 'hookForPlugin' | 'hookParallel' | 'hookReduceArg0
44
44
  type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
45
45
  strategy: Strategy
46
46
  hookName: H
47
- plugin: KubbPlugin
47
+ plugin: Plugin
48
48
  parameters?: unknown[] | undefined
49
49
  output?: unknown
50
50
  }
@@ -53,7 +53,7 @@ type ParseResult<H extends PluginLifecycleHooks> = RequiredPluginLifecycle[H]
53
53
 
54
54
  type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseResult<H>>> = {
55
55
  result: Result
56
- plugin: KubbPlugin
56
+ plugin: Plugin
57
57
  }
58
58
 
59
59
  // inspired by: https://github.com/rollup/rollup/blob/master/src/utils/PluginDriver.ts#
@@ -74,22 +74,22 @@ type Events = {
74
74
  }
75
75
 
76
76
  export class PluginManager {
77
- readonly plugins: KubbPluginWithLifeCycle[]
77
+ readonly plugins: PluginWithLifeCycle[]
78
78
  readonly fileManager: FileManager
79
79
  readonly events: EventEmitter<Events> = new EventEmitter()
80
80
 
81
- readonly config: KubbConfig
81
+ readonly config: Config
82
82
 
83
83
  readonly executed: Array<Executer> = []
84
84
  readonly logger: Logger
85
- readonly #core: KubbPlugin<CorePluginOptions>
85
+ readonly #core: Plugin<CorePluginOptions>
86
86
 
87
87
  readonly #usedPluginNames: Record<string, number> = {}
88
88
  readonly #promiseManager: PromiseManager
89
89
 
90
90
  readonly queue: PQueue
91
91
 
92
- constructor(config: KubbConfig, options: Options) {
92
+ constructor(config: Config, options: Options) {
93
93
  this.config = config
94
94
  this.logger = options.logger
95
95
  this.queue = new PQueue({ concurrency: 1 })
@@ -109,10 +109,10 @@ export class PluginManager {
109
109
  })
110
110
 
111
111
  // call core.api.call with empty context so we can transform `api()` to `api: {}`
112
- this.#core = this.#parse(core as unknown as KubbUserPlugin, this as any, core.api.call(null as any)) as KubbPlugin<CorePluginOptions>
112
+ this.#core = this.#parse(core as unknown as UserPlugin, this as any, core.api.call(null as any)) as Plugin<CorePluginOptions>
113
113
 
114
114
  this.plugins = [this.#core, ...plugins].map((plugin) => {
115
- return this.#parse(plugin as KubbUserPlugin, this, this.#core.api)
115
+ return this.#parse(plugin as UserPlugin, this, this.#core.api)
116
116
  })
117
117
 
118
118
  return this
@@ -127,11 +127,11 @@ export class PluginManager {
127
127
  })
128
128
 
129
129
  if (paths && paths?.length > 1 && this.logger.logLevel === LogLevel.debug) {
130
- this.logger.debug(
130
+ this.logger.emit('debug', [
131
131
  `Cannot return a path where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough\n\nPaths: ${
132
132
  JSON.stringify(paths, undefined, 2)
133
133
  }\n\nFalling back on the first item.\n`,
134
- )
134
+ ])
135
135
  }
136
136
 
137
137
  return paths?.at(0)
@@ -150,11 +150,11 @@ export class PluginManager {
150
150
  })
151
151
 
152
152
  if (names && names?.length > 1 && this.logger.logLevel === LogLevel.debug) {
153
- this.logger.debug(
153
+ this.logger.emit('debug', [
154
154
  `Cannot return a name where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough\n\nNames: ${
155
155
  JSON.stringify(names, undefined, 2)
156
156
  }\n\nFalling back on the first item.\n`,
157
- )
157
+ ])
158
158
  }
159
159
 
160
160
  return transformReservedWord(names?.at(0) || params.name)
@@ -183,7 +183,7 @@ export class PluginManager {
183
183
  hookName,
184
184
  parameters,
185
185
  }: {
186
- pluginKey: KubbPlugin['key']
186
+ pluginKey: Plugin['key']
187
187
  hookName: H
188
188
  parameters: PluginParameter<H>
189
189
  }): Promise<Array<ReturnType<ParseResult<H>> | null>> | null {
@@ -211,7 +211,7 @@ export class PluginManager {
211
211
  hookName,
212
212
  parameters,
213
213
  }: {
214
- pluginKey: KubbPlugin['key']
214
+ pluginKey: Plugin['key']
215
215
  hookName: H
216
216
  parameters: PluginParameter<H>
217
217
  }): Array<ReturnType<ParseResult<H>>> | null {
@@ -239,7 +239,7 @@ export class PluginManager {
239
239
  }: {
240
240
  hookName: H
241
241
  parameters: PluginParameter<H>
242
- skipped?: ReadonlySet<KubbPlugin> | null
242
+ skipped?: ReadonlySet<Plugin> | null
243
243
  }): Promise<SafeParseResult<H>> {
244
244
  const promises = this.#getSortedPlugins().filter(plugin => {
245
245
  return skipped ? skipped.has(plugin) : true
@@ -274,7 +274,7 @@ export class PluginManager {
274
274
  }: {
275
275
  hookName: H
276
276
  parameters: PluginParameter<H>
277
- skipped?: ReadonlySet<KubbPlugin> | null
277
+ skipped?: ReadonlySet<Plugin> | null
278
278
  }): SafeParseResult<H> {
279
279
  let parseResult: SafeParseResult<H> = null as unknown as SafeParseResult<H>
280
280
 
@@ -338,7 +338,7 @@ export class PluginManager {
338
338
  }: {
339
339
  hookName: H
340
340
  parameters: PluginParameter<H>
341
- reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: KubbPlugin) => PossiblePromise<Argument0<H> | null>
341
+ reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: Plugin) => PossiblePromise<Argument0<H> | null>
342
342
  }): Promise<Argument0<H>> {
343
343
  const [argument0, ...rest] = parameters
344
344
 
@@ -377,14 +377,14 @@ export class PluginManager {
377
377
  return this.#promiseManager.run('seq', promises)
378
378
  }
379
379
 
380
- #getSortedPlugins(hookName?: keyof PluginLifecycle): KubbPlugin[] {
380
+ #getSortedPlugins(hookName?: keyof PluginLifecycle): Plugin[] {
381
381
  const plugins = [...this.plugins].filter((plugin) => plugin.name !== 'core')
382
382
 
383
383
  if (hookName) {
384
384
  if (this.logger.logLevel === LogLevel.info) {
385
385
  const containsHookName = plugins.some((item) => item[hookName])
386
386
  if (!containsHookName) {
387
- this.logger.warn(`No hook ${hookName} found`)
387
+ this.logger.emit('warning', `No hook ${hookName} found`)
388
388
  }
389
389
  }
390
390
 
@@ -413,7 +413,7 @@ export class PluginManager {
413
413
  })
414
414
  }
415
415
 
416
- getPluginsByKey(hookName: keyof PluginLifecycle, pluginKey: KubbPlugin['key']): KubbPlugin[] {
416
+ getPluginsByKey(hookName: keyof PluginLifecycle, pluginKey: Plugin['key']): Plugin[] {
417
417
  const plugins = [...this.plugins]
418
418
  const [searchPluginName, searchIdentifier] = pluginKey
419
419
 
@@ -439,9 +439,9 @@ export class PluginManager {
439
439
 
440
440
  if (this.logger.logLevel === LogLevel.debug) {
441
441
  if (corePlugin) {
442
- this.logger.debug(`No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, falling back on the '@kubb/core' plugin`)
442
+ this.logger.emit('debug', [`No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, falling back on the '@kubb/core' plugin`])
443
443
  } else {
444
- this.logger.debug(`No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, no fallback found in the '@kubb/core' plugin`)
444
+ this.logger.emit('debug', [`No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, no fallback found in the '@kubb/core' plugin`])
445
445
  }
446
446
  }
447
447
 
@@ -474,7 +474,7 @@ export class PluginManager {
474
474
  strategy: Strategy
475
475
  hookName: H
476
476
  parameters: unknown[] | undefined
477
- plugin: KubbPluginWithLifeCycle
477
+ plugin: PluginWithLifeCycle
478
478
  }): Promise<ReturnType<ParseResult<H>> | null> | null {
479
479
  const hook = plugin[hookName]
480
480
  let output: unknown
@@ -536,7 +536,7 @@ export class PluginManager {
536
536
  strategy: Strategy
537
537
  hookName: H
538
538
  parameters: PluginParameter<H>
539
- plugin: KubbPluginWithLifeCycle
539
+ plugin: PluginWithLifeCycle
540
540
  }): ReturnType<ParseResult<H>> | null {
541
541
  const hook = plugin[hookName]
542
542
  let output: unknown
@@ -573,18 +573,18 @@ export class PluginManager {
573
573
  }
574
574
  }
575
575
 
576
- #catcher<H extends PluginLifecycleHooks>(e: Error, plugin?: KubbPlugin, hookName?: H) {
576
+ #catcher<H extends PluginLifecycleHooks>(e: Error, plugin?: Plugin, hookName?: H) {
577
577
  const text = `${e.message} (plugin: ${plugin?.name || 'unknown'}, hook: ${hookName || 'unknown'})\n`
578
578
 
579
- this.logger.error(text)
579
+ this.logger.emit('error', text)
580
580
  this.events.emit('error', e)
581
581
  }
582
582
 
583
- #parse<TPlugin extends KubbUserPluginWithLifeCycle>(
583
+ #parse<TPlugin extends UserPluginWithLifeCycle>(
584
584
  plugin: TPlugin,
585
585
  pluginManager: PluginManager,
586
586
  context: CorePluginOptions['api'] | undefined,
587
- ): KubbPlugin<GetPluginFactoryOptions<TPlugin>> {
587
+ ): Plugin<GetPluginFactoryOptions<TPlugin>> {
588
588
  const usedPluginNames = pluginManager.#usedPluginNames
589
589
 
590
590
  setUniqueName(plugin.name, usedPluginNames)
@@ -605,23 +605,23 @@ export class PluginManager {
605
605
  ...plugin,
606
606
  key,
607
607
  api,
608
- } as unknown as KubbPlugin<GetPluginFactoryOptions<TPlugin>>
608
+ } as unknown as Plugin<GetPluginFactoryOptions<TPlugin>>
609
609
  }
610
610
 
611
611
  return {
612
612
  ...plugin,
613
613
  key,
614
- } as unknown as KubbPlugin<GetPluginFactoryOptions<TPlugin>>
614
+ } as unknown as Plugin<GetPluginFactoryOptions<TPlugin>>
615
615
  }
616
616
 
617
617
  static getDependedPlugins<
618
618
  T1 extends PluginFactoryOptions,
619
619
  T2 extends PluginFactoryOptions = never,
620
620
  T3 extends PluginFactoryOptions = never,
621
- TOutput = T3 extends never ? T2 extends never ? [T1: KubbPlugin<T1>]
622
- : [T1: KubbPlugin<T1>, T2: KubbPlugin<T2>]
623
- : [T1: KubbPlugin<T1>, T2: KubbPlugin<T2>, T3: KubbPlugin<T3>],
624
- >(plugins: Array<KubbPlugin>, dependedPluginNames: string | string[]): TOutput {
621
+ TOutput = T3 extends never ? T2 extends never ? [T1: Plugin<T1>]
622
+ : [T1: Plugin<T1>, T2: Plugin<T2>]
623
+ : [T1: Plugin<T1>, T2: Plugin<T2>, T3: Plugin<T3>],
624
+ >(plugins: Array<Plugin>, dependedPluginNames: string | string[]): TOutput {
625
625
  let pluginNames: string[] = []
626
626
  if (typeof dependedPluginNames === 'string') {
627
627
  pluginNames = [dependedPluginNames]
package/src/build.ts CHANGED
@@ -11,7 +11,7 @@ import { isPromise } from './PromiseManager.ts'
11
11
 
12
12
  import type { KubbFile } from './FileManager.ts'
13
13
  import type { Logger } from './logger.ts'
14
- import type { KubbPlugin, PluginContext, PluginParameter, TransformResult } from './types.ts'
14
+ import type { Plugin, PluginContext, PluginParameter, TransformResult } from './types.ts'
15
15
 
16
16
  type BuildOptions = {
17
17
  config: PluginContext['config']
@@ -34,7 +34,7 @@ async function transformReducer(
34
34
  this: PluginContext,
35
35
  _previousCode: string,
36
36
  result: TransformResult | Promise<TransformResult>,
37
- _plugin: KubbPlugin,
37
+ _plugin: Plugin,
38
38
  ): Promise<string | null> {
39
39
  return result
40
40
  }
@@ -113,11 +113,11 @@ async function setup(options: BuildOptions): Promise<PluginManager> {
113
113
  pluginManager.on('execute', (executer) => {
114
114
  const { hookName, parameters, plugin } = executer
115
115
 
116
- if (hookName === 'writeFile' && logger.spinner) {
116
+ if (hookName === 'writeFile') {
117
117
  const [code] = parameters as PluginParameter<'writeFile'>
118
118
 
119
119
  if (logger.logLevel === LogLevel.debug) {
120
- logger.debug(`PluginKey ${c.dim(JSON.stringify(plugin.key))} \nwith source\n\n${code}`)
120
+ logger.emit('debug', [`PluginKey ${c.dim(JSON.stringify(plugin.key))} \nwith source\n\n${code}`])
121
121
  }
122
122
  }
123
123
  })
@@ -127,8 +127,8 @@ async function setup(options: BuildOptions): Promise<PluginManager> {
127
127
  return
128
128
  }
129
129
 
130
- if (logger.spinner && count === 0) {
131
- logger.spinner?.start(`💾 Writing`)
130
+ if (count === 0) {
131
+ logger.emit('start', `💾 Writing`)
132
132
  }
133
133
  })
134
134
 
@@ -169,7 +169,7 @@ async function setup(options: BuildOptions): Promise<PluginManager> {
169
169
  output,
170
170
  ].filter(Boolean)
171
171
 
172
- logger.debug(logs.join('\n'))
172
+ logger.emit('debug', logs as string[])
173
173
  }
174
174
  })
175
175
 
@@ -188,9 +188,8 @@ export async function build(options: BuildOptions): Promise<BuildOutput> {
188
188
 
189
189
  await pluginManager.hookParallel({ hookName: 'buildEnd' })
190
190
 
191
- if (logger.logLevel === LogLevel.info && logger.spinner) {
192
- logger.spinner.suffixText = ''
193
- logger.spinner.succeed(`💾 Writing completed`)
191
+ if (logger.logLevel === LogLevel.info) {
192
+ logger.emit('end', `💾 Writing completed`)
194
193
  }
195
194
 
196
195
  return { files: fileManager.files.map((file) => ({ ...file, source: FileManager.getSource(file) })), pluginManager }
@@ -209,9 +208,8 @@ export async function safeBuild(options: BuildOptions): Promise<BuildOutput> {
209
208
 
210
209
  await pluginManager.hookParallel({ hookName: 'buildEnd' })
211
210
 
212
- if (logger.logLevel === LogLevel.info && logger.spinner) {
213
- logger.spinner.suffixText = ''
214
- logger.spinner.succeed(`💾 Writing completed`)
211
+ if (logger.logLevel === LogLevel.info) {
212
+ logger.emit('end', `💾 Writing completed`)
215
213
  }
216
214
  } catch (e) {
217
215
  return { files: fileManager.files.map((file) => ({ ...file, source: FileManager.getSource(file) })), pluginManager, error: e as Error }
package/src/config.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import type { PossiblePromise } from '@kubb/types'
2
- import type { CLIOptions, InputPath, KubbConfig, KubbUserConfig } from './types.ts'
2
+ import type { CLIOptions, Config, InputPath, UserConfig } from './types.ts'
3
3
 
4
4
  /**
5
5
  * Type helper to make it easier to use kubb.config.js
6
- * accepts a direct {@link KubbConfig} object, or a function that returns it.
6
+ * accepts a direct {@link Config} object, or a function that returns it.
7
7
  * The function receives a {@link ConfigEnv} object that exposes two properties:
8
8
  */
9
9
  export function defineConfig(
10
10
  options:
11
- | PossiblePromise<KubbUserConfig | Array<KubbUserConfig>>
11
+ | PossiblePromise<UserConfig | Array<UserConfig>>
12
12
  | ((
13
13
  /** The options derived from the CLI flags */
14
14
  cliOptions: CLIOptions,
15
- ) => PossiblePromise<KubbUserConfig | Array<KubbUserConfig>>),
15
+ ) => PossiblePromise<UserConfig | Array<UserConfig>>),
16
16
  ): typeof options {
17
17
  return options
18
18
  }
19
19
 
20
- export function isInputPath(result: KubbConfig | undefined): result is KubbConfig<InputPath> {
20
+ export function isInputPath(result: Config | undefined): result is Config<InputPath> {
21
21
  return !!result && 'path' in (result as any)
22
22
  }
package/src/index.ts CHANGED
@@ -1,8 +1,4 @@
1
- import { build } from './build.ts'
2
-
3
- import type { ObjValueTuple, TupleToUnion } from '@kubb/types'
4
-
5
- export { build, safeBuild } from './build.ts'
1
+ export { build, build as default, safeBuild } from './build.ts'
6
2
  export { defineConfig, isInputPath } from './config.ts'
7
3
  export { Warning } from './errors.ts'
8
4
  export { FileManager, KubbFile } from './FileManager.ts'
@@ -12,16 +8,8 @@ export { PackageManager } from './PackageManager.ts'
12
8
  export { createPlugin, pluginName as name, pluginName } from './plugin.ts'
13
9
  export { PluginManager } from './PluginManager.ts'
14
10
  export { PromiseManager } from './PromiseManager.ts'
15
- export type * from './types.ts'
11
+ export type { CLIOptions, Config, Plugin, PluginFactoryOptions, ResolveNameParams, ResolvePathParams, UserConfig } from './types.ts'
16
12
 
17
13
  export interface _Register {}
18
- export type Plugins = _Register
19
- export type OptionsPlugins = { [K in keyof Plugins]: Plugins[K]['options'] }
20
-
21
- export type OptionsOfPlugin<K extends keyof Plugins> = Plugins[K]['options']
22
-
23
- export type PluginUnion = TupleToUnion<ObjValueTuple<OptionsPlugins>>
24
-
25
- export type Plugin = keyof Plugins
26
14
 
27
- export default build
15
+ export * as Kubb from './kubb.ts'
package/src/kubb.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { ObjValueTuple, TupleToUnion } from '@kubb/types'
2
+ import type { _Register } from './index.ts'
3
+
4
+ export type Plugins = _Register
5
+ export type OptionsPlugins = { [K in keyof Plugins]: Plugins[K]['options'] }
6
+
7
+ export type OptionsOfPlugin<K extends keyof Plugins> = Plugins[K]['options']
8
+
9
+ export type PluginUnion = TupleToUnion<ObjValueTuple<OptionsPlugins>>
10
+
11
+ export type Plugin = keyof Plugins
package/src/logger.ts CHANGED
@@ -2,6 +2,7 @@ import seedrandom from 'seedrandom'
2
2
  import c, { createColors } from 'tinyrainbow'
3
3
 
4
4
  import { writeLog } from './fs/write.ts'
5
+ import { EventEmitter } from './utils/EventEmitter.ts'
5
6
 
6
7
  import type { Ora } from 'ora'
7
8
  import type { Formatter } from 'tinyrainbow'
@@ -14,19 +15,23 @@ export const LogLevel = {
14
15
 
15
16
  export type LogLevel = keyof typeof LogLevel
16
17
 
18
+ type Events = {
19
+ start: [message: string]
20
+ end: [message: string]
21
+ error: [message: string]
22
+ warning: [message: string]
23
+ debug: [logs: string[]]
24
+ }
17
25
  export type Logger = {
18
26
  /**
19
27
  * Optional config name to show in CLI output
20
28
  */
21
29
  name?: string
22
30
  logLevel: LogLevel
23
- log: (message: string | null) => void
24
- error: (message: string | null) => void
25
- info: (message: string | null) => void
26
- warn: (message: string | null) => void
27
- debug: (message: string | null) => Promise<void>
31
+
28
32
  spinner?: Ora
29
- logs: string[]
33
+ on: EventEmitter<Events>['on']
34
+ emit: EventEmitter<Events>['emit']
30
35
  }
31
36
 
32
37
  type Props = {
@@ -36,50 +41,45 @@ type Props = {
36
41
  }
37
42
 
38
43
  export function createLogger({ logLevel, name, spinner }: Props): Logger {
39
- const logs: string[] = []
40
- const log: Logger['log'] = (message) => {
41
- if (message && spinner) {
42
- spinner.text = message
43
- logs.push(message)
44
+ const events = new EventEmitter<Events>()
45
+
46
+ events.on('start', (message) => {
47
+ if (spinner) {
48
+ spinner.start(message)
44
49
  }
45
- }
50
+ })
46
51
 
47
- const error: Logger['error'] = (message) => {
48
- if (message) {
49
- throw new Error(message || 'Something went wrong')
52
+ events.on('end', (message) => {
53
+ if (spinner) {
54
+ spinner.suffixText = ''
55
+ spinner.succeed(message)
50
56
  }
51
- }
57
+ })
52
58
 
53
- const warn: Logger['warn'] = (message) => {
54
- if (message && spinner) {
59
+ events.on('warning', (message) => {
60
+ if (spinner) {
55
61
  spinner.warn(c.yellow(message))
56
- logs.push(message)
57
62
  }
58
- }
63
+ })
59
64
 
60
- const info: Logger['warn'] = (message) => {
61
- if (message && spinner && logLevel !== LogLevel.silent) {
62
- spinner.info(message)
63
- logs.push(message)
64
- }
65
- }
65
+ events.on('error', (message) => {
66
+ throw new Error(message || 'Something went wrong')
67
+ })
66
68
 
67
- const debug: Logger['debug'] = async (message) => {
68
- if (message) {
69
- await writeLog(message)
70
- }
71
- }
69
+ events.on('debug', async (messages) => {
70
+ await writeLog(messages.join('\n'))
71
+ })
72
72
 
73
73
  const logger: Logger = {
74
74
  name,
75
75
  logLevel,
76
- log,
77
- error,
78
- warn,
79
- info,
80
- debug,
81
76
  spinner,
82
- logs,
77
+ on: (...args) => {
78
+ return events.on(...args)
79
+ },
80
+ emit: (...args) => {
81
+ return events.emit(...args)
82
+ },
83
83
  }
84
84
 
85
85
  return logger
package/src/plugin.ts CHANGED
@@ -4,12 +4,12 @@ import { createPluginCache } from './utils/cache.ts'
4
4
 
5
5
  import type { FileManager } from './FileManager.ts'
6
6
  import type { PluginManager } from './PluginManager.ts'
7
- import type { KubbPlugin, KubbUserPluginWithLifeCycle, PluginContext, PluginFactoryOptions } from './types.ts'
7
+ import type { Plugin, PluginContext, PluginFactoryOptions, UserPluginWithLifeCycle } from './types.ts'
8
8
 
9
- type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => KubbUserPluginWithLifeCycle<T>
9
+ type PluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => UserPluginWithLifeCycle<T>
10
10
 
11
- export function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: KubbPluginFactory<T>) {
12
- return (options: T['options']): ReturnType<KubbPluginFactory<T>> => {
11
+ export function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: PluginFactory<T>) {
12
+ return (options: T['options']): ReturnType<PluginFactory<T>> => {
13
13
  return factory(options)
14
14
  }
15
15
  }
@@ -21,7 +21,7 @@ type Options = {
21
21
  resolvePath: PluginContext['resolvePath']
22
22
  resolveName: PluginContext['resolveName']
23
23
  logger: PluginContext['logger']
24
- getPlugins: () => Array<KubbPlugin>
24
+ getPlugins: () => Array<Plugin>
25
25
  plugin?: PluginContext['plugin']
26
26
  }
27
27
 
@@ -1,9 +1,37 @@
1
- import { camelCase as changeCaseCamel, pascalCase as changePascalCase } from 'change-case'
1
+ import { camelCase as changeCamelCase, pascalCase as changePascalCase, pathCase as changePathCase } from 'change-case'
2
2
 
3
- export function camelCase(text: string): string {
4
- return changeCaseCamel(text, { delimiter: '', mergeAmbiguousCharacters: true })
3
+ type Options = {
4
+ /**
5
+ * When set it will replace all `.` with `/`.
6
+ */
7
+ isFile?: boolean
8
+ prefix?: string
9
+ suffix?: string
5
10
  }
6
11
 
7
- export function pascalCase(text: string): string {
8
- return changePascalCase(text, { delimiter: '', mergeAmbiguousCharacters: true })
12
+ export function camelCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {
13
+ if (isFile) {
14
+ const splitArray = text.split('.')
15
+ return splitArray.map((item, i) => i === splitArray.length - 1 ? camelCase(item, { prefix, suffix }) : camelCase(item)).join('/')
16
+ }
17
+
18
+ return changeCamelCase(`${prefix} ${text} ${suffix}`, { delimiter: '', mergeAmbiguousCharacters: true })
19
+ }
20
+
21
+ export function pascalCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {
22
+ if (isFile) {
23
+ const splitArray = text.split('.')
24
+ return splitArray.map((item, i) => i === splitArray.length - 1 ? pascalCase(item, { prefix, suffix }) : camelCase(item)).join('/')
25
+ }
26
+
27
+ return changePascalCase(`${prefix} ${text} ${suffix}`, { delimiter: '', mergeAmbiguousCharacters: true })
28
+ }
29
+
30
+ export function pathCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {
31
+ if (isFile) {
32
+ const splitArray = text.split('.')
33
+ return splitArray.map((item, i) => i === splitArray.length - 1 ? pathCase(item, { prefix, suffix }) : camelCase(item)).join('/')
34
+ }
35
+
36
+ return changePathCase(`${prefix} ${text} ${suffix}`, { delimiter: '' })
9
37
  }
@@ -1,4 +1,4 @@
1
- import { camelCase, pascalCase } from './casing.ts'
1
+ import { camelCase, pascalCase, pathCase } from './casing.ts'
2
2
  import { combineCodes } from './combineCodes.ts'
3
3
  import { createJSDocBlockText } from './createJSDocBlockText.ts'
4
4
  import { escape, jsStringEscape } from './escape.ts'
@@ -8,7 +8,7 @@ import { searchAndReplace } from './searchAndReplace.ts'
8
8
  import { transformReservedWord } from './transformReservedWord.ts'
9
9
  import { trim, trimExtName } from './trim.ts'
10
10
 
11
- export { camelCase, pascalCase } from './casing.ts'
11
+ export { camelCase, pascalCase, pathCase } from './casing.ts'
12
12
  export { combineCodes } from './combineCodes.ts'
13
13
  export { createJSDocBlockText } from './createJSDocBlockText.ts'
14
14
  export { escape, jsStringEscape } from './escape.ts'
@@ -33,4 +33,5 @@ export default {
33
33
  },
34
34
  camelCase,
35
35
  pascalCase,
36
+ pathCase,
36
37
  } as const