@kubb/cli 4.12.10 → 4.12.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.
@@ -6,6 +6,7 @@ import { formatHrtime, formatMs } from '@kubb/core/utils'
6
6
  import { execa } from 'execa'
7
7
  import { default as gradientString } from 'gradient-string'
8
8
  import pc from 'picocolors'
9
+ import { formatMsWithColor } from '../utils/formatMsWithColor.ts'
9
10
  import { getSummary } from '../utils/getSummary.ts'
10
11
  import { ClackWritable } from '../utils/Writables.ts'
11
12
 
@@ -261,9 +262,9 @@ Run \`npm install -g @kubb/cli\` to update`,
261
262
  state.failedPlugins++
262
263
  }
263
264
 
264
- const durationStr = formatMs(duration)
265
+ const durationStr = formatMsWithColor(duration)
265
266
  const text = getMessage(
266
- success ? `${pc.bold(plugin.name)} completed in ${pc.green(durationStr)}` : `${pc.bold(plugin.name)} failed in ${pc.red(durationStr)}`,
267
+ success ? `${pc.bold(plugin.name)} completed in ${durationStr}` : `${pc.bold(plugin.name)} failed in ${pc.red(formatMs(duration))}`,
267
268
  )
268
269
 
269
270
  active.progressBar.stop(text)
@@ -1,4 +1,4 @@
1
- import { resolve } from 'node:path'
1
+ import { relative, resolve } from 'node:path'
2
2
  import { defineLogger } from '@kubb/core'
3
3
  import { write } from '@kubb/core/fs'
4
4
  import { formatMs } from '@kubb/core/utils'
@@ -31,7 +31,7 @@ export const fileSystemLogger = defineLogger({
31
31
 
32
32
  async function writeLogs(name?: string) {
33
33
  if (state.cachedLogs.size === 0) {
34
- return
34
+ return []
35
35
  }
36
36
 
37
37
  const files: Record<string, string[]> = {}
@@ -55,6 +55,8 @@ export const fileSystemLogger = defineLogger({
55
55
  return write(fileName, logs.join('\n\n'))
56
56
  }),
57
57
  )
58
+
59
+ return Object.keys(files)
58
60
  }
59
61
 
60
62
  context.on('info', (message, info) => {
@@ -124,10 +126,18 @@ export const fileSystemLogger = defineLogger({
124
126
  })
125
127
 
126
128
  context.on('generation:end', async (config) => {
127
- await writeLogs(config.name)
129
+ const writtenFilePaths = await writeLogs(config.name)
130
+ if (writtenFilePaths.length > 0) {
131
+ const files = writtenFilePaths.map((f) => relative(process.cwd(), f))
132
+ await context.emit('info', 'Debug files written to:', files.join(', '))
133
+ }
128
134
  reset()
129
135
  })
130
136
 
137
+ context.on('lifecycle:end', async () => {
138
+ // lifecycle:end handler can be used for cleanup if needed in the future
139
+ })
140
+
131
141
  // Fallback: Write logs on process exit to handle crashes
132
142
  const exitHandler = () => {
133
143
  // Synchronous write on exit - best effort
@@ -2,6 +2,7 @@ import { type Config, defineLogger, LogLevel } from '@kubb/core'
2
2
  import { formatHrtime, formatMs } from '@kubb/core/utils'
3
3
  import { execa } from 'execa'
4
4
  import pc from 'picocolors'
5
+ import { formatMsWithColor } from '../utils/formatMsWithColor.ts'
5
6
 
6
7
  /**
7
8
  * GitHub Actions adapter for CI environments
@@ -189,9 +190,9 @@ export const githubActionsLogger = defineLogger({
189
190
  state.failedPlugins++
190
191
  }
191
192
 
192
- const durationStr = formatMs(duration)
193
+ const durationStr = formatMsWithColor(duration)
193
194
  const text = getMessage(
194
- success ? `${pc.bold(plugin.name)} completed in ${pc.green(durationStr)}` : `${pc.bold(plugin.name)} failed in ${pc.red(durationStr)}`,
195
+ success ? `${pc.bold(plugin.name)} completed in ${durationStr}` : `${pc.bold(plugin.name)} failed in ${pc.red(formatMs(duration))}`,
195
196
  )
196
197
 
197
198
  console.log(text)
@@ -1,4 +1,3 @@
1
- import { createHash } from 'node:crypto'
2
1
  import path from 'node:path'
3
2
  import process from 'node:process'
4
3
  import { type Config, type KubbEvents, LogLevel, safeBuild, setup } from '@kubb/core'
@@ -13,14 +12,13 @@ type GenerateProps = {
13
12
  logLevel: number
14
13
  }
15
14
 
16
- export async function generate({ input, config, events, logLevel }: GenerateProps): Promise<void> {
17
- const { root = process.cwd(), ...userConfig } = config
15
+ export async function generate({ input, config: userConfig, events, logLevel }: GenerateProps): Promise<void> {
18
16
  const inputPath = input ?? ('path' in userConfig.input ? userConfig.input.path : undefined)
19
17
  const hrStart = process.hrtime()
20
18
 
21
- const definedConfig: Config = {
22
- root,
19
+ const config: Config = {
23
20
  ...userConfig,
21
+ root: userConfig.root || process.cwd(),
24
22
  input: inputPath
25
23
  ? {
26
24
  ...userConfig.input,
@@ -38,12 +36,12 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
38
36
  },
39
37
  }
40
38
 
41
- await events.emit('generation:start', definedConfig)
39
+ await events.emit('generation:start', config)
42
40
 
43
41
  await events.emit('info', config.name ? `Setup generation ${pc.bold(config.name)}` : 'Setup generation', inputPath)
44
42
 
45
43
  const { fabric, pluginManager } = await setup({
46
- config: definedConfig,
44
+ config,
47
45
  events,
48
46
  })
49
47
 
@@ -51,7 +49,7 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
51
49
 
52
50
  const { files, failedPlugins, pluginTimings, error } = await safeBuild(
53
51
  {
54
- config: definedConfig,
52
+ config,
55
53
  events,
56
54
  },
57
55
  { pluginManager, fabric, events },
@@ -75,9 +73,9 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
75
73
  events.emit('error', err)
76
74
  })
77
75
 
78
- await events.emit('generation:end', definedConfig)
76
+ await events.emit('generation:end', config)
79
77
 
80
- await events.emit('generation:summary', definedConfig, {
78
+ await events.emit('generation:summary', config, {
81
79
  failedPlugins,
82
80
  filesCreated: files.length,
83
81
  status: failedPlugins.size > 0 || error ? 'failed' : 'success',
@@ -89,7 +87,7 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
89
87
  }
90
88
 
91
89
  await events.emit('success', 'Generation successfully', inputPath)
92
- await events.emit('generation:end', definedConfig)
90
+ await events.emit('generation:end', config)
93
91
 
94
92
  // formatting
95
93
  if (config.output.format) {
@@ -99,7 +97,7 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
99
97
  'info',
100
98
  [
101
99
  `Formatting with ${pc.dim(config.output.format as string)}`,
102
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
100
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
103
101
  ]
104
102
  .filter(Boolean)
105
103
  .join(' '),
@@ -107,26 +105,22 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
107
105
 
108
106
  if (config.output.format === 'prettier') {
109
107
  try {
110
- const hookId = createHash('sha256').update([config.name, config.output.format].filter(Boolean).join('-')).digest('hex')
111
108
  await events.emit('hook:start', {
112
- id: hookId,
113
109
  command: 'prettier',
114
- args: ['--ignore-unknown', '--write', path.resolve(definedConfig.root, definedConfig.output.path)],
110
+ args: ['--ignore-unknown', '--write', path.resolve(config.root, config.output.path)],
115
111
  })
116
112
 
117
- await events.on('hook:end', async ({ id }) => {
118
- if (id === hookId) {
119
- await events.emit(
120
- 'success',
121
- [
122
- `Formatting with ${pc.dim(config.output.format as string)}`,
123
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
124
- 'successfully',
125
- ]
126
- .filter(Boolean)
127
- .join(' '),
128
- )
129
- }
113
+ await events.onOnce('hook:end', async () => {
114
+ await events.emit(
115
+ 'success',
116
+ [
117
+ `Formatting with ${pc.dim(config.output.format as string)}`,
118
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
119
+ 'successfully',
120
+ ]
121
+ .filter(Boolean)
122
+ .join(' '),
123
+ )
130
124
  })
131
125
  } catch (caughtError) {
132
126
  await events.emit('error', caughtError as Error)
@@ -137,26 +131,22 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
137
131
 
138
132
  if (config.output.format === 'biome') {
139
133
  try {
140
- const hookId = createHash('sha256').update([config.name, config.output.format].filter(Boolean).join('-')).digest('hex')
141
134
  await events.emit('hook:start', {
142
- id: hookId,
143
135
  command: 'biome',
144
- args: ['format', '--write', path.resolve(definedConfig.root, definedConfig.output.path)],
136
+ args: ['format', '--write', path.resolve(config.root, config.output.path)],
145
137
  })
146
138
 
147
- await events.on('hook:end', async ({ id }) => {
148
- if (id === hookId) {
149
- await events.emit(
150
- 'success',
151
- [
152
- `Formatting with ${pc.dim(config.output.format as string)}`,
153
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
154
- 'successfully',
155
- ]
156
- .filter(Boolean)
157
- .join(' '),
158
- )
159
- }
139
+ await events.onOnce('hook:end', async () => {
140
+ await events.emit(
141
+ 'success',
142
+ [
143
+ `Formatting with ${pc.dim(config.output.format as string)}`,
144
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
145
+ 'successfully',
146
+ ]
147
+ .filter(Boolean)
148
+ .join(' '),
149
+ )
160
150
  })
161
151
  } catch (caughtError) {
162
152
  const error = new Error('Biome not found')
@@ -176,7 +166,7 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
176
166
  'info',
177
167
  [
178
168
  `Linting with ${pc.dim(config.output.lint as string)}`,
179
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
169
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
180
170
  ]
181
171
  .filter(Boolean)
182
172
  .join(' '),
@@ -184,26 +174,22 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
184
174
 
185
175
  if (config.output.lint === 'eslint') {
186
176
  try {
187
- const hookId = createHash('sha256').update([config.name, config.output.lint].filter(Boolean).join('-')).digest('hex')
188
177
  await events.emit('hook:start', {
189
- id: hookId,
190
178
  command: 'eslint',
191
- args: [path.resolve(definedConfig.root, definedConfig.output.path), '--fix'],
179
+ args: [path.resolve(config.root, config.output.path), '--fix'],
192
180
  })
193
181
 
194
- await events.on('hook:end', async ({ id }) => {
195
- if (id === hookId) {
196
- await events.emit(
197
- 'success',
198
- [
199
- `Linted with ${pc.dim(config.output.lint as string)}`,
200
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
201
- 'successfully',
202
- ]
203
- .filter(Boolean)
204
- .join(' '),
205
- )
206
- }
182
+ await events.onOnce('hook:end', async () => {
183
+ await events.emit(
184
+ 'success',
185
+ [
186
+ `Linted with ${pc.dim(config.output.lint as string)}`,
187
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
188
+ 'successfully',
189
+ ]
190
+ .filter(Boolean)
191
+ .join(' '),
192
+ )
207
193
  })
208
194
  } catch (caughtError) {
209
195
  const error = new Error('Eslint not found')
@@ -214,26 +200,22 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
214
200
 
215
201
  if (config.output.lint === 'biome') {
216
202
  try {
217
- const hookId = createHash('sha256').update([config.name, config.output.lint].filter(Boolean).join('-')).digest('hex')
218
203
  await events.emit('hook:start', {
219
- id: hookId,
220
204
  command: 'biome',
221
- args: ['lint', '--fix', path.resolve(definedConfig.root, definedConfig.output.path)],
205
+ args: ['lint', '--fix', path.resolve(config.root, config.output.path)],
222
206
  })
223
207
 
224
- await events.on('hook:end', async ({ id }) => {
225
- if (id === hookId) {
226
- await events.emit(
227
- 'success',
228
- [
229
- `Linted with ${pc.dim(config.output.lint as string)}`,
230
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
231
- 'successfully',
232
- ]
233
- .filter(Boolean)
234
- .join(' '),
235
- )
236
- }
208
+ await events.onOnce('hook:end', async () => {
209
+ await events.emit(
210
+ 'success',
211
+ [
212
+ `Linted with ${pc.dim(config.output.lint as string)}`,
213
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
214
+ 'successfully',
215
+ ]
216
+ .filter(Boolean)
217
+ .join(' '),
218
+ )
237
219
  })
238
220
  } catch (caughtError) {
239
221
  const error = new Error('Biome not found')
@@ -244,26 +226,22 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
244
226
 
245
227
  if (config.output.lint === 'oxlint') {
246
228
  try {
247
- const hookId = createHash('sha256').update([config.name, config.output.lint].filter(Boolean).join('-')).digest('hex')
248
229
  await events.emit('hook:start', {
249
- id: hookId,
250
230
  command: 'oxlint',
251
- args: ['--fix', path.resolve(definedConfig.root, definedConfig.output.path)],
231
+ args: ['--fix', path.resolve(config.root, config.output.path)],
252
232
  })
253
233
 
254
- await events.on('hook:end', async ({ id }) => {
255
- if (id === hookId) {
256
- await events.emit(
257
- 'success',
258
- [
259
- `Linted with ${pc.dim(config.output.lint as string)}`,
260
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(definedConfig.root, definedConfig.output.path))}` : undefined,
261
- 'successfully',
262
- ]
263
- .filter(Boolean)
264
- .join(' '),
265
- )
266
- }
234
+ await events.onOnce('hook:end', async () => {
235
+ await events.emit(
236
+ 'success',
237
+ [
238
+ `Linted with ${pc.dim(config.output.lint as string)}`,
239
+ logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : undefined,
240
+ 'successfully',
241
+ ]
242
+ .filter(Boolean)
243
+ .join(' '),
244
+ )
267
245
  })
268
246
  } catch (caughtError) {
269
247
  const error = new Error('Oxlint not found')
@@ -282,7 +260,7 @@ export async function generate({ input, config, events, logLevel }: GenerateProp
282
260
  await events.emit('hooks:end')
283
261
  }
284
262
 
285
- await events.emit('generation:summary', definedConfig, {
263
+ await events.emit('generation:summary', config, {
286
264
  failedPlugins,
287
265
  filesCreated: files.length,
288
266
  status: failedPlugins.size > 0 || error ? 'failed' : 'success',
@@ -1,4 +1,3 @@
1
- import { createHash } from 'node:crypto'
2
1
  import type { Config, KubbEvents } from '@kubb/core'
3
2
  import type { AsyncEventEmitter } from '@kubb/core/utils'
4
3
 
@@ -20,14 +19,10 @@ export async function executeHooks({ hooks, events }: ExecutingHooksProps): Prom
20
19
  continue
21
20
  }
22
21
 
23
- const hookId = createHash('sha256').update(command).digest('hex')
22
+ await events.emit('hook:start', { command: cmd, args })
24
23
 
25
- await events.emit('hook:start', { id: hookId, command: cmd, args })
26
-
27
- await events.on('hook:end', async ({ id }) => {
28
- if (id === hookId) {
29
- await events.emit('success', `${pc.dim(command)} successfully executed`)
30
- }
24
+ await events.onOnce('hook:end', async () => {
25
+ await events.emit('success', `${pc.dim(command)} successfully executed`)
31
26
  })
32
27
  }
33
28
  }
@@ -0,0 +1,22 @@
1
+ import { formatMs } from '@kubb/core/utils'
2
+ import pc from 'picocolors'
3
+
4
+ /**
5
+ * Formats milliseconds with color based on duration thresholds:
6
+ * - Green: <= 500ms
7
+ * - Yellow: > 500ms and <= 1000ms
8
+ * - Red: > 1000ms
9
+ */
10
+ export function formatMsWithColor(ms: number): string {
11
+ const formatted = formatMs(ms)
12
+
13
+ if (ms <= 500) {
14
+ return pc.green(formatted)
15
+ }
16
+
17
+ if (ms <= 1000) {
18
+ return pc.yellow(formatted)
19
+ }
20
+
21
+ return pc.red(formatted)
22
+ }