@kubb/core 3.0.0-alpha.3 → 3.0.0-alpha.4
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/{FileManager-Bw-FNS3q.d.cts → FileManager--scIq4y4.d.cts} +26 -18
- package/dist/{FileManager-BW--rO8q.d.ts → FileManager-CaejIVBd.d.ts} +26 -18
- package/dist/{chunk-3OXCZ5DJ.js → chunk-EFQPHF4E.js} +29 -21
- package/dist/chunk-EFQPHF4E.js.map +1 -0
- package/dist/{chunk-LM2YQC3T.cjs → chunk-QRIDQ4RG.cjs} +38 -23
- package/dist/chunk-QRIDQ4RG.cjs.map +1 -0
- package/dist/{chunk-SA2GZKXS.js → chunk-UUBPTMRW.js} +49 -37
- package/dist/chunk-UUBPTMRW.js.map +1 -0
- package/dist/{chunk-ADC5UNZ5.cjs → chunk-V5THHXXQ.cjs} +259 -250
- package/dist/chunk-V5THHXXQ.cjs.map +1 -0
- package/dist/index.cjs +45 -127
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -15
- package/dist/index.d.ts +4 -15
- package/dist/index.js +27 -102
- package/dist/index.js.map +1 -1
- package/dist/{logger-DChjnJMn.d.cts → logger-dzAcLeAA.d.cts} +12 -19
- package/dist/{logger-DChjnJMn.d.ts → logger-dzAcLeAA.d.ts} +12 -19
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -2
- package/dist/logger.d.ts +1 -2
- package/dist/logger.js +2 -3
- package/dist/mocks.cjs +3 -3
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.cts +2 -4
- package/dist/mocks.d.ts +2 -4
- package/dist/mocks.js +2 -2
- package/dist/mocks.js.map +1 -1
- package/package.json +7 -8
- package/src/FileManager.ts +62 -42
- package/src/PluginManager.ts +4 -20
- package/src/build.ts +23 -99
- package/src/errors.ts +0 -11
- package/src/index.ts +0 -1
- package/src/logger.ts +42 -33
- package/dist/chunk-3OXCZ5DJ.js.map +0 -1
- package/dist/chunk-ADC5UNZ5.cjs.map +0 -1
- package/dist/chunk-LM2YQC3T.cjs.map +0 -1
- package/dist/chunk-SA2GZKXS.js.map +0 -1
package/src/FileManager.ts
CHANGED
|
@@ -2,7 +2,6 @@ import crypto from 'node:crypto'
|
|
|
2
2
|
import { extname, resolve } from 'node:path'
|
|
3
3
|
|
|
4
4
|
import { orderBy } from 'natural-orderby'
|
|
5
|
-
import PQueue from 'p-queue'
|
|
6
5
|
import { isDeepEqual } from 'remeda'
|
|
7
6
|
|
|
8
7
|
import { getRelativePath, read, write } from '@kubb/fs'
|
|
@@ -19,6 +18,7 @@ import type { Logger } from './logger.ts'
|
|
|
19
18
|
import transformers from './transformers/index.ts'
|
|
20
19
|
import type { Plugin } from './types.ts'
|
|
21
20
|
import { getParser } from './utils'
|
|
21
|
+
import PQueue from 'p-queue'
|
|
22
22
|
|
|
23
23
|
export type ResolvedFile<TMeta extends FileMetaBase = FileMetaBase, TBaseName extends BaseName = BaseName> = File<TMeta, TBaseName> & {
|
|
24
24
|
/**
|
|
@@ -64,21 +64,9 @@ type AddIndexesProps = {
|
|
|
64
64
|
meta?: FileWithMeta['meta']
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
type Options = {
|
|
68
|
-
queue?: PQueue
|
|
69
|
-
task?: (file: ResolvedFile) => Promise<ResolvedFile>
|
|
70
|
-
}
|
|
71
|
-
|
|
72
67
|
export class FileManager {
|
|
73
68
|
#cache: Map<KubbFile.Path, CacheItem[]> = new Map()
|
|
74
|
-
|
|
75
|
-
#task: Options['task']
|
|
76
|
-
#queue: PQueue
|
|
77
|
-
|
|
78
|
-
constructor({ task = async (file) => file, queue = new PQueue() }: Options = {}) {
|
|
79
|
-
this.#task = task
|
|
80
|
-
this.#queue = queue
|
|
81
|
-
|
|
69
|
+
constructor() {
|
|
82
70
|
return this
|
|
83
71
|
}
|
|
84
72
|
|
|
@@ -90,9 +78,6 @@ export class FileManager {
|
|
|
90
78
|
|
|
91
79
|
return files
|
|
92
80
|
}
|
|
93
|
-
get isExecuting(): boolean {
|
|
94
|
-
return this.#queue.size !== 0 && this.#queue.pending !== 0
|
|
95
|
-
}
|
|
96
81
|
|
|
97
82
|
async add<T extends Array<FileWithMeta> = Array<FileWithMeta>>(...files: T): AddResult<T> {
|
|
98
83
|
const promises = combineFiles(files).map((file) => {
|
|
@@ -136,12 +121,7 @@ export class FileManager {
|
|
|
136
121
|
|
|
137
122
|
this.#cache.set(resolvedFile.path, [{ cancel: () => controller.abort(), ...resolvedFile }])
|
|
138
123
|
|
|
139
|
-
return
|
|
140
|
-
async () => {
|
|
141
|
-
return this.#task?.(resolvedFile)
|
|
142
|
-
},
|
|
143
|
-
{ signal: controller.signal },
|
|
144
|
-
) as Promise<ResolvedFile>
|
|
124
|
+
return resolvedFile
|
|
145
125
|
}
|
|
146
126
|
|
|
147
127
|
async #addOrAppend(file: FileWithMeta): Promise<ResolvedFile> {
|
|
@@ -162,18 +142,18 @@ export class FileManager {
|
|
|
162
142
|
return this.#add(file)
|
|
163
143
|
}
|
|
164
144
|
|
|
165
|
-
async
|
|
145
|
+
async getIndexFiles({ root, output, meta, logger, options = {} }: AddIndexesProps): Promise<ResolvedFile[]> {
|
|
166
146
|
const { exportType = 'barrel' } = output
|
|
167
147
|
// ^?
|
|
168
148
|
if (exportType === false) {
|
|
169
|
-
return
|
|
149
|
+
return []
|
|
170
150
|
}
|
|
171
151
|
|
|
172
152
|
const pathToBuildFrom = resolve(root, output.path)
|
|
173
153
|
|
|
174
154
|
if (transformers.trimExtName(pathToBuildFrom).endsWith('index')) {
|
|
175
155
|
logger.emit('warning', 'Output has the same fileName as the barrelFiles, please disable barrel generation')
|
|
176
|
-
return
|
|
156
|
+
return []
|
|
177
157
|
}
|
|
178
158
|
|
|
179
159
|
const exportPath = output.path.startsWith('./') ? trimExtName(output.path) : `./${trimExtName(output.path)}`
|
|
@@ -185,7 +165,7 @@ export class FileManager {
|
|
|
185
165
|
let files = barrelManager.getIndexes(pathToBuildFrom)
|
|
186
166
|
|
|
187
167
|
if (!files) {
|
|
188
|
-
return
|
|
168
|
+
return []
|
|
189
169
|
}
|
|
190
170
|
|
|
191
171
|
if (exportType === 'barrelNamed') {
|
|
@@ -200,15 +180,6 @@ export class FileManager {
|
|
|
200
180
|
})
|
|
201
181
|
}
|
|
202
182
|
|
|
203
|
-
await Promise.all(
|
|
204
|
-
files.map((file) => {
|
|
205
|
-
return this.#addOrAppend({
|
|
206
|
-
...file,
|
|
207
|
-
meta: meta ? meta : file.meta,
|
|
208
|
-
})
|
|
209
|
-
}),
|
|
210
|
-
)
|
|
211
|
-
|
|
212
183
|
const rootPath = mode === 'split' ? `${exportPath}/index${output.extName || ''}` : `${exportPath}${output.extName || ''}`
|
|
213
184
|
const rootFile: FileWithMeta = {
|
|
214
185
|
path: resolve(root, 'index.ts'),
|
|
@@ -234,10 +205,20 @@ export class FileManager {
|
|
|
234
205
|
rootFile.exports = barrelManager.getNamedExport(root, rootFile.exports[0])
|
|
235
206
|
}
|
|
236
207
|
|
|
237
|
-
|
|
238
|
-
...
|
|
239
|
-
|
|
240
|
-
|
|
208
|
+
return [
|
|
209
|
+
...(await Promise.all(
|
|
210
|
+
files.map((file) => {
|
|
211
|
+
return this.#addOrAppend({
|
|
212
|
+
...file,
|
|
213
|
+
meta: meta ? meta : file.meta,
|
|
214
|
+
})
|
|
215
|
+
}),
|
|
216
|
+
)),
|
|
217
|
+
await this.#addOrAppend({
|
|
218
|
+
...rootFile,
|
|
219
|
+
meta: meta ? meta : rootFile.meta,
|
|
220
|
+
}),
|
|
221
|
+
]
|
|
241
222
|
}
|
|
242
223
|
|
|
243
224
|
getCacheByUUID(UUID: KubbFile.UUID): FileWithMeta | undefined {
|
|
@@ -262,13 +243,16 @@ export class FileManager {
|
|
|
262
243
|
this.#cache.delete(path)
|
|
263
244
|
}
|
|
264
245
|
|
|
265
|
-
async write(...params: Parameters<typeof write>):
|
|
246
|
+
async write(...params: Parameters<typeof write>): ReturnType<typeof write> {
|
|
266
247
|
return write(...params)
|
|
267
248
|
}
|
|
268
249
|
|
|
269
|
-
async read(...params: Parameters<typeof read>):
|
|
250
|
+
async read(...params: Parameters<typeof read>): ReturnType<typeof read> {
|
|
270
251
|
return read(...params)
|
|
271
252
|
}
|
|
253
|
+
async processFiles(...params: Parameters<typeof processFiles>): ReturnType<typeof processFiles> {
|
|
254
|
+
return processFiles(...params)
|
|
255
|
+
}
|
|
272
256
|
|
|
273
257
|
// statics
|
|
274
258
|
|
|
@@ -520,3 +504,39 @@ function getEnvSource(source: string, env: NodeJS.ProcessEnv | undefined): strin
|
|
|
520
504
|
return prev
|
|
521
505
|
}, source)
|
|
522
506
|
}
|
|
507
|
+
|
|
508
|
+
type WriteFilesProps = {
|
|
509
|
+
files: KubbFile.File[]
|
|
510
|
+
logger: Logger
|
|
511
|
+
dryRun?: boolean
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Global queue
|
|
515
|
+
*/
|
|
516
|
+
const queue = new PQueue({ concurrency: 1 })
|
|
517
|
+
|
|
518
|
+
export async function processFiles({ dryRun, logger, files }: WriteFilesProps) {
|
|
519
|
+
const mergedFiles = await Promise.all(
|
|
520
|
+
files.map(async (file) => ({
|
|
521
|
+
...file,
|
|
522
|
+
source: await FileManager.getSource(file),
|
|
523
|
+
})),
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
if (!dryRun) {
|
|
527
|
+
logger.consola?.pauseLogs()
|
|
528
|
+
|
|
529
|
+
const filePromises = mergedFiles.map(async (file, index) => {
|
|
530
|
+
await queue.add(() => {
|
|
531
|
+
logger.emit('progress', index, mergedFiles.length)
|
|
532
|
+
return write(file.path, file.source, { sanity: false })
|
|
533
|
+
})
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
await Promise.all(filePromises)
|
|
537
|
+
|
|
538
|
+
logger.consola?.resumeLogs()
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return mergedFiles
|
|
542
|
+
}
|
package/src/PluginManager.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import PQueue from 'p-queue'
|
|
2
|
-
|
|
3
1
|
import { readSync } from '@kubb/fs'
|
|
4
2
|
import { FileManager, type ResolvedFile } from './FileManager.ts'
|
|
5
3
|
import { isPromise, isPromiseRejectedResult } from './PromiseManager.ts'
|
|
6
4
|
import { PromiseManager } from './PromiseManager.ts'
|
|
7
5
|
import { ValidationPluginError } from './errors.ts'
|
|
8
|
-
import { LogLevel } from './logger.ts'
|
|
9
6
|
import { pluginCore } from './plugin.ts'
|
|
10
7
|
import { transformReservedWord } from './transformers/transformReservedWord.ts'
|
|
11
8
|
import { EventEmitter } from './utils/EventEmitter.ts'
|
|
@@ -59,11 +56,6 @@ type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseRe
|
|
|
59
56
|
|
|
60
57
|
type Options = {
|
|
61
58
|
logger: Logger
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Task for the FileManager
|
|
65
|
-
*/
|
|
66
|
-
task: (file: ResolvedFile) => Promise<ResolvedFile>
|
|
67
59
|
}
|
|
68
60
|
|
|
69
61
|
type Events = {
|
|
@@ -94,16 +86,10 @@ export class PluginManager {
|
|
|
94
86
|
readonly #usedPluginNames: Record<string, number> = {}
|
|
95
87
|
readonly #promiseManager: PromiseManager
|
|
96
88
|
|
|
97
|
-
readonly queue: PQueue
|
|
98
|
-
|
|
99
89
|
constructor(config: Config, options: Options) {
|
|
100
90
|
this.config = config
|
|
101
91
|
this.logger = options.logger
|
|
102
|
-
this.
|
|
103
|
-
this.fileManager = new FileManager({
|
|
104
|
-
task: options.task,
|
|
105
|
-
queue: this.queue,
|
|
106
|
-
})
|
|
92
|
+
this.fileManager = new FileManager()
|
|
107
93
|
this.#promiseManager = new PromiseManager({
|
|
108
94
|
nullCheck: (state: SafeParseResult<'resolveName'> | null) => !!state?.result,
|
|
109
95
|
})
|
|
@@ -423,11 +409,9 @@ export class PluginManager {
|
|
|
423
409
|
const plugins = [...this.plugins].filter((plugin) => plugin.name !== 'core')
|
|
424
410
|
|
|
425
411
|
if (hookName) {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
this.logger.emit('warning', `No hook ${hookName} found`)
|
|
430
|
-
}
|
|
412
|
+
const containsHookName = plugins.some((item) => item[hookName])
|
|
413
|
+
if (!containsHookName) {
|
|
414
|
+
this.logger.emit('info', `No hook ${hookName} found`)
|
|
431
415
|
}
|
|
432
416
|
|
|
433
417
|
return plugins.filter((item) => item[hookName])
|
package/src/build.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import c from 'tinyrainbow'
|
|
2
2
|
|
|
3
|
-
import { clean, read } from '@kubb/fs'
|
|
4
|
-
import { FileManager,
|
|
3
|
+
import { clean, read, write } from '@kubb/fs'
|
|
4
|
+
import { type FileManager, processFiles } from './FileManager.ts'
|
|
5
5
|
import { PluginManager } from './PluginManager.ts'
|
|
6
6
|
import { isInputPath } from './config.ts'
|
|
7
|
-
import {
|
|
7
|
+
import { createLogger, randomCliColour } from './logger.ts'
|
|
8
8
|
import { URLPath } from './utils/URLPath.ts'
|
|
9
9
|
|
|
10
10
|
import type { Logger } from './logger.ts'
|
|
11
11
|
import type { PluginContext } from './types.ts'
|
|
12
|
-
import { createConsola } from 'consola'
|
|
13
12
|
|
|
14
13
|
type BuildOptions = {
|
|
15
14
|
config: PluginContext['config']
|
|
@@ -29,16 +28,7 @@ type BuildOutput = {
|
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
async function setup(options: BuildOptions): Promise<PluginManager> {
|
|
32
|
-
const {
|
|
33
|
-
config,
|
|
34
|
-
logger = createLogger({
|
|
35
|
-
logLevel: LogLevel.silent,
|
|
36
|
-
consola: createConsola({
|
|
37
|
-
level: 3,
|
|
38
|
-
}),
|
|
39
|
-
}),
|
|
40
|
-
} = options
|
|
41
|
-
let count = 0
|
|
31
|
+
const { config, logger = createLogger() } = options
|
|
42
32
|
|
|
43
33
|
try {
|
|
44
34
|
if (isInputPath(config) && !new URLPath(config.input.path).isURL) {
|
|
@@ -59,59 +49,7 @@ async function setup(options: BuildOptions): Promise<PluginManager> {
|
|
|
59
49
|
await clean(config.output.path)
|
|
60
50
|
}
|
|
61
51
|
|
|
62
|
-
const
|
|
63
|
-
const { path } = file
|
|
64
|
-
|
|
65
|
-
const source: string | null = await FileManager.getSource(file)
|
|
66
|
-
|
|
67
|
-
if (source) {
|
|
68
|
-
if (config.output.write || config.output.write === undefined) {
|
|
69
|
-
await pluginManager.fileManager.write(path, source, { sanity: false })
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
...file,
|
|
75
|
-
source: source || '',
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const pluginManager = new PluginManager(config, { logger, task })
|
|
80
|
-
|
|
81
|
-
pluginManager.queue.on('add', () => {
|
|
82
|
-
if (logger.logLevel !== LogLevel.info) {
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (count === 0) {
|
|
87
|
-
logger.emit('start', '💾 Writing')
|
|
88
|
-
}
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
pluginManager.queue.on('active', () => {
|
|
92
|
-
if (logger.logLevel !== LogLevel.info) {
|
|
93
|
-
return
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (logger.spinner && pluginManager.queue.size > 0) {
|
|
97
|
-
const text = `Item: ${count} Size: ${pluginManager.queue.size} Pending: ${pluginManager.queue.pending}`
|
|
98
|
-
|
|
99
|
-
logger.spinner.suffixText = c.dim(text)
|
|
100
|
-
}
|
|
101
|
-
++count
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
pluginManager.queue.on('completed', () => {
|
|
105
|
-
if (logger.logLevel !== LogLevel.info) {
|
|
106
|
-
return
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (logger.spinner) {
|
|
110
|
-
const text = `Item: ${count} Size: ${pluginManager.queue.size} Pending: ${pluginManager.queue.pending}`
|
|
111
|
-
|
|
112
|
-
logger.spinner.suffixText = c.dim(text)
|
|
113
|
-
}
|
|
114
|
-
})
|
|
52
|
+
const pluginManager = new PluginManager(config, { logger })
|
|
115
53
|
|
|
116
54
|
pluginManager.on('executed', (executer) => {
|
|
117
55
|
const { hookName, plugin, output, parameters } = executer
|
|
@@ -133,25 +71,18 @@ async function setup(options: BuildOptions): Promise<PluginManager> {
|
|
|
133
71
|
export async function build(options: BuildOptions): Promise<BuildOutput> {
|
|
134
72
|
const pluginManager = await setup(options)
|
|
135
73
|
|
|
136
|
-
const { fileManager, logger } = pluginManager
|
|
137
|
-
|
|
138
74
|
await pluginManager.hookParallel({
|
|
139
75
|
hookName: 'buildStart',
|
|
140
76
|
parameters: [options.config],
|
|
141
77
|
})
|
|
142
78
|
|
|
143
|
-
await
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
logger.
|
|
147
|
-
}
|
|
79
|
+
const files = await processFiles({
|
|
80
|
+
dryRun: !options.config.output.write,
|
|
81
|
+
files: pluginManager.fileManager.files,
|
|
82
|
+
logger: pluginManager.logger,
|
|
83
|
+
})
|
|
148
84
|
|
|
149
|
-
|
|
150
|
-
fileManager.files.map(async (file) => ({
|
|
151
|
-
...file,
|
|
152
|
-
source: await FileManager.getSource(file),
|
|
153
|
-
})),
|
|
154
|
-
)
|
|
85
|
+
await pluginManager.hookParallel({ hookName: 'buildEnd' })
|
|
155
86
|
|
|
156
87
|
return {
|
|
157
88
|
files,
|
|
@@ -161,8 +92,7 @@ export async function build(options: BuildOptions): Promise<BuildOutput> {
|
|
|
161
92
|
|
|
162
93
|
export async function safeBuild(options: BuildOptions): Promise<BuildOutput> {
|
|
163
94
|
const pluginManager = await setup(options)
|
|
164
|
-
|
|
165
|
-
const { fileManager, logger } = pluginManager
|
|
95
|
+
let files = []
|
|
166
96
|
|
|
167
97
|
try {
|
|
168
98
|
await pluginManager.hookParallel({
|
|
@@ -170,18 +100,19 @@ export async function safeBuild(options: BuildOptions): Promise<BuildOutput> {
|
|
|
170
100
|
parameters: [options.config],
|
|
171
101
|
})
|
|
172
102
|
|
|
173
|
-
await
|
|
103
|
+
files = await processFiles({
|
|
104
|
+
dryRun: !options.config.output.write,
|
|
105
|
+
files: pluginManager.fileManager.files,
|
|
106
|
+
logger: pluginManager.logger,
|
|
107
|
+
})
|
|
174
108
|
|
|
175
|
-
|
|
176
|
-
logger.emit('end', '💾 Writing completed')
|
|
177
|
-
}
|
|
109
|
+
await pluginManager.hookParallel({ hookName: 'buildEnd' })
|
|
178
110
|
} catch (e) {
|
|
179
|
-
const files = await
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
)
|
|
111
|
+
const files = await processFiles({
|
|
112
|
+
dryRun: true,
|
|
113
|
+
files: pluginManager.fileManager.files,
|
|
114
|
+
logger: pluginManager.logger,
|
|
115
|
+
})
|
|
185
116
|
|
|
186
117
|
return {
|
|
187
118
|
files,
|
|
@@ -190,13 +121,6 @@ export async function safeBuild(options: BuildOptions): Promise<BuildOutput> {
|
|
|
190
121
|
}
|
|
191
122
|
}
|
|
192
123
|
|
|
193
|
-
const files = await Promise.all(
|
|
194
|
-
fileManager.files.map(async (file) => ({
|
|
195
|
-
...file,
|
|
196
|
-
source: await FileManager.getSource(file),
|
|
197
|
-
})),
|
|
198
|
-
)
|
|
199
|
-
|
|
200
124
|
return {
|
|
201
125
|
files,
|
|
202
126
|
pluginManager,
|
package/src/errors.ts
CHANGED
|
@@ -1,12 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Behaves as an Error to log a warning in the console(still stops the execution)
|
|
3
|
-
*/
|
|
4
|
-
export class Warning extends Error {
|
|
5
|
-
constructor(message?: string, options?: { cause: Error }) {
|
|
6
|
-
super(message, { cause: options?.cause })
|
|
7
|
-
|
|
8
|
-
this.name = 'Warning'
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
1
|
export class ValidationPluginError extends Error {}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { build, build as default, safeBuild } from './build.ts'
|
|
2
2
|
export { defineConfig, isInputPath } from './config.ts'
|
|
3
|
-
export { Warning } from './errors.ts'
|
|
4
3
|
export { FileManager } from './FileManager.ts'
|
|
5
4
|
export type { FileMetaBase } from './FileManager.ts'
|
|
6
5
|
export { Generator } from './Generator.ts'
|
package/src/logger.ts
CHANGED
|
@@ -3,16 +3,18 @@ import c, { createColors } from 'tinyrainbow'
|
|
|
3
3
|
|
|
4
4
|
import { EventEmitter } from './utils/EventEmitter.ts'
|
|
5
5
|
|
|
6
|
-
import type
|
|
7
|
-
import type { Ora } from 'ora'
|
|
6
|
+
import { type ConsolaInstance, createConsola, type LogLevel } from 'consola'
|
|
8
7
|
import type { Formatter } from 'tinyrainbow'
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
type Events = {
|
|
10
|
+
start: [message: string]
|
|
11
|
+
success: [message: string]
|
|
12
|
+
error: [message: string, cause: Error]
|
|
13
|
+
warning: [message: string]
|
|
14
|
+
debug: [logs: string[]]
|
|
15
|
+
info: [message: string]
|
|
16
|
+
progress: [count: number, size: number]
|
|
17
|
+
}
|
|
16
18
|
|
|
17
19
|
export const LogMapper = {
|
|
18
20
|
silent: Number.NEGATIVE_INFINITY,
|
|
@@ -20,22 +22,12 @@ export const LogMapper = {
|
|
|
20
22
|
debug: 4,
|
|
21
23
|
} as const
|
|
22
24
|
|
|
23
|
-
export type LogLevel = keyof typeof LogLevel
|
|
24
|
-
|
|
25
|
-
type Events = {
|
|
26
|
-
start: [message: string]
|
|
27
|
-
end: [message: string]
|
|
28
|
-
error: [message: string, cause: Error]
|
|
29
|
-
warning: [message: string]
|
|
30
|
-
debug: [logs: string[]]
|
|
31
|
-
}
|
|
32
25
|
export type Logger = {
|
|
33
26
|
/**
|
|
34
27
|
* Optional config name to show in CLI output
|
|
35
28
|
*/
|
|
36
29
|
name?: string
|
|
37
30
|
logLevel: LogLevel
|
|
38
|
-
spinner?: Ora
|
|
39
31
|
consola?: ConsolaInstance
|
|
40
32
|
on: EventEmitter<Events>['on']
|
|
41
33
|
emit: EventEmitter<Events>['emit']
|
|
@@ -43,31 +35,45 @@ export type Logger = {
|
|
|
43
35
|
|
|
44
36
|
type Props = {
|
|
45
37
|
name?: string
|
|
46
|
-
logLevel
|
|
47
|
-
spinner?: Ora
|
|
38
|
+
logLevel?: LogLevel
|
|
48
39
|
consola?: ConsolaInstance
|
|
49
40
|
}
|
|
50
41
|
|
|
51
|
-
export function createLogger({ logLevel, name,
|
|
42
|
+
export function createLogger({ logLevel = 3, name, consola: _consola }: Props = {}): Logger {
|
|
52
43
|
const events = new EventEmitter<Events>()
|
|
53
44
|
|
|
45
|
+
const consola =
|
|
46
|
+
_consola ||
|
|
47
|
+
createConsola({
|
|
48
|
+
level: logLevel,
|
|
49
|
+
formatOptions: {
|
|
50
|
+
colors: true,
|
|
51
|
+
date: true,
|
|
52
|
+
columns: 120,
|
|
53
|
+
compact: logLevel !== LogMapper.debug,
|
|
54
|
+
},
|
|
55
|
+
}).withTag(name ? randomCliColour(name) : '')
|
|
56
|
+
|
|
57
|
+
consola?.wrapConsole()
|
|
58
|
+
|
|
54
59
|
events.on('start', (message) => {
|
|
55
|
-
|
|
56
|
-
spinner.start(message)
|
|
57
|
-
}
|
|
60
|
+
consola.start(message)
|
|
58
61
|
})
|
|
59
62
|
|
|
60
|
-
events.on('
|
|
61
|
-
|
|
62
|
-
spinner.suffixText = ''
|
|
63
|
-
spinner.succeed(message)
|
|
64
|
-
}
|
|
63
|
+
events.on('success', (message) => {
|
|
64
|
+
consola.success(message)
|
|
65
65
|
})
|
|
66
66
|
|
|
67
67
|
events.on('warning', (message) => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
consola.warn(c.yellow(message))
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
events.on('info', (message) => {
|
|
72
|
+
consola.info(c.yellow(message))
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
events.on('debug', (message) => {
|
|
76
|
+
consola.debug(c.yellow(message))
|
|
71
77
|
})
|
|
72
78
|
|
|
73
79
|
events.on('error', (message, cause) => {
|
|
@@ -77,10 +83,13 @@ export function createLogger({ logLevel, name, spinner, consola }: Props): Logge
|
|
|
77
83
|
throw error
|
|
78
84
|
})
|
|
79
85
|
|
|
86
|
+
if (consola) {
|
|
87
|
+
consola.level = logLevel
|
|
88
|
+
}
|
|
89
|
+
|
|
80
90
|
const logger: Logger = {
|
|
81
91
|
name,
|
|
82
92
|
logLevel,
|
|
83
|
-
spinner,
|
|
84
93
|
consola,
|
|
85
94
|
on: (...args) => {
|
|
86
95
|
return events.on(...args)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/logger.ts","../../../node_modules/.pnpm/tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/chunk-BVHSVHOK.js","../../../node_modules/.pnpm/tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/node.js","../src/utils/EventEmitter.ts"],"sourcesContent":["import seedrandom from 'seedrandom'\nimport c, { createColors } from 'tinyrainbow'\n\nimport { EventEmitter } from './utils/EventEmitter.ts'\n\nimport type { ConsolaInstance } from 'consola'\nimport type { Ora } from 'ora'\nimport type { Formatter } from 'tinyrainbow'\n\n//TODO replace with verbose flag and debug flag\nexport const LogLevel = {\n silent: 'silent',\n info: 'info',\n debug: 'debug',\n} as const\n\nexport const LogMapper = {\n silent: Number.NEGATIVE_INFINITY,\n info: 3,\n debug: 4,\n} as const\n\nexport type LogLevel = keyof typeof LogLevel\n\ntype Events = {\n start: [message: string]\n end: [message: string]\n error: [message: string, cause: Error]\n warning: [message: string]\n debug: [logs: string[]]\n}\nexport type Logger = {\n /**\n * Optional config name to show in CLI output\n */\n name?: string\n logLevel: LogLevel\n spinner?: Ora\n consola?: ConsolaInstance\n on: EventEmitter<Events>['on']\n emit: EventEmitter<Events>['emit']\n}\n\ntype Props = {\n name?: string\n logLevel: LogLevel\n spinner?: Ora\n consola?: ConsolaInstance\n}\n\nexport function createLogger({ logLevel, name, spinner, consola }: Props): Logger {\n const events = new EventEmitter<Events>()\n\n events.on('start', (message) => {\n if (spinner) {\n spinner.start(message)\n }\n })\n\n events.on('end', (message) => {\n if (spinner) {\n spinner.suffixText = ''\n spinner.succeed(message)\n }\n })\n\n events.on('warning', (message) => {\n if (spinner) {\n spinner.warn(c.yellow(message))\n }\n })\n\n events.on('error', (message, cause) => {\n const error = new Error(message || 'Something went wrong')\n error.cause = cause\n\n throw error\n })\n\n const logger: Logger = {\n name,\n logLevel,\n spinner,\n consola,\n on: (...args) => {\n return events.on(...args)\n },\n emit: (...args) => {\n return events.emit(...args)\n },\n }\n\n return logger\n}\n\nconst defaultColours = ['black', 'blue', 'darkBlue', 'cyan', 'gray', 'green', 'darkGreen', 'magenta', 'red', 'darkRed', 'yellow', 'darkYellow'] as const\n\nexport function randomColour(text?: string, colours = defaultColours): string {\n if (!text) {\n return 'white'\n }\n\n const random = seedrandom(text)\n const colour = colours.at(Math.floor(random() * colours.length)) || 'white'\n\n return colour\n}\n\nexport function randomCliColour(text?: string, colors = defaultColours): string {\n const colours = createColors(true)\n\n if (!text) {\n return colours.white(text)\n }\n\n const colour = randomColour(text, colors)\n const isDark = colour.includes('dark')\n const key = colour.replace('dark', '').toLowerCase() as keyof typeof colours\n const formatter: Formatter = colours[key] as Formatter\n\n if (isDark) {\n return c.bold(formatter(text))\n }\n\n if (typeof formatter !== 'function') {\n throw new Error('Formatter for picoColor is not of type function/Formatter')\n }\n return formatter(text)\n}\n","// src/index.ts\nvar f = {\n reset: [0, 0],\n bold: [1, 22, \"\\x1B[22m\\x1B[1m\"],\n dim: [2, 22, \"\\x1B[22m\\x1B[2m\"],\n italic: [3, 23],\n underline: [4, 24],\n inverse: [7, 27],\n hidden: [8, 28],\n strikethrough: [9, 29],\n black: [30, 39],\n red: [31, 39],\n green: [32, 39],\n yellow: [33, 39],\n blue: [34, 39],\n magenta: [35, 39],\n cyan: [36, 39],\n white: [37, 39],\n gray: [90, 39],\n bgBlack: [40, 49],\n bgRed: [41, 49],\n bgGreen: [42, 49],\n bgYellow: [43, 49],\n bgBlue: [44, 49],\n bgMagenta: [45, 49],\n bgCyan: [46, 49],\n bgWhite: [47, 49],\n blackBright: [90, 39],\n redBright: [91, 39],\n greenBright: [92, 39],\n yellowBright: [93, 39],\n blueBright: [94, 39],\n magentaBright: [95, 39],\n cyanBright: [96, 39],\n whiteBright: [97, 39],\n bgBlackBright: [100, 49],\n bgRedBright: [101, 49],\n bgGreenBright: [102, 49],\n bgYellowBright: [103, 49],\n bgBlueBright: [104, 49],\n bgMagentaBright: [105, 49],\n bgCyanBright: [106, 49],\n bgWhiteBright: [107, 49]\n}, h = Object.entries(f);\nfunction a(n) {\n return String(n);\n}\na.open = \"\";\na.close = \"\";\nvar B = /* @__PURE__ */ h.reduce(\n (n, [e]) => (n[e] = a, n),\n { isColorSupported: !1 }\n);\nfunction m() {\n return { ...B };\n}\nfunction C(n = !1) {\n let e = typeof process != \"undefined\" ? process : void 0, i = (e == null ? void 0 : e.env) || {}, g = (e == null ? void 0 : e.argv) || [];\n return !(\"NO_COLOR\" in i || g.includes(\"--no-color\")) && (\"FORCE_COLOR\" in i || g.includes(\"--color\") || (e == null ? void 0 : e.platform) === \"win32\" || n && i.TERM !== \"dumb\" || \"CI\" in i) || typeof window != \"undefined\" && !!window.chrome;\n}\nfunction p(n = !1) {\n let e = C(n), i = (r, t, c, o) => {\n let l = \"\", s = 0;\n do\n l += r.substring(s, o) + c, s = o + t.length, o = r.indexOf(t, s);\n while (~o);\n return l + r.substring(s);\n }, g = (r, t, c = r) => {\n let o = (l) => {\n let s = String(l), b = s.indexOf(t, r.length);\n return ~b ? r + i(s, t, c, b) + t : r + s + t;\n };\n return o.open = r, o.close = t, o;\n }, u = {\n isColorSupported: e\n }, d = (r) => `\\x1B[${r}m`;\n for (let [r, t] of h)\n u[r] = e ? g(\n d(t[0]),\n d(t[1]),\n t[2]\n ) : a;\n return u;\n}\n\nexport {\n m as a,\n C as b,\n p as c\n};\n","import {\n a as t,\n b as e,\n c as o\n} from \"./chunk-BVHSVHOK.js\";\n\n// src/node.ts\nimport { isatty as r } from \"tty\";\nvar p = o(r(1));\nexport {\n o as createColors,\n p as default,\n t as getDefaultColors,\n e as isSupported\n};\n","import { EventEmitter as NodeEventEmitter } from 'node:events'\n\nexport class EventEmitter<TEvents extends Record<string, any>> {\n constructor() {\n this.#emitter.setMaxListeners(100)\n }\n #emitter = new NodeEventEmitter()\n\n emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArg: TEvents[TEventName]): void {\n this.#emitter.emit(eventName, ...(eventArg as []))\n }\n\n on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.on(eventName, handler as any)\n }\n\n off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.off(eventName, handler as any)\n }\n removeAll(): void {\n this.#emitter.removeAllListeners()\n }\n}\n"],"mappings":";;;;;;AAAA,OAAO,gBAAgB;;;ACCvB,IAAI,IAAI;AAAA,EACN,OAAO,CAAC,GAAG,CAAC;AAAA,EACZ,MAAM,CAAC,GAAG,IAAI,iBAAiB;AAAA,EAC/B,KAAK,CAAC,GAAG,IAAI,iBAAiB;AAAA,EAC9B,QAAQ,CAAC,GAAG,EAAE;AAAA,EACd,WAAW,CAAC,GAAG,EAAE;AAAA,EACjB,SAAS,CAAC,GAAG,EAAE;AAAA,EACf,QAAQ,CAAC,GAAG,EAAE;AAAA,EACd,eAAe,CAAC,GAAG,EAAE;AAAA,EACrB,OAAO,CAAC,IAAI,EAAE;AAAA,EACd,KAAK,CAAC,IAA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