@kubb/core 5.0.0-beta.6 → 5.0.0-beta.60
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/LICENSE +17 -10
- package/README.md +25 -158
- package/dist/diagnostics-B-UZnFqP.d.ts +2906 -0
- package/dist/index.cjs +2497 -1071
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +80 -273
- package/dist/index.js +2487 -1067
- package/dist/index.js.map +1 -1
- package/dist/memoryStorage-CUj1hrxa.cjs +823 -0
- package/dist/memoryStorage-CUj1hrxa.cjs.map +1 -0
- package/dist/memoryStorage-CWFzAz4o.js +714 -0
- package/dist/memoryStorage-CWFzAz4o.js.map +1 -0
- package/dist/mocks.cjs +79 -19
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +35 -9
- package/dist/mocks.js +80 -22
- package/dist/mocks.js.map +1 -1
- package/package.json +8 -28
- package/src/FileManager.ts +86 -64
- package/src/FileProcessor.ts +170 -44
- package/src/KubbDriver.ts +908 -0
- package/src/Transform.ts +75 -0
- package/src/constants.ts +111 -20
- package/src/createAdapter.ts +112 -17
- package/src/createKubb.ts +140 -517
- package/src/createRenderer.ts +43 -28
- package/src/createReporter.ts +134 -0
- package/src/createStorage.ts +36 -23
- package/src/defineGenerator.ts +147 -17
- package/src/defineParser.ts +30 -12
- package/src/definePlugin.ts +370 -21
- package/src/defineResolver.ts +402 -212
- package/src/diagnostics.ts +662 -0
- package/src/index.ts +8 -8
- package/src/mocks.ts +91 -20
- package/src/reporters/cliReporter.ts +89 -0
- package/src/reporters/fileReporter.ts +103 -0
- package/src/reporters/jsonReporter.ts +20 -0
- package/src/reporters/report.ts +85 -0
- package/src/storages/fsStorage.ts +23 -55
- package/src/types.ts +411 -887
- package/dist/PluginDriver-BkTRD2H2.js +0 -946
- package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
- package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
- package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
- package/dist/types-DVPKmzw_.d.ts +0 -2159
- package/src/Kubb.ts +0 -300
- package/src/PluginDriver.ts +0 -426
- package/src/defineLogger.ts +0 -19
- package/src/defineMiddleware.ts +0 -62
- package/src/devtools.ts +0 -59
- package/src/renderNode.ts +0 -35
- package/src/utils/diagnostics.ts +0 -18
- package/src/utils/isInputPath.ts +0 -10
- package/src/utils/packageJSON.ts +0 -99
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/package.json
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
4
|
-
"description": "Core
|
|
3
|
+
"version": "5.0.0-beta.60",
|
|
4
|
+
"description": "Core engine for Kubb's plugin-based code generation system. Provides the plugin driver, file manager, defineConfig, and build orchestration used by every Kubb plugin.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"ast",
|
|
7
6
|
"code-generator",
|
|
8
7
|
"codegen",
|
|
9
|
-
"core-library",
|
|
10
|
-
"file-system",
|
|
11
8
|
"kubb",
|
|
12
|
-
"oas",
|
|
13
9
|
"openapi",
|
|
14
|
-
"plugin-framework",
|
|
15
10
|
"plugin-system",
|
|
16
|
-
"plugins",
|
|
17
|
-
"swagger",
|
|
18
11
|
"typescript"
|
|
19
12
|
],
|
|
20
13
|
"license": "MIT",
|
|
@@ -63,39 +56,26 @@
|
|
|
63
56
|
"registry": "https://registry.npmjs.org/"
|
|
64
57
|
},
|
|
65
58
|
"dependencies": {
|
|
66
|
-
"
|
|
67
|
-
"tinyexec": "^1.1.2",
|
|
68
|
-
"@kubb/ast": "5.0.0-beta.6"
|
|
59
|
+
"@kubb/ast": "5.0.0-beta.60"
|
|
69
60
|
},
|
|
70
61
|
"devDependencies": {
|
|
71
|
-
"p-limit": "^7.3.0",
|
|
72
62
|
"@internals/utils": "0.0.0",
|
|
73
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
63
|
+
"@kubb/renderer-jsx": "5.0.0-beta.60"
|
|
74
64
|
},
|
|
75
65
|
"peerDependencies": {
|
|
76
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
66
|
+
"@kubb/renderer-jsx": "5.0.0-beta.60"
|
|
77
67
|
},
|
|
78
|
-
"size-limit": [
|
|
79
|
-
{
|
|
80
|
-
"path": "./dist/*.js",
|
|
81
|
-
"limit": "510 KiB",
|
|
82
|
-
"gzip": true
|
|
83
|
-
}
|
|
84
|
-
],
|
|
85
68
|
"engines": {
|
|
86
69
|
"node": ">=22"
|
|
87
70
|
},
|
|
88
|
-
"inlinedDependencies": {
|
|
89
|
-
"p-limit": "7.3.0",
|
|
90
|
-
"yocto-queue": "1.2.2"
|
|
91
|
-
},
|
|
92
71
|
"scripts": {
|
|
93
|
-
"build": "tsdown
|
|
94
|
-
"clean": "
|
|
72
|
+
"build": "tsdown",
|
|
73
|
+
"clean": "node -e \"require('node:fs').rmSync('./dist', {recursive:true,force:true})\"",
|
|
95
74
|
"lint": "oxlint .",
|
|
96
75
|
"lint:fix": "oxlint --fix .",
|
|
97
76
|
"release": "pnpm publish --no-git-check",
|
|
98
77
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
78
|
+
"release:stage": "pnpm stage publish --no-git-check",
|
|
99
79
|
"start": "tsdown --watch",
|
|
100
80
|
"test": "vitest --passWithNoTests",
|
|
101
81
|
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
|
package/src/FileManager.ts
CHANGED
|
@@ -1,80 +1,103 @@
|
|
|
1
|
+
import { AsyncEventEmitter } from '@internals/utils'
|
|
1
2
|
import type { FileNode } from '@kubb/ast'
|
|
2
|
-
import
|
|
3
|
+
import * as factory from '@kubb/ast/factory'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hooks fired by a `FileManager`.
|
|
7
|
+
*
|
|
8
|
+
* - `upsert` fires once per resolved file added through `add` or `upsert`.
|
|
9
|
+
*/
|
|
10
|
+
export type FileManagerHooks = {
|
|
11
|
+
upsert: [file: FileNode]
|
|
12
|
+
}
|
|
3
13
|
|
|
4
14
|
function mergeFile<TMeta extends object = object>(a: FileNode<TMeta>, b: FileNode<TMeta>): FileNode<TMeta> {
|
|
5
15
|
return {
|
|
6
16
|
...a,
|
|
7
|
-
// Incoming file (b) takes precedence for banner/footer so
|
|
8
|
-
//
|
|
9
|
-
// at the same path.
|
|
17
|
+
// Incoming file (b) takes precedence for banner/footer so a barrel file (whose
|
|
18
|
+
// banner/footer the barrel plugin resolves last) wins over a plugin-generated
|
|
19
|
+
// file at the same path.
|
|
10
20
|
banner: b.banner,
|
|
11
21
|
footer: b.footer,
|
|
12
|
-
sources:
|
|
13
|
-
imports:
|
|
14
|
-
exports:
|
|
22
|
+
sources: a.sources.length ? (b.sources.length ? [...a.sources, ...b.sources] : a.sources) : b.sources,
|
|
23
|
+
imports: a.imports.length ? (b.imports.length ? [...a.imports, ...b.imports] : a.imports) : b.imports,
|
|
24
|
+
exports: a.exports.length ? (b.exports.length ? [...a.exports, ...b.exports] : a.exports) : b.exports,
|
|
15
25
|
}
|
|
16
26
|
}
|
|
17
27
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
28
|
+
function isIndexPath(path: string): boolean {
|
|
29
|
+
return path.endsWith('/index.ts') || path === 'index.ts'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Sort order: shortest path first. Within a length bucket, index.ts barrels last.
|
|
33
|
+
function compareFiles(a: FileNode, b: FileNode): number {
|
|
34
|
+
const lenDiff = a.path.length - b.path.length
|
|
35
|
+
if (lenDiff !== 0) return lenDiff
|
|
36
|
+
const aIsIndex = isIndexPath(a.path)
|
|
37
|
+
const bIsIndex = isIndexPath(b.path)
|
|
38
|
+
if (aIsIndex && !bIsIndex) return 1
|
|
39
|
+
if (!aIsIndex && bIsIndex) return -1
|
|
40
|
+
return 0
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
/**
|
|
32
|
-
* In-memory file store for generated files.
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* The `files` getter returns all stored files sorted by path length (shortest first).
|
|
44
|
+
* In-memory file store for generated files. Files sharing a `path` are merged
|
|
45
|
+
* (sources/imports/exports concatenated). The `files` getter is sorted by
|
|
46
|
+
* path length (barrel `index.ts` last within a bucket).
|
|
36
47
|
*
|
|
37
48
|
* @example
|
|
38
49
|
* ```ts
|
|
39
|
-
* import { FileManager } from '@kubb/core'
|
|
40
|
-
*
|
|
41
50
|
* const manager = new FileManager()
|
|
42
51
|
* manager.upsert(myFile)
|
|
43
|
-
*
|
|
52
|
+
* manager.files // sorted view
|
|
44
53
|
* ```
|
|
45
54
|
*/
|
|
46
55
|
export class FileManager {
|
|
47
|
-
readonly #cache = new Map<string, FileNode>()
|
|
48
|
-
#filesCache: Array<FileNode> | null = null
|
|
49
|
-
|
|
50
56
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* replaced — use {@link upsert} when you want to merge into the cache too.
|
|
57
|
+
* Subscribe to file-store changes. Listeners on `upsert` see each resolved file as it lands
|
|
58
|
+
* through `add` or `upsert`.
|
|
54
59
|
*/
|
|
60
|
+
readonly hooks = new AsyncEventEmitter<FileManagerHooks>()
|
|
61
|
+
readonly #cache = new Map<string, FileNode>()
|
|
62
|
+
// Cached sorted view. Null means stale and rebuilt lazily on next `files` read.
|
|
63
|
+
// Nulled (not mutated) on every write so callers holding a prior reference
|
|
64
|
+
// keep their snapshot, `dispose()` must not silently empty an array the
|
|
65
|
+
// consumer already holds.
|
|
66
|
+
#sorted: Array<FileNode> | null = null
|
|
67
|
+
|
|
55
68
|
add(...files: Array<FileNode>): Array<FileNode> {
|
|
56
69
|
return this.#store(files, false)
|
|
57
70
|
}
|
|
58
71
|
|
|
59
|
-
/**
|
|
60
|
-
* Adds or merges one or more files.
|
|
61
|
-
* If a file with the same path already exists in the cache, its
|
|
62
|
-
* sources/imports/exports are merged into the incoming file.
|
|
63
|
-
*/
|
|
64
72
|
upsert(...files: Array<FileNode>): Array<FileNode> {
|
|
65
73
|
return this.#store(files, true)
|
|
66
74
|
}
|
|
67
75
|
|
|
68
76
|
#store(files: ReadonlyArray<FileNode>, mergeExisting: boolean): Array<FileNode> {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.#cache.
|
|
74
|
-
|
|
77
|
+
const batch = files.length > 1 ? this.#dedupe(files) : files
|
|
78
|
+
const resolved: Array<FileNode> = []
|
|
79
|
+
|
|
80
|
+
for (const file of batch) {
|
|
81
|
+
const existing = this.#cache.get(file.path)
|
|
82
|
+
const merged = existing && mergeExisting ? factory.createFile(mergeFile(existing, file)) : factory.createFile(file)
|
|
83
|
+
this.#cache.set(merged.path, merged)
|
|
84
|
+
resolved.push(merged)
|
|
85
|
+
this.hooks.emit('upsert', merged)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (resolved.length > 0) this.#sorted = null
|
|
89
|
+
return resolved
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Merges same-path entries within a batch so the cache update loop stays
|
|
93
|
+
// uniform. Only called for multi-file batches.
|
|
94
|
+
#dedupe(files: ReadonlyArray<FileNode>): Array<FileNode> {
|
|
95
|
+
const seen = new Map<string, FileNode>()
|
|
96
|
+
for (const file of files) {
|
|
97
|
+
const prev = seen.get(file.path)
|
|
98
|
+
seen.set(file.path, prev ? mergeFile(prev, file) : file)
|
|
75
99
|
}
|
|
76
|
-
|
|
77
|
-
return resolvedFiles
|
|
100
|
+
return [...seen.values()]
|
|
78
101
|
}
|
|
79
102
|
|
|
80
103
|
getByPath(path: string): FileNode | null {
|
|
@@ -82,34 +105,33 @@ export class FileManager {
|
|
|
82
105
|
}
|
|
83
106
|
|
|
84
107
|
deleteByPath(path: string): void {
|
|
85
|
-
this.#cache.delete(path)
|
|
86
|
-
this.#
|
|
108
|
+
if (!this.#cache.delete(path)) return
|
|
109
|
+
this.#sorted = null
|
|
87
110
|
}
|
|
88
111
|
|
|
89
112
|
clear(): void {
|
|
90
113
|
this.#cache.clear()
|
|
91
|
-
this.#
|
|
114
|
+
this.#sorted = null
|
|
92
115
|
}
|
|
93
116
|
|
|
94
117
|
/**
|
|
95
|
-
*
|
|
118
|
+
* Releases all stored files and clears every `hooks` listener. Called by the core after
|
|
119
|
+
* `kubb:build:end`.
|
|
96
120
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
121
|
+
dispose(): void {
|
|
122
|
+
this.clear()
|
|
123
|
+
this.hooks.removeAll()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
[Symbol.dispose](): void {
|
|
127
|
+
this.dispose()
|
|
128
|
+
}
|
|
101
129
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const bIsIndex = b.path.endsWith('/index.ts') || b.path === 'index.ts'
|
|
109
|
-
if (aIsIndex && !bIsIndex) return 1
|
|
110
|
-
if (!aIsIndex && bIsIndex) return -1
|
|
111
|
-
return 0
|
|
112
|
-
})
|
|
113
|
-
return this.#filesCache
|
|
130
|
+
/**
|
|
131
|
+
* All stored files in stable sort order (shortest path first, barrel files
|
|
132
|
+
* last within a length bucket). Returns a cached view, do not mutate.
|
|
133
|
+
*/
|
|
134
|
+
get files(): Array<FileNode> {
|
|
135
|
+
return (this.#sorted ??= [...this.#cache.values()].sort(compareFiles))
|
|
114
136
|
}
|
|
115
137
|
}
|
package/src/FileProcessor.ts
CHANGED
|
@@ -1,42 +1,102 @@
|
|
|
1
|
+
import { AsyncEventEmitter } from '@internals/utils'
|
|
1
2
|
import type { CodeNode, FileNode } from '@kubb/ast'
|
|
2
|
-
import { extractStringsFromNodes } from '@kubb/ast'
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
3
|
+
import { extractStringsFromNodes } from '@kubb/ast/utils'
|
|
4
|
+
import { STREAM_FLUSH_EVERY } from './constants.ts'
|
|
5
|
+
import type { Storage } from './createStorage.ts'
|
|
5
6
|
import type { Parser } from './defineParser.ts'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Hooks fired by a `FileProcessor`.
|
|
10
|
+
*
|
|
11
|
+
* - `start` opens a batch, from `run` or a queue flush.
|
|
12
|
+
* - `update` fires once per file as it is converted.
|
|
13
|
+
* - `end` closes a batch.
|
|
14
|
+
* - `enqueue` fires for every `enqueue` call.
|
|
15
|
+
* - `drain` fires when `drain()` empties the queue with no in-flight batch left.
|
|
16
|
+
*/
|
|
17
|
+
export type FileProcessorHooks = {
|
|
18
|
+
start: [files: Array<FileNode>]
|
|
19
|
+
update: [params: { file: FileNode; source?: string; processed: number; total: number; percentage: number }]
|
|
20
|
+
end: [files: Array<FileNode>]
|
|
21
|
+
enqueue: [file: FileNode]
|
|
22
|
+
drain: []
|
|
10
23
|
}
|
|
11
24
|
|
|
12
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Per-file progress record yielded by `stream` and surfaced through the `update` event.
|
|
27
|
+
*/
|
|
28
|
+
export type ParsedFile = {
|
|
29
|
+
file: FileNode
|
|
30
|
+
source: string
|
|
31
|
+
processed: number
|
|
32
|
+
total: number
|
|
33
|
+
percentage: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type FileProcessorOptions = {
|
|
37
|
+
/**
|
|
38
|
+
* Storage destination for queued writes.
|
|
39
|
+
*/
|
|
40
|
+
storage: Storage
|
|
13
41
|
/**
|
|
14
|
-
*
|
|
42
|
+
* Parsers indexed by file extension.
|
|
15
43
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
44
|
+
parsers?: Map<FileNode['extname'], Parser>
|
|
45
|
+
/**
|
|
46
|
+
* Output extname per source extname, applied during conversion.
|
|
47
|
+
*/
|
|
48
|
+
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
|
|
20
49
|
}
|
|
21
50
|
|
|
22
51
|
function joinSources(file: FileNode): string {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
52
|
+
const sources = file.sources
|
|
53
|
+
if (sources.length === 0) return ''
|
|
54
|
+
const parts: Array<string> = []
|
|
55
|
+
for (const source of sources) {
|
|
56
|
+
const text = extractStringsFromNodes(source.nodes as Array<CodeNode>)
|
|
57
|
+
if (text) parts.push(text)
|
|
58
|
+
}
|
|
59
|
+
return parts.join('\n\n')
|
|
27
60
|
}
|
|
28
61
|
|
|
29
62
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
63
|
+
* Turns `FileNode`s into source strings and writes them to storage.
|
|
64
|
+
*
|
|
65
|
+
* Two modes share the same instance. Stateless mode (`parse`, `stream`, `run`) just runs the
|
|
66
|
+
* conversion. Queue mode (`enqueue`, `flush`, `drain`) buffers files deduped by path and
|
|
67
|
+
* writes each batch through storage with up to `STREAM_FLUSH_EVERY` requests in flight.
|
|
32
68
|
*
|
|
33
|
-
*
|
|
69
|
+
* `flush` does not wait for its batch to finish, so dispatch can overlap with IO. The next
|
|
70
|
+
* `flush` or `drain` picks the in-flight batch up. `drain` blocks until everything has been
|
|
71
|
+
* written and is meant for the end of a build.
|
|
72
|
+
*
|
|
73
|
+
* To surface build-level hook signals (`kubb:files:processing:*` and friends) subscribe to
|
|
74
|
+
* `hooks` and re-emit on the kubb bus.
|
|
34
75
|
*/
|
|
35
76
|
export class FileProcessor {
|
|
36
|
-
readonly
|
|
77
|
+
readonly hooks = new AsyncEventEmitter<FileProcessorHooks>()
|
|
78
|
+
readonly #parsers: Map<FileNode['extname'], Parser> | null
|
|
79
|
+
readonly #storage: Storage
|
|
80
|
+
readonly #extension: Record<FileNode['extname'], FileNode['extname'] | ''> | null
|
|
81
|
+
readonly #pending = new Map<string, FileNode>()
|
|
82
|
+
#runningFlush: Promise<void> | null = null
|
|
83
|
+
|
|
84
|
+
constructor(options: FileProcessorOptions) {
|
|
85
|
+
this.#parsers = options.parsers ?? null
|
|
86
|
+
this.#storage = options.storage
|
|
87
|
+
this.#extension = options.extension ?? null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Files waiting in the queue.
|
|
92
|
+
*/
|
|
93
|
+
get size(): number {
|
|
94
|
+
return this.#pending.size
|
|
95
|
+
}
|
|
37
96
|
|
|
38
|
-
|
|
39
|
-
const
|
|
97
|
+
parse(file: FileNode): string {
|
|
98
|
+
const parsers = this.#parsers
|
|
99
|
+
const parseExtName = this.#extension?.[file.extname] || undefined
|
|
40
100
|
|
|
41
101
|
if (!parsers || !file.extname) {
|
|
42
102
|
return joinSources(file)
|
|
@@ -51,36 +111,102 @@ export class FileProcessor {
|
|
|
51
111
|
return parser.parse(file, { extname: parseExtName })
|
|
52
112
|
}
|
|
53
113
|
|
|
54
|
-
|
|
55
|
-
await onStart?.(files)
|
|
56
|
-
|
|
114
|
+
*stream(files: ReadonlyArray<FileNode>): Generator<ParsedFile> {
|
|
57
115
|
const total = files.length
|
|
116
|
+
if (total === 0) return
|
|
117
|
+
|
|
58
118
|
let processed = 0
|
|
119
|
+
for (const file of files) {
|
|
120
|
+
const source = this.parse(file)
|
|
121
|
+
processed++
|
|
59
122
|
|
|
60
|
-
|
|
61
|
-
const source = await this.parse(file, { extension, parsers })
|
|
62
|
-
const currentProcessed = ++processed
|
|
63
|
-
const percentage = (currentProcessed / total) * 100
|
|
64
|
-
|
|
65
|
-
await onUpdate?.({
|
|
66
|
-
file,
|
|
67
|
-
source,
|
|
68
|
-
processed: currentProcessed,
|
|
69
|
-
percentage,
|
|
70
|
-
total,
|
|
71
|
-
})
|
|
123
|
+
yield { file, source, processed, total, percentage: (processed / total) * 100 }
|
|
72
124
|
}
|
|
125
|
+
}
|
|
73
126
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await Promise.all(files.map((file) => this.#limit(() => processOne(file))))
|
|
127
|
+
async run(files: Array<FileNode>): Promise<Array<FileNode>> {
|
|
128
|
+
await this.hooks.emit('start', files)
|
|
129
|
+
|
|
130
|
+
for (const { file, source, processed, total, percentage } of this.stream(files)) {
|
|
131
|
+
await this.hooks.emit('update', { file, source, processed, percentage, total })
|
|
80
132
|
}
|
|
81
133
|
|
|
82
|
-
await
|
|
134
|
+
await this.hooks.emit('end', files)
|
|
83
135
|
|
|
84
136
|
return files
|
|
85
137
|
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Adds a file to the next flush. A later `enqueue` for the same path replaces the previous
|
|
141
|
+
* entry, matching `FileManager.upsert`. Fires the `enqueue` event.
|
|
142
|
+
*/
|
|
143
|
+
enqueue(file: FileNode): void {
|
|
144
|
+
this.#pending.set(file.path, file)
|
|
145
|
+
this.hooks.emit('enqueue', file)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Starts processing the queued files. Waits for any previous flush to finish (so two
|
|
150
|
+
* batches never run together) and then returns without waiting for the new one. The next
|
|
151
|
+
* `flush` or `drain` picks up the in-flight task.
|
|
152
|
+
*/
|
|
153
|
+
async flush(): Promise<void> {
|
|
154
|
+
if (this.#runningFlush) await this.#runningFlush
|
|
155
|
+
if (this.#pending.size === 0) return
|
|
156
|
+
|
|
157
|
+
const batch = [...this.#pending.values()]
|
|
158
|
+
this.#pending.clear()
|
|
159
|
+
|
|
160
|
+
this.#runningFlush = this.#processAndWrite(batch).finally(() => {
|
|
161
|
+
this.#runningFlush = null
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Waits for the in-flight flush and writes any files still queued. Fires the `drain` event
|
|
167
|
+
* when both are done.
|
|
168
|
+
*/
|
|
169
|
+
async drain(): Promise<void> {
|
|
170
|
+
if (this.#runningFlush) await this.#runningFlush
|
|
171
|
+
|
|
172
|
+
if (this.#pending.size > 0) {
|
|
173
|
+
const batch = [...this.#pending.values()]
|
|
174
|
+
this.#pending.clear()
|
|
175
|
+
await this.#processAndWrite(batch)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
await this.hooks.emit('drain')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async #processAndWrite(files: Array<FileNode>): Promise<void> {
|
|
182
|
+
const storage = this.#storage
|
|
183
|
+
|
|
184
|
+
await this.hooks.emit('start', files)
|
|
185
|
+
|
|
186
|
+
// Single pass: each file's write starts right after its `update` fires, so IO overlaps
|
|
187
|
+
// parsing and the batch never holds every rendered source in memory at once.
|
|
188
|
+
const queue: Array<Promise<void>> = []
|
|
189
|
+
for (const item of this.stream(files)) {
|
|
190
|
+
await this.hooks.emit('update', item)
|
|
191
|
+
if (item.source) {
|
|
192
|
+
queue.push(storage.setItem(item.file.path, item.source))
|
|
193
|
+
if (queue.length >= STREAM_FLUSH_EVERY) await Promise.all(queue.splice(0))
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
await Promise.all(queue)
|
|
197
|
+
|
|
198
|
+
await this.hooks.emit('end', files)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Clears every listener and the pending queue.
|
|
203
|
+
*/
|
|
204
|
+
dispose(): void {
|
|
205
|
+
this.hooks.removeAll()
|
|
206
|
+
this.#pending.clear()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
[Symbol.dispose](): void {
|
|
210
|
+
this.dispose()
|
|
211
|
+
}
|
|
86
212
|
}
|