@kubb/core
Advanced tools
Comparing version 3.0.0-alpha.1 to 3.0.0-alpha.2
@@ -1,3 +0,3 @@ | ||
import { P as PluginContext, F as FileManager, a as PluginManager, U as UserConfig, C as Config, I as InputPath, b as PluginFactoryOptions, c as UserPluginWithLifeCycle } from './FileManager-EVJig-LT.js'; | ||
export { d as FileMetaBase, G as GetPluginFactoryOptions, e as InputData, h as Plugin, m as PluginCache, f as PluginKey, j as PluginLifecycle, k as PluginLifecycleHooks, l as PluginParameter, i as PluginWithLifeCycle, n as ResolveNameParams, R as ResolvePathParams, T as TransformResult, g as UserPlugin } from './FileManager-EVJig-LT.js'; | ||
import { P as PluginContext, F as FileManager, a as PluginManager, U as UserConfig, C as Config, I as InputPath, b as PluginFactoryOptions, c as UserPluginWithLifeCycle } from './FileManager-BW--rO8q.js'; | ||
export { d as FileMetaBase, G as GetPluginFactoryOptions, e as InputData, h as Plugin, m as PluginCache, f as PluginKey, j as PluginLifecycle, k as PluginLifecycleHooks, l as PluginParameter, i as PluginWithLifeCycle, n as ResolveNameParams, R as ResolvePathParams, g as UserPlugin } from './FileManager-BW--rO8q.js'; | ||
import { L as Logger } from './logger-DChjnJMn.js'; | ||
@@ -4,0 +4,0 @@ import { PossiblePromise } from '@kubb/types'; |
@@ -149,3 +149,3 @@ import { | ||
key: ["core"], | ||
api() { | ||
context() { | ||
return { | ||
@@ -265,5 +265,5 @@ get config() { | ||
}); | ||
__privateSet(this, _core, __privateMethod(this, _PluginManager_instances, parse_fn).call(this, core, this, core.api.call(null))); | ||
__privateSet(this, _core, __privateMethod(this, _PluginManager_instances, parse_fn).call(this, core, this, core.context.call(null))); | ||
this.plugins = [__privateGet(this, _core), ...plugins].map((plugin) => { | ||
return __privateMethod(this, _PluginManager_instances, parse_fn).call(this, plugin, this, __privateGet(this, _core).api); | ||
return __privateMethod(this, _PluginManager_instances, parse_fn).call(this, plugin, this, __privateGet(this, _core).context); | ||
}); | ||
@@ -432,3 +432,3 @@ return this; | ||
return value; | ||
}).then((result) => reduce.call(__privateGet(this, _core).api, argument0, result, plugin)); | ||
}).then((result) => reduce.call(__privateGet(this, _core).context, argument0, result, plugin)); | ||
} | ||
@@ -490,3 +490,3 @@ return promise; | ||
static get hooks() { | ||
return ["buildStart", "resolvePath", "resolveName", "load", "transform", "writeFile", "buildEnd"]; | ||
return ["buildStart", "resolvePath", "resolveName", "buildEnd"]; | ||
} | ||
@@ -554,3 +554,3 @@ }; | ||
if (typeof hook === "function") { | ||
const possiblePromiseResult = hook.apply({ ...__privateGet(this, _core).api, plugin }, parameters); | ||
const possiblePromiseResult = hook.apply({ ...__privateGet(this, _core).context, plugin }, parameters); | ||
if (isPromise(possiblePromiseResult)) { | ||
@@ -599,3 +599,3 @@ return Promise.resolve(possiblePromiseResult); | ||
if (typeof hook === "function") { | ||
const fn = hook.apply({ ...__privateGet(this, _core).api, plugin }, parameters); | ||
const fn = hook.apply({ ...__privateGet(this, _core).context, plugin }, parameters); | ||
output = fn; | ||
@@ -627,13 +627,7 @@ return fn; | ||
const key = [plugin.name, usedPluginNames[plugin.name]].filter(Boolean); | ||
if (!plugin.transform) { | ||
plugin.transform = function transform(_path, code) { | ||
return code; | ||
}; | ||
} | ||
if (plugin.api && typeof plugin.api === "function") { | ||
const api = plugin.api.call(context); | ||
if (plugin.context && typeof plugin.context === "function") { | ||
return { | ||
...plugin, | ||
key, | ||
api | ||
context: plugin.context.call(context) | ||
}; | ||
@@ -656,5 +650,2 @@ } | ||
// src/build.ts | ||
async function transformReducer(_previousCode, result, _plugin) { | ||
return result; | ||
} | ||
async function setup(options) { | ||
@@ -682,31 +673,6 @@ const { config, logger = createLogger({ logLevel: LogLevel.silent }) } = options; | ||
const { path: path2 } = file; | ||
let source = await FileManager.getSource(file); | ||
const { result: loadedResult } = await pluginManager.hookFirst({ | ||
hookName: "load", | ||
parameters: [path2] | ||
}); | ||
if (loadedResult && isPromise(loadedResult)) { | ||
source = await loadedResult; | ||
} | ||
if (loadedResult && !isPromise(loadedResult)) { | ||
source = loadedResult; | ||
} | ||
const source = await FileManager.getSource(file); | ||
if (source) { | ||
source = await pluginManager.hookReduceArg0({ | ||
hookName: "transform", | ||
parameters: [path2, source], | ||
reduce: transformReducer | ||
}); | ||
if (config.output.write || config.output.write === void 0) { | ||
if (file.meta?.pluginKey) { | ||
await pluginManager.hookForPlugin({ | ||
pluginKey: file.meta?.pluginKey, | ||
hookName: "writeFile", | ||
parameters: [path2, source] | ||
}); | ||
} | ||
await pluginManager.hookFirst({ | ||
hookName: "writeFile", | ||
parameters: [path2, source] | ||
}); | ||
await pluginManager.fileManager.write(path2, source, { sanity: false }); | ||
} | ||
@@ -720,12 +686,2 @@ } | ||
const pluginManager = new PluginManager(config, { logger, task }); | ||
pluginManager.on("execute", (executer) => { | ||
const { hookName, parameters, plugin } = executer; | ||
if (hookName === "writeFile") { | ||
const [code] = parameters; | ||
logger.emit("debug", [`PluginKey ${p.dim(JSON.stringify(plugin.key))} | ||
with source | ||
${code}`]); | ||
} | ||
}); | ||
pluginManager.queue.on("add", () => { | ||
@@ -732,0 +688,0 @@ if (logger.logLevel !== LogLevel.info) { |
import * as KubbFile from '@kubb/fs/types'; | ||
import { a as PluginManager } from './FileManager-EVJig-LT.js'; | ||
import { a as PluginManager } from './FileManager-BW--rO8q.js'; | ||
import 'p-queue'; | ||
@@ -4,0 +4,0 @@ import '@kubb/fs'; |
{ | ||
"name": "@kubb/core", | ||
"version": "3.0.0-alpha.1", | ||
"version": "3.0.0-alpha.2", | ||
"description": "Generator core", | ||
@@ -87,5 +87,5 @@ "keywords": [ | ||
"unraw": "^3.0.0", | ||
"@kubb/fs": "3.0.0-alpha.1", | ||
"@kubb/parser-ts": "3.0.0-alpha.1", | ||
"@kubb/types": "3.0.0-alpha.1" | ||
"@kubb/fs": "3.0.0-alpha.2", | ||
"@kubb/parser-ts": "3.0.0-alpha.2", | ||
"@kubb/types": "3.0.0-alpha.2" | ||
}, | ||
@@ -102,5 +102,5 @@ "devDependencies": { | ||
"typescript": "^5.5.4", | ||
"@kubb/config-biome": "3.0.0-alpha.1", | ||
"@kubb/config-ts": "3.0.0-alpha.1", | ||
"@kubb/config-tsup": "3.0.0-alpha.1" | ||
"@kubb/config-biome": "3.0.0-alpha.2", | ||
"@kubb/config-ts": "3.0.0-alpha.2", | ||
"@kubb/config-tsup": "3.0.0-alpha.2" | ||
}, | ||
@@ -107,0 +107,0 @@ "engines": { |
@@ -6,3 +6,2 @@ import c from 'tinyrainbow' | ||
import { PluginManager } from './PluginManager.ts' | ||
import { isPromise } from './PromiseManager.ts' | ||
import { isInputPath } from './config.ts' | ||
@@ -13,3 +12,3 @@ import { LogLevel, createLogger, randomCliColour } from './logger.ts' | ||
import type { Logger } from './logger.ts' | ||
import type { Plugin, PluginContext, PluginParameter, TransformResult } from './types.ts' | ||
import type { PluginContext, PluginParameter } from './types.ts' | ||
@@ -33,11 +32,2 @@ type BuildOptions = { | ||
async function transformReducer( | ||
this: PluginContext, | ||
_previousCode: string, | ||
result: TransformResult | Promise<TransformResult>, | ||
_plugin: Plugin, | ||
): Promise<string | null> { | ||
return result | ||
} | ||
async function setup(options: BuildOptions): Promise<PluginManager> { | ||
@@ -69,36 +59,7 @@ const { config, logger = createLogger({ logLevel: LogLevel.silent }) } = options | ||
let source: string | null = await FileManager.getSource(file) | ||
const source: string | null = await FileManager.getSource(file) | ||
const { result: loadedResult } = await pluginManager.hookFirst({ | ||
hookName: 'load', | ||
parameters: [path], | ||
}) | ||
if (loadedResult && isPromise(loadedResult)) { | ||
source = await loadedResult | ||
} | ||
if (loadedResult && !isPromise(loadedResult)) { | ||
source = loadedResult | ||
} | ||
if (source) { | ||
source = await pluginManager.hookReduceArg0({ | ||
hookName: 'transform', | ||
parameters: [path, source], | ||
reduce: transformReducer, | ||
}) | ||
if (config.output.write || config.output.write === undefined) { | ||
if (file.meta?.pluginKey) { | ||
// run only for pluginKey defined in the meta of the file | ||
await pluginManager.hookForPlugin({ | ||
pluginKey: file.meta?.pluginKey, | ||
hookName: 'writeFile', | ||
parameters: [path, source], | ||
}) | ||
} | ||
await pluginManager.hookFirst({ | ||
hookName: 'writeFile', | ||
parameters: [path, source], | ||
}) | ||
await pluginManager.fileManager.write(path, source, { sanity: false }) | ||
} | ||
@@ -115,12 +76,2 @@ } | ||
pluginManager.on('execute', (executer) => { | ||
const { hookName, parameters, plugin } = executer | ||
if (hookName === 'writeFile') { | ||
const [code] = parameters as PluginParameter<'writeFile'> | ||
logger.emit('debug', [`PluginKey ${c.dim(JSON.stringify(plugin.key))} \nwith source\n\n${code}`]) | ||
} | ||
}) | ||
pluginManager.queue.on('add', () => { | ||
@@ -127,0 +78,0 @@ if (logger.logLevel !== LogLevel.info) { |
@@ -40,3 +40,3 @@ import path from 'node:path' | ||
key: ['core'], | ||
api() { | ||
context() { | ||
return { | ||
@@ -43,0 +43,0 @@ get config() { |
@@ -123,7 +123,7 @@ import PQueue from 'p-queue' | ||
// call core.api.call with empty context so we can transform `api()` to `api: {}` | ||
this.#core = this.#parse(core as unknown as UserPlugin, this as any, core.api.call(null as any)) as Plugin<PluginCore> | ||
// call core.context.call with empty context so we can transform `context()` to `context: {}` | ||
this.#core = this.#parse(core as unknown as UserPlugin, this as any, core.context.call(null as any)) as Plugin<PluginCore> | ||
this.plugins = [this.#core, ...plugins].map((plugin) => { | ||
return this.#parse(plugin as UserPlugin, this, this.#core.api) | ||
return this.#parse(plugin as UserPlugin, this, this.#core.context) | ||
}) | ||
@@ -400,3 +400,3 @@ | ||
}) | ||
.then((result) => reduce.call(this.#core.api, argument0, result as ReturnType<ParseResult<H>>, plugin)) as Promise<Argument0<H>> | ||
.then((result) => reduce.call(this.#core.context, argument0, result as ReturnType<ParseResult<H>>, plugin)) as Promise<Argument0<H>> | ||
} | ||
@@ -534,3 +534,3 @@ | ||
if (typeof hook === 'function') { | ||
const possiblePromiseResult = (hook as Function).apply({ ...this.#core.api, plugin }, parameters) as Promise<ReturnType<ParseResult<H>>> | ||
const possiblePromiseResult = (hook as Function).apply({ ...this.#core.context, plugin }, parameters) as Promise<ReturnType<ParseResult<H>>> | ||
@@ -596,3 +596,3 @@ if (isPromise(possiblePromiseResult)) { | ||
if (typeof hook === 'function') { | ||
const fn = (hook as Function).apply({ ...this.#core.api, plugin }, parameters) as ReturnType<ParseResult<H>> | ||
const fn = (hook as Function).apply({ ...this.#core.context, plugin }, parameters) as ReturnType<ParseResult<H>> | ||
@@ -631,3 +631,3 @@ output = fn | ||
pluginManager: PluginManager, | ||
context: PluginCore['api'] | undefined, | ||
context: PluginCore['context'] | undefined, | ||
): Plugin<GetPluginFactoryOptions<TPlugin>> { | ||
@@ -640,16 +640,7 @@ const usedPluginNames = pluginManager.#usedPluginNames | ||
// default transform | ||
if (!plugin.transform) { | ||
plugin.transform = function transform(_path, code) { | ||
return code | ||
} | ||
} | ||
if (plugin.api && typeof plugin.api === 'function') { | ||
const api = (plugin.api as Function).call(context) as typeof plugin.api | ||
if (plugin.context && typeof plugin.context === 'function') { | ||
return { | ||
...plugin, | ||
key, | ||
api, | ||
context: (plugin.context as Function).call(context) as typeof plugin.context, | ||
} as unknown as Plugin<GetPluginFactoryOptions<TPlugin>> | ||
@@ -687,4 +678,4 @@ } | ||
static get hooks() { | ||
return ['buildStart', 'resolvePath', 'resolveName', 'load', 'transform', 'writeFile', 'buildEnd'] as const | ||
return ['buildStart', 'resolvePath', 'resolveName', 'buildEnd'] as const | ||
} | ||
} |
@@ -26,7 +26,5 @@ import type * as KubbFile from '@kubb/fs/types' | ||
/** | ||
* Plugin type can be KubbJSONPlugin or Plugin | ||
* Example: ['@kubb/plugin-oas', { output: false }] | ||
* Or: pluginOas({ output: false }) | ||
* Plugin type should be a Kubb plugin | ||
*/ | ||
plugins?: Array<Omit<UnknownUserPlugin, 'api'>> | ||
plugins?: Array<Omit<UnknownUserPlugin, 'context'>> | ||
} | ||
@@ -116,5 +114,5 @@ | ||
/** | ||
* API that you want to expose to other plugins. | ||
* Context that you want to expose to other plugins. | ||
*/ | ||
TAPI = any, | ||
TContext = any, | ||
/** | ||
@@ -132,3 +130,3 @@ * When calling `resolvePath` you can specify better types. | ||
resolvedOptions: TResolvedOptions | ||
api: TAPI | ||
context: TContext | ||
resolvePathOptions: TResolvePathOptions | ||
@@ -161,8 +159,8 @@ } | ||
post?: Array<string> | ||
} & (TOptions['api'] extends never | ||
} & (TOptions['context'] extends never | ||
? { | ||
api?: never | ||
context?: never | ||
} | ||
: { | ||
api: (this: TOptions['name'] extends 'core' ? null : Omit<PluginContext<TOptions>, 'addFile'>) => TOptions['api'] | ||
context: (this: TOptions['name'] extends 'core' ? null : Omit<PluginContext<TOptions>, 'addFile'>) => TOptions['context'] | ||
}) | ||
@@ -199,10 +197,10 @@ | ||
/** | ||
* Define an api that can be used by other plugins, see `PluginManager' where we convert from `UserPlugin` to `Plugin`(used when calling `createPlugin`). | ||
* Define a context that can be used by other plugins, see `PluginManager' where we convert from `UserPlugin` to `Plugin`(used when calling `createPlugin`). | ||
*/ | ||
} & (TOptions['api'] extends never | ||
} & (TOptions['context'] extends never | ||
? { | ||
api?: never | ||
context?: never | ||
} | ||
: { | ||
api: TOptions['api'] | ||
context: TOptions['context'] | ||
}) | ||
@@ -233,17 +231,2 @@ | ||
/** | ||
* Makes it possible to run async logic to override the path defined previously by `resolvePath`. | ||
* @type hookFirst | ||
*/ | ||
load?: (this: Omit<PluginContext<TOptions>, 'addFile'>, path: KubbFile.Path) => PossiblePromise<TransformResult | null> | ||
/** | ||
* Transform the source-code. | ||
* @type hookReduceArg0 | ||
*/ | ||
transform?: (this: Omit<PluginContext<TOptions>, 'addFile'>, source: string, path: KubbFile.Path) => PossiblePromise<TransformResult> | ||
/** | ||
* Write the result to the file-system based on the id(defined by `resolvePath` or changed by `load`). | ||
* @type hookParallel | ||
*/ | ||
writeFile?: (this: Omit<PluginContext<TOptions>, 'addFile'>, path: KubbFile.Path, source: string | undefined) => PossiblePromise<string | void> | ||
/** | ||
* End of the plugin lifecycle. | ||
@@ -300,4 +283,1 @@ * @type hookParallel | ||
} | ||
// null will mean clear the watcher for this key | ||
export type TransformResult = string | null |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
792461
10224
+ Added@kubb/fs@3.0.0-alpha.2(transitive)
+ Added@kubb/parser-ts@3.0.0-alpha.2(transitive)
+ Added@kubb/types@3.0.0-alpha.2(transitive)
- Removed@kubb/fs@3.0.0-alpha.1(transitive)
- Removed@kubb/parser-ts@3.0.0-alpha.1(transitive)
- Removed@kubb/types@3.0.0-alpha.1(transitive)
Updated@kubb/fs@3.0.0-alpha.2
Updated@kubb/types@3.0.0-alpha.2