@kubb/core 5.0.0-beta.6 → 5.0.0-beta.61
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-DiaUv_iK.d.ts +2904 -0
- package/dist/index.cjs +2523 -1071
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +80 -273
- package/dist/index.js +2513 -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 +83 -23
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +36 -10
- package/dist/mocks.js +85 -27
- 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 +909 -0
- package/src/Transform.ts +105 -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 +140 -17
- package/src/defineParser.ts +30 -12
- package/src/definePlugin.ts +375 -21
- package/src/defineResolver.ts +402 -212
- package/src/diagnostics.ts +662 -0
- package/src/index.ts +8 -8
- package/src/mocks.ts +97 -26
- 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/dist/mocks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mocks.js","names":[],"sources":["../src/mocks.ts"],"sourcesContent":["import { resolve } from 'node:path'\nimport type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'\nimport { transform } from '@kubb/ast'\nimport { FileManager } from './FileManager.ts'\nimport { PluginDriver } from './PluginDriver.ts'\nimport { applyHookResult } from './renderNode.ts'\nimport type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'\n\n/**\n\n * Creates a minimal `PluginDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {\n return {\n config: options?.config ?? {\n root: '.',\n output: {\n path: './path',\n },\n },\n getPlugin(_pluginName: string): NormalizedPlugin | undefined {\n return options?.plugin\n },\n getResolver: (_pluginName: string) => options?.plugin?.resolver,\n fileManager: new FileManager(),\n } as unknown as PluginDriver\n}\n\n/**\n * Creates a minimal `Adapter` mock for unit tests.\n * `parse` returns an empty `InputNode` by default; override via `options.parse`.\n * `getImports` returns `[]` by default.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n inputNode?: Adapter<TOptions>['inputNode']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n const inputNode = options.inputNode ?? null\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n inputNode,\n parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),\n getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),\n } as Adapter<TOptions>\n}\n\n/**\n * Creates a minimal plugin mock for unit tests.\n *\n * @example\n * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`\n */\nexport function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n transformer?: Visitor\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n transformer: params.transformer,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {\n config: Config\n adapter: Adapter\n driver: PluginDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {\n const root = resolve(opts.config.root, opts.config.output.path)\n\n return {\n config: opts.config,\n root,\n getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),\n adapter: opts.adapter,\n resolver: opts.resolver,\n plugin: opts.plugin,\n driver: opts.driver,\n getResolver: (name: string) => opts.driver.getResolver(name),\n inputNode: { kind: 'Input', schemas: [], operations: [] },\n addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),\n upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),\n hooks: opts.driver.hooks ?? ({} as never),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n openInStudio: async () => {},\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\n}\n\n/**\n * Renders a generator's `schema` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\n}\n\n/**\n * Renders a generator's `operation` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\n}\n\n/**\n * Renders a generator's `operations` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n nodes: Array<OperationNode>,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operations) return\n const context = createMockedPluginContext(opts)\n const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\n}\n"],"mappings":";;;;;;;;;AAYA,SAAgB,yBAAyB,UAAyE,EAAE,EAAgB;CAClI,OAAO;EACL,QAAQ,SAAS,UAAU;GACzB,MAAM;GACN,QAAQ,EACN,MAAM,UACP;GACF;EACD,UAAU,aAAmD;GAC3D,OAAO,SAAS;;EAElB,cAAc,gBAAwB,SAAS,QAAQ;EACvD,aAAa,IAAI,aAAa;EAC/B;;;;;;;AAQH,SAAgB,oBACd,UAMI,EAAE,EACa;CACnB,MAAM,YAAY,QAAQ,aAAa;CACvC,OAAO;EACL,MAAO,QAAQ,QAAQ;EACvB,SAAU,QAAQ,mBAAmB,EAAE;EACvC;EACA,OAAO,QAAQ,UAAU,aAAa;GAAE,MAAM;GAAkB,SAAS,EAAE;GAAE,YAAY,EAAE;GAAE;EAC7F,YAAY,QAAQ,gBAAgB,OAAmB,aAAqE,EAAE;EAC/H;;;;;;;;AASH,SAAgB,mBAAiF,QAMlE;CAC7B,OAAO;EACL,MAAM,OAAO;EACb,SAAS,OAAO;EAChB,UAAU,OAAO;EACjB,aAAa,OAAO;EACpB,cAAc,OAAO;EACrB,OAAO,EAAE;EACV;;AAYH,SAAS,0BAAiE,MAAqF;CAC7J,MAAM,OAAO,QAAQ,KAAK,OAAO,MAAM,KAAK,OAAO,OAAO,KAAK;CAE/D,OAAO;EACL,QAAQ,KAAK;EACb;EACA,UAAU,WAA6B,aAAa,QAAQ,QAAQ,MAAM,OAAO,KAAK,CAAC;EACvF,SAAS,KAAK;EACd,UAAU,KAAK;EACf,QAAQ,KAAK;EACb,QAAQ,KAAK;EACb,cAAc,SAAiB,KAAK,OAAO,YAAY,KAAK;EAC5D,WAAW;GAAE,MAAM;GAAS,SAAS,EAAE;GAAE,YAAY,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AG;EACH,SAAS,KAAK;EACf,CAAC,EAC4B,KAAK,QAAQ,UAAU,YAAY,KAAA,EAAU"}
|
|
1
|
+
{"version":3,"file":"mocks.js","names":[],"sources":["../src/mocks.ts"],"sourcesContent":["import path, { resolve } from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport type { FileNode, InputMeta, Macro, OperationNode, SchemaNode } from '@kubb/ast'\nimport { applyMacros } from '@kubb/ast'\nimport { expect } from 'vitest'\nimport type { Parser } from './defineParser.ts'\nimport { FileManager } from './FileManager.ts'\nimport { FileProcessor } from './FileProcessor.ts'\nimport type { KubbDriver } from './KubbDriver.ts'\nimport { memoryStorage } from './storages/memoryStorage.ts'\nimport type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions, RendererFactory } from './types.ts'\n\n/**\n * Creates a minimal `PluginDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): KubbDriver {\n const fileManager = new FileManager()\n\n return {\n config: options?.config ?? {\n root: '.',\n output: {\n path: './path',\n },\n },\n getPlugin(_pluginName: string): NormalizedPlugin | undefined {\n return options?.plugin\n },\n getResolver: (_pluginName: string) => options?.plugin?.resolver,\n fileManager,\n async dispatch({ result, renderer }: { result: unknown; renderer?: RendererFactory | null }): Promise<void> {\n if (!result) return\n\n if (Array.isArray(result)) {\n fileManager.upsert(...(result as Array<FileNode>))\n return\n }\n\n if (!renderer) return\n\n using instance = renderer()\n if (instance.stream) {\n for (const file of instance.stream(result)) fileManager.upsert(file)\n return\n }\n\n await instance.render(result)\n fileManager.upsert(...instance.files)\n },\n } as unknown as KubbDriver\n}\n\n/**\n * Creates a minimal `Adapter` mock for unit tests.\n * `parse` returns an empty `InputNode` by default. Override via `options.parse`.\n * `getImports` returns `[]` by default.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),\n getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),\n } as Adapter<TOptions>\n}\n\n/**\n * Creates a minimal plugin mock for unit tests.\n *\n * @example\n * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`\n */\nexport function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n macros?: Array<Macro>\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n macros: params.macros,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {\n config: Config\n adapter: Adapter\n meta?: InputMeta\n driver: KubbDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {\n const root = resolve(opts.config.root, opts.config.output.path)\n\n return {\n config: opts.config,\n root,\n adapter: opts.adapter,\n resolver: opts.resolver,\n plugin: opts.plugin,\n driver: opts.driver,\n getResolver: (name: string) => opts.driver.getResolver(name),\n meta: opts.meta ?? { circularNames: [], enumNames: [] },\n addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),\n upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),\n hooks: opts.driver.hooks ?? ({} as never),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\n}\n\n/**\n * Renders a generator's `schema` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\n}\n\n/**\n * Renders a generator's `operation` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\n}\n\n/**\n * Renders a generator's `operations` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n nodes: Array<OperationNode>,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operations) return\n const context = createMockedPluginContext(opts)\n const transformedNodes = opts.plugin.macros?.length ? nodes.map((n) => applyMacros(n, opts.plugin.macros!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\n}\n\ntype MatchFilesOptions = {\n /**\n * Parsers indexed by file extension, used to render each `FileNode` to source.\n * Without a matching parser the file's raw content is used.\n */\n parsers?: Map<FileNode['extname'], Parser>\n /**\n * Formatter applied to non-JSON output before snapshotting, e.g. prettier. When\n * omitted the parsed source is snapshotted as-is.\n */\n format?: (source?: string) => string | Promise<string>\n /**\n * Subfolder under `__snapshots__`, camelCased. Useful to keep variant snapshots apart.\n */\n pre?: string\n}\n\n/**\n * Renders the driver's collected `FileNode`s to source and asserts each against a file snapshot.\n * Pair it with the `renderGenerator*` helpers to snapshot a generator's output.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files, { parsers, format })\n * ```\n */\nexport async function matchFiles(files: Array<FileNode> | undefined, options: MatchFilesOptions = {}): Promise<Map<string, string> | undefined> {\n if (!files?.length) return\n\n const { parsers = new Map(), format, pre } = options\n const fileProcessor = new FileProcessor({ storage: memoryStorage(), parsers })\n const processed = new Map<string, string>()\n\n for (const file of files) {\n if (!file?.path || processed.has(file.path)) {\n continue\n }\n\n const parsed = fileProcessor.parse(file)\n const code = file.baseName.endsWith('.json') || !format ? parsed : await format(parsed)\n\n processed.set(file.path, code)\n\n const snapshotPath = path.join('__snapshots__', ...(pre ? [camelCase(pre)] : []), file.baseName)\n await expect(code).toMatchFileSnapshot(snapshotPath)\n }\n\n return processed\n}\n"],"mappings":";;;;;;;;;AAeA,SAAgB,yBAAyB,UAAyE,CAAC,GAAe;CAChI,MAAM,cAAc,IAAI,YAAY;CAEpC,OAAO;EACL,QAAQ,SAAS,UAAU;GACzB,MAAM;GACN,QAAQ,EACN,MAAM,SACR;EACF;EACA,UAAU,aAAmD;GA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B;EAC1D,GAAG;EACH,SAAS,KAAK;CAChB,CAAC;CACD,MAAM,KAAK,OAAO,SAAS;EAAE;EAAQ,UAAU,UAAU;CAAS,CAAC;AACrE;;;;;;;;;;;AA6BA,eAAsB,WAAW,OAAoC,UAA6B,CAAC,GAA6C;CAC9I,IAAI,CAAC,OAAO,QAAQ;CAEpB,MAAM,EAAE,0BAAU,IAAI,IAAI,GAAG,QAAQ,QAAQ;CAC7C,MAAM,gBAAgB,IAAI,cAAc;EAAE,SAAS,cAAc;EAAG;CAAQ,CAAC;CAC7E,MAAM,4BAAY,IAAI,IAAoB;CAE1C,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,CAAC,MAAM,QAAQ,UAAU,IAAI,KAAK,IAAI,GACxC;EAGF,MAAM,SAAS,cAAc,MAAM,IAAI;EACvC,MAAM,OAAO,KAAK,SAAS,SAAS,OAAO,KAAK,CAAC,SAAS,SAAS,MAAM,OAAO,MAAM;EAEtF,UAAU,IAAI,KAAK,MAAM,IAAI;EAE7B,MAAM,eAAe,KAAK,KAAK,iBAAiB,GAAI,MAAM,CAAC,UAAU,GAAG,CAAC,IAAI,CAAC,GAAI,KAAK,QAAQ;EAC/F,MAAM,OAAO,IAAI,CAAC,CAAC,oBAAoB,YAAY;CACrD;CAEA,OAAO;AACT"}
|
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.61",
|
|
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.61"
|
|
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.61"
|
|
74
64
|
},
|
|
75
65
|
"peerDependencies": {
|
|
76
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
66
|
+
"@kubb/renderer-jsx": "5.0.0-beta.61"
|
|
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
|
}
|