@kubb/core 3.0.0-alpha.3 → 3.0.0-alpha.5
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--rO8q.d.ts → FileManager-jZpqETKU.d.cts} +15 -20
- package/dist/{FileManager-Bw-FNS3q.d.cts → FileManager-tzl0YsYE.d.ts} +15 -20
- package/dist/{chunk-SA2GZKXS.js → chunk-CEWT73XF.js} +110 -69
- package/dist/chunk-CEWT73XF.js.map +1 -0
- package/dist/{chunk-ADC5UNZ5.cjs → chunk-FWU62YO5.cjs} +319 -281
- package/dist/chunk-FWU62YO5.cjs.map +1 -0
- package/dist/{chunk-LM2YQC3T.cjs → chunk-RBP2ASUX.cjs} +38 -23
- package/dist/chunk-RBP2ASUX.cjs.map +1 -0
- package/dist/{chunk-3OXCZ5DJ.js → chunk-Z5CHJQJB.js} +29 -21
- package/dist/chunk-Z5CHJQJB.js.map +1 -0
- package/dist/index.cjs +43 -130
- 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 +25 -105
- package/dist/index.js.map +1 -1
- package/dist/{logger-DChjnJMn.d.cts → logger-BnWJh6Yq.d.cts} +17 -19
- package/dist/{logger-DChjnJMn.d.ts → logger-BnWJh6Yq.d.ts} +17 -19
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +2 -2
- package/dist/logger.d.ts +2 -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 +4 -4
- package/dist/mocks.js.map +1 -1
- package/package.json +7 -8
- package/src/BarrelManager.ts +10 -1
- package/src/FileManager.ts +107 -63
- package/src/PluginManager.ts +6 -22
- package/src/build.ts +19 -101
- package/src/errors.ts +0 -11
- package/src/index.ts +0 -1
- package/src/logger.ts +43 -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.5",
|
|
4
4
|
"description": "Generator core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -84,23 +84,22 @@
|
|
|
84
84
|
"seedrandom": "^3.0.5",
|
|
85
85
|
"semver": "^7.6.3",
|
|
86
86
|
"unraw": "^3.0.0",
|
|
87
|
-
"@kubb/fs": "3.0.0-alpha.
|
|
88
|
-
"@kubb/parser-ts": "3.0.0-alpha.
|
|
89
|
-
"@kubb/types": "3.0.0-alpha.
|
|
87
|
+
"@kubb/fs": "3.0.0-alpha.5",
|
|
88
|
+
"@kubb/parser-ts": "3.0.0-alpha.5",
|
|
89
|
+
"@kubb/types": "3.0.0-alpha.5"
|
|
90
90
|
},
|
|
91
91
|
"devDependencies": {
|
|
92
92
|
"@types/react": "^18.3.4",
|
|
93
93
|
"@types/seedrandom": "^3.0.8",
|
|
94
94
|
"@types/semver": "^7.5.8",
|
|
95
95
|
"consola": "^3.2.3",
|
|
96
|
-
"ora": "^8.0.1",
|
|
97
96
|
"prettier": "^3.3.3",
|
|
98
97
|
"tinyrainbow": "^1.2.0",
|
|
99
98
|
"tsup": "^8.2.4",
|
|
100
99
|
"typescript": "^5.5.4",
|
|
101
|
-
"@kubb/config-biome": "3.0.0-alpha.
|
|
102
|
-
"@kubb/config-ts": "3.0.0-alpha.
|
|
103
|
-
"@kubb/config-tsup": "3.0.0-alpha.
|
|
100
|
+
"@kubb/config-biome": "3.0.0-alpha.5",
|
|
101
|
+
"@kubb/config-ts": "3.0.0-alpha.5",
|
|
102
|
+
"@kubb/config-tsup": "3.0.0-alpha.5"
|
|
104
103
|
},
|
|
105
104
|
"engines": {
|
|
106
105
|
"node": ">=20"
|
package/src/BarrelManager.ts
CHANGED
|
@@ -17,6 +17,9 @@ export type BarrelManagerOptions = {
|
|
|
17
17
|
extName?: KubbFile.Extname
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Replace with the use of the FileManager exports/imports
|
|
22
|
+
*/
|
|
20
23
|
export class BarrelManager {
|
|
21
24
|
#options: BarrelManagerOptions
|
|
22
25
|
|
|
@@ -26,6 +29,10 @@ export class BarrelManager {
|
|
|
26
29
|
return this
|
|
27
30
|
}
|
|
28
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Loop through the file and find all exports(with the help of the ts printer)
|
|
34
|
+
* Important: a real file is needed(cannot work from memory/FileManager)
|
|
35
|
+
*/
|
|
29
36
|
getNamedExport(root: string, item: KubbFile.Export): KubbFile.Export[] {
|
|
30
37
|
const exportedNames = getExports(path.resolve(root, item.path))
|
|
31
38
|
|
|
@@ -33,7 +40,7 @@ export class BarrelManager {
|
|
|
33
40
|
return [item]
|
|
34
41
|
}
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
const exports = exportedNames.reduce(
|
|
37
44
|
(prev, curr) => {
|
|
38
45
|
if (!prev[0]?.name || !prev[1]?.name) {
|
|
39
46
|
return prev
|
|
@@ -60,6 +67,8 @@ export class BarrelManager {
|
|
|
60
67
|
},
|
|
61
68
|
] as KubbFile.Export[],
|
|
62
69
|
)
|
|
70
|
+
|
|
71
|
+
return exports
|
|
63
72
|
}
|
|
64
73
|
|
|
65
74
|
getNamedExports(root: string, exports: KubbFile.Export[]): KubbFile.Export[] {
|
package/src/FileManager.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import crypto from 'node:crypto'
|
|
2
|
-
import { extname, resolve } from 'node:path'
|
|
2
|
+
import path, { 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
|
/**
|
|
@@ -61,24 +61,12 @@ type AddIndexesProps = {
|
|
|
61
61
|
}
|
|
62
62
|
logger: Logger
|
|
63
63
|
options?: BarrelManagerOptions
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
type Options = {
|
|
68
|
-
queue?: PQueue
|
|
69
|
-
task?: (file: ResolvedFile) => Promise<ResolvedFile>
|
|
64
|
+
plugin: Plugin
|
|
70
65
|
}
|
|
71
66
|
|
|
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,17 @@ export class FileManager {
|
|
|
162
142
|
return this.#add(file)
|
|
163
143
|
}
|
|
164
144
|
|
|
165
|
-
async
|
|
145
|
+
async getIndexFiles({ plugin, root, output, logger, options = {} }: AddIndexesProps): Promise<ResolvedFile[]> {
|
|
166
146
|
const { exportType = 'barrel' } = output
|
|
167
|
-
// ^?
|
|
168
147
|
if (exportType === false) {
|
|
169
|
-
return
|
|
148
|
+
return []
|
|
170
149
|
}
|
|
171
150
|
|
|
172
151
|
const pathToBuildFrom = resolve(root, output.path)
|
|
173
152
|
|
|
174
153
|
if (transformers.trimExtName(pathToBuildFrom).endsWith('index')) {
|
|
175
154
|
logger.emit('warning', 'Output has the same fileName as the barrelFiles, please disable barrel generation')
|
|
176
|
-
return
|
|
155
|
+
return []
|
|
177
156
|
}
|
|
178
157
|
|
|
179
158
|
const exportPath = output.path.startsWith('./') ? trimExtName(output.path) : `./${trimExtName(output.path)}`
|
|
@@ -185,30 +164,9 @@ export class FileManager {
|
|
|
185
164
|
let files = barrelManager.getIndexes(pathToBuildFrom)
|
|
186
165
|
|
|
187
166
|
if (!files) {
|
|
188
|
-
return
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (exportType === 'barrelNamed') {
|
|
192
|
-
files = files.map((file) => {
|
|
193
|
-
if (file.exports) {
|
|
194
|
-
return {
|
|
195
|
-
...file,
|
|
196
|
-
exports: barrelManager.getNamedExports(pathToBuildFrom, file.exports),
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return file
|
|
200
|
-
})
|
|
167
|
+
return []
|
|
201
168
|
}
|
|
202
169
|
|
|
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
170
|
const rootPath = mode === 'split' ? `${exportPath}/index${output.extName || ''}` : `${exportPath}${output.extName || ''}`
|
|
213
171
|
const rootFile: FileWithMeta = {
|
|
214
172
|
path: resolve(root, 'index.ts'),
|
|
@@ -230,14 +188,53 @@ export class FileManager {
|
|
|
230
188
|
exportable: true,
|
|
231
189
|
}
|
|
232
190
|
|
|
233
|
-
if (exportType === 'barrelNamed'
|
|
234
|
-
|
|
191
|
+
if (exportType === 'barrelNamed') {
|
|
192
|
+
files = files.map((file) => {
|
|
193
|
+
if (file.exports) {
|
|
194
|
+
return {
|
|
195
|
+
...file,
|
|
196
|
+
exports: barrelManager.getNamedExports(pathToBuildFrom, file.exports),
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return file
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
const barrelExportRoot = rootFile.exports?.[0]
|
|
203
|
+
|
|
204
|
+
if (!output.exportAs && barrelExportRoot) {
|
|
205
|
+
const exportFile = files.find((file) => {
|
|
206
|
+
return trimExtName(file.path) === path.resolve(root, barrelExportRoot.path)
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
if (exportFile?.exports) {
|
|
210
|
+
rootFile.exports = exportFile.exports.map((exportItem) => {
|
|
211
|
+
return {
|
|
212
|
+
...exportItem,
|
|
213
|
+
path: getRelativePath(rootFile.path, exportFile.path),
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
}
|
|
235
218
|
}
|
|
236
219
|
|
|
237
|
-
|
|
238
|
-
...
|
|
239
|
-
|
|
240
|
-
|
|
220
|
+
return [
|
|
221
|
+
...(await Promise.all(
|
|
222
|
+
files.map((file) => {
|
|
223
|
+
return this.#addOrAppend({
|
|
224
|
+
...file,
|
|
225
|
+
meta: {
|
|
226
|
+
pluginKey: plugin.key,
|
|
227
|
+
},
|
|
228
|
+
})
|
|
229
|
+
}),
|
|
230
|
+
)),
|
|
231
|
+
await this.#addOrAppend({
|
|
232
|
+
...rootFile,
|
|
233
|
+
meta: {
|
|
234
|
+
pluginKey: plugin.key,
|
|
235
|
+
},
|
|
236
|
+
}),
|
|
237
|
+
]
|
|
241
238
|
}
|
|
242
239
|
|
|
243
240
|
getCacheByUUID(UUID: KubbFile.UUID): FileWithMeta | undefined {
|
|
@@ -262,20 +259,20 @@ export class FileManager {
|
|
|
262
259
|
this.#cache.delete(path)
|
|
263
260
|
}
|
|
264
261
|
|
|
265
|
-
async write(...params: Parameters<typeof write>):
|
|
262
|
+
async write(...params: Parameters<typeof write>): ReturnType<typeof write> {
|
|
266
263
|
return write(...params)
|
|
267
264
|
}
|
|
268
265
|
|
|
269
|
-
async read(...params: Parameters<typeof read>):
|
|
266
|
+
async read(...params: Parameters<typeof read>): ReturnType<typeof read> {
|
|
270
267
|
return read(...params)
|
|
271
268
|
}
|
|
272
269
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
static async getSource<TMeta extends FileMetaBase = FileMetaBase>(file: FileWithMeta<TMeta>): Promise<string> {
|
|
276
|
-
return getSource<TMeta>(file)
|
|
270
|
+
async processFiles(...params: Parameters<typeof processFiles>): ReturnType<typeof processFiles> {
|
|
271
|
+
return processFiles(...params)
|
|
277
272
|
}
|
|
278
273
|
|
|
274
|
+
// statics
|
|
275
|
+
|
|
279
276
|
static combineFiles<TMeta extends FileMetaBase = FileMetaBase>(files: Array<FileWithMeta<TMeta> | null>): Array<FileWithMeta<TMeta>> {
|
|
280
277
|
return combineFiles<TMeta>(files)
|
|
281
278
|
}
|
|
@@ -520,3 +517,50 @@ function getEnvSource(source: string, env: NodeJS.ProcessEnv | undefined): strin
|
|
|
520
517
|
return prev
|
|
521
518
|
}, source)
|
|
522
519
|
}
|
|
520
|
+
|
|
521
|
+
type WriteFilesProps = {
|
|
522
|
+
files: KubbFile.File[]
|
|
523
|
+
logger: Logger
|
|
524
|
+
dryRun?: boolean
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Global queue
|
|
528
|
+
*/
|
|
529
|
+
const queue = new PQueue({ concurrency: 10 })
|
|
530
|
+
|
|
531
|
+
export async function processFiles({ dryRun, logger, files }: WriteFilesProps) {
|
|
532
|
+
const mergedFiles: Array<KubbFile.File<FileMetaBase>> = await Promise.all(
|
|
533
|
+
files.map(async (file) => ({
|
|
534
|
+
...file,
|
|
535
|
+
source: await getSource(file),
|
|
536
|
+
})),
|
|
537
|
+
)
|
|
538
|
+
const orderedFiles = orderBy(mergedFiles, [(v) => !v.meta?.pluginKey, (v) => v.path.length, (v) => trimExtName(v.path).endsWith('index')], ['desc', 'desc'])
|
|
539
|
+
|
|
540
|
+
logger.emit(
|
|
541
|
+
'debug',
|
|
542
|
+
orderedFiles.map((item) => `[${item.meta?.pluginKey || 'unknown'}]${item.path}: \n${item.source}`),
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
if (!dryRun) {
|
|
546
|
+
logger.consola?.pauseLogs()
|
|
547
|
+
const size = orderedFiles.length
|
|
548
|
+
|
|
549
|
+
const promises = orderedFiles.map(async (file, index) => {
|
|
550
|
+
await queue.add(async () => {
|
|
551
|
+
logger.emit('progress', { count: index, size, file })
|
|
552
|
+
await write(file.path, file.source, { sanity: false })
|
|
553
|
+
await new Promise((resolve) => {
|
|
554
|
+
setTimeout(resolve, 0)
|
|
555
|
+
})
|
|
556
|
+
logger.emit('progress', { count: index + 1, size, file })
|
|
557
|
+
})
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
await Promise.all(promises)
|
|
561
|
+
|
|
562
|
+
logger.consola?.resumeLogs()
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
return mergedFiles
|
|
566
|
+
}
|
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])
|
|
@@ -482,9 +466,9 @@ export class PluginManager {
|
|
|
482
466
|
const corePlugin = plugins.find((plugin) => plugin.name === 'core' && plugin[hookName])
|
|
483
467
|
|
|
484
468
|
if (corePlugin) {
|
|
485
|
-
this.logger.emit('
|
|
469
|
+
this.logger.emit('warning', `No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, falling back on the '@kubb/core' plugin`)
|
|
486
470
|
} else {
|
|
487
|
-
this.logger.emit('
|
|
471
|
+
this.logger.emit('warning', `No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, no fallback found in the '@kubb/core' plugin`)
|
|
488
472
|
}
|
|
489
473
|
return corePlugin ? [corePlugin] : []
|
|
490
474
|
}
|
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,33 +100,21 @@ 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 Promise.all(
|
|
180
|
-
fileManager.files.map(async (file) => ({
|
|
181
|
-
...file,
|
|
182
|
-
source: await FileManager.getSource(file),
|
|
183
|
-
})),
|
|
184
|
-
)
|
|
185
|
-
|
|
186
111
|
return {
|
|
187
|
-
files,
|
|
112
|
+
files: [],
|
|
188
113
|
pluginManager,
|
|
189
114
|
error: e as Error,
|
|
190
115
|
}
|
|
191
116
|
}
|
|
192
117
|
|
|
193
|
-
const files = await Promise.all(
|
|
194
|
-
fileManager.files.map(async (file) => ({
|
|
195
|
-
...file,
|
|
196
|
-
source: await FileManager.getSource(file),
|
|
197
|
-
})),
|
|
198
|
-
)
|
|
199
|
-
|
|
200
118
|
return {
|
|
201
119
|
files,
|
|
202
120
|
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'
|