@kubb/core
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-alpha.5
@@ -1,2 +0,1 @@ | ||
import EventEmitter from 'events'; | ||
import { DirectoryTreeOptions } from 'directory-tree'; | ||
@@ -120,3 +119,3 @@ | ||
*/ | ||
validate: (this: PluginContext, plugins: KubbPlugin[]) => MaybePromise<true>; | ||
validate: (this: Omit<PluginContext, 'addFile'>, plugins: KubbPlugin[]) => MaybePromise<true>; | ||
/** | ||
@@ -132,3 +131,3 @@ * Start of the lifecycle of a plugin. | ||
*/ | ||
resolveId: (this: PluginContext, fileName: string, directory?: string, options?: TOptions['resolveIdOptions']) => OptionalPath; | ||
resolveId: (this: Omit<PluginContext, 'addFile'>, fileName: string, directory?: string, options?: TOptions['resolveIdOptions']) => OptionalPath; | ||
/** | ||
@@ -138,3 +137,3 @@ * Makes it possible to run async logic to override the path defined previously by `resolveId`. | ||
*/ | ||
load: (this: PluginContext, path: Path) => MaybePromise<TransformResult | null>; | ||
load: (this: Omit<PluginContext, 'addFile'>, path: Path) => MaybePromise<TransformResult | null>; | ||
/** | ||
@@ -144,3 +143,3 @@ * Transform the source-code. | ||
*/ | ||
transform: (this: PluginContext, source: string, path: Path) => MaybePromise<TransformResult>; | ||
transform: (this: Omit<PluginContext, 'addFile'>, source: string, path: Path) => MaybePromise<TransformResult>; | ||
/** | ||
@@ -150,3 +149,3 @@ * Write the result to the file-system based on the id(defined by `resolveId` or changed by `load`). | ||
*/ | ||
writeFile: (this: PluginContext, source: string | undefined, path: Path) => MaybePromise<void>; | ||
writeFile: (this: Omit<PluginContext, 'addFile'>, source: string | undefined, path: Path) => MaybePromise<void>; | ||
/** | ||
@@ -156,3 +155,3 @@ * End of the plugin lifecycle. | ||
*/ | ||
buildEnd: (this: PluginContext) => MaybePromise<void>; | ||
buildEnd: (this: Omit<PluginContext, 'addFile'>) => MaybePromise<void>; | ||
}; | ||
@@ -211,3 +210,3 @@ type PluginLifecycleHooks = keyof PluginLifecycle; | ||
name: string; | ||
}>(a: T, b: T): 0 | 1 | -1; | ||
}>(a: T, b: T): 1 | -1 | 0; | ||
@@ -222,2 +221,14 @@ declare function createJSDocBlockText({ comments }: { | ||
interface QueueTask<T> { | ||
(...args: any): Promise<T>; | ||
} | ||
declare class Queue { | ||
private readonly queue; | ||
workerCount: number; | ||
private maxParallel; | ||
constructor(maxParallel: number); | ||
run<T>(task: QueueTask<T>): Promise<T>; | ||
private work; | ||
} | ||
type Import = { | ||
@@ -250,22 +261,13 @@ name: string | string[]; | ||
private cache; | ||
emitter: EventEmitter; | ||
events: { | ||
emitFile: (id: string, file: File) => void; | ||
emitStatusChange: (file: File) => void; | ||
emitStatusChangeById: (id: string, status: Status) => void; | ||
emitSuccess: () => void; | ||
emitRemove: (id: string, file: File) => void; | ||
onAdd: (callback: (id: string, file: File) => void) => () => void; | ||
onStatusChange: (callback: (file: File) => void) => () => void; | ||
onStatusChangeById: (id: string, callback: (status: Status) => void) => () => void; | ||
onSuccess: (callback: () => void) => () => void; | ||
onRemove: (id: string, callback: (file: File) => void) => () => void; | ||
}; | ||
constructor(); | ||
private task?; | ||
private queue?; | ||
constructor(options?: { | ||
queue: Queue; | ||
task: QueueTask<unknown>; | ||
}); | ||
private getCache; | ||
private getCacheByPath; | ||
private getCountByStatus; | ||
getSource(file: File): string; | ||
get files(): File[]; | ||
add(file: File, timeoutMs?: number): Promise<File>; | ||
add(file: File): Promise<File>; | ||
addOrAppend(file: File): Promise<File>; | ||
@@ -293,3 +295,3 @@ combine(files: Array<File | null>): File[]; | ||
path: string; | ||
type: "file" | "directory"; | ||
type: "directory" | "file"; | ||
}> | undefined; | ||
@@ -355,4 +357,6 @@ } | ||
readonly core: KubbPlugin<CorePluginOptions>; | ||
queue: Queue; | ||
constructor(config: KubbConfig, options: { | ||
logger?: Logger; | ||
task: QueueTask<unknown>; | ||
}); | ||
@@ -407,2 +411,2 @@ resolveId: (params: ResolveIdParams) => Promise<OptionalPath>; | ||
export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, File, FileManager, FileName, Generator, KubbBuild, KubbConfig, KubbJSONPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, OptionalPath, Path, PathMode, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, ResolveIdParams, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, UUID, ValidationPluginError, build, clean, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getPathMode, getRelativePath, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, timeout, validatePlugins, write }; | ||
export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, File, FileManager, FileName, Generator, KubbBuild, KubbConfig, KubbJSONPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, OptionalPath, Path, PathMode, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, Queue, QueueTask, ResolveIdParams, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, UUID, ValidationPluginError, build, clean, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getPathMode, getRelativePath, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, timeout, validatePlugins, write }; |
@@ -5,3 +5,2 @@ import { createRequire } from 'module'; | ||
import rimraf from 'rimraf'; | ||
import EventEmitter from 'events'; | ||
import uniq from 'lodash.uniq'; | ||
@@ -174,2 +173,29 @@ import { v4 } from 'uuid'; | ||
// src/utils/queue.ts | ||
var Queue = class { | ||
queue = []; | ||
workerCount = 0; | ||
maxParallel; | ||
constructor(maxParallel) { | ||
this.maxParallel = maxParallel; | ||
} | ||
run(task) { | ||
return new Promise((resolve, reject) => { | ||
this.queue.push({ reject, resolve, task }); | ||
this.work(); | ||
}); | ||
} | ||
async work() { | ||
if (this.workerCount >= this.maxParallel) | ||
return; | ||
this.workerCount++; | ||
let entry; | ||
while (entry = this.queue.shift()) { | ||
const { reject, resolve, task } = entry; | ||
task().then((result) => resolve(result)).catch((err) => reject(err)); | ||
} | ||
this.workerCount--; | ||
} | ||
}; | ||
// src/plugin.ts | ||
@@ -217,68 +243,11 @@ function createPlugin(factory) { | ||
}); | ||
// src/managers/fileManager/events.ts | ||
var keys = { | ||
getFileKey: () => `file`, | ||
getStatusChangeKey: () => `status-change`, | ||
getStatusChangeByIdKey: (id) => `${id}-status-change`, | ||
getSuccessKey: () => `success`, | ||
getRemoveKey: (id) => `${id}remove` | ||
}; | ||
function getFileManagerEvents(emitter) { | ||
return { | ||
/** | ||
* Emiter | ||
*/ | ||
emitFile: (id, file) => { | ||
emitter.emit(keys.getFileKey(), id, file); | ||
}, | ||
emitStatusChange: (file) => { | ||
emitter.emit(keys.getStatusChangeKey(), file); | ||
}, | ||
emitStatusChangeById: (id, status) => { | ||
emitter.emit(keys.getStatusChangeByIdKey(id), status); | ||
}, | ||
emitSuccess: () => { | ||
emitter.emit(keys.getSuccessKey()); | ||
}, | ||
emitRemove: (id, file) => { | ||
emitter.emit(keys.getRemoveKey(id), file); | ||
}, | ||
/** | ||
* Listeners | ||
*/ | ||
onAdd: (callback) => { | ||
emitter.on(keys.getFileKey(), callback); | ||
return () => emitter.removeListener(keys.getFileKey(), callback); | ||
}, | ||
onStatusChange: (callback) => { | ||
emitter.on(keys.getStatusChangeKey(), callback); | ||
return () => emitter.removeListener(keys.getStatusChangeKey(), callback); | ||
}, | ||
onStatusChangeById: (id, callback) => { | ||
emitter.on(keys.getStatusChangeByIdKey(id), callback); | ||
return () => emitter.removeListener(keys.getStatusChangeByIdKey(id), callback); | ||
}, | ||
onSuccess: (callback) => { | ||
emitter.on(keys.getSuccessKey(), callback); | ||
return () => emitter.removeListener(keys.getSuccessKey(), callback); | ||
}, | ||
onRemove: (id, callback) => { | ||
emitter.on(keys.getRemoveKey(id), callback); | ||
return () => emitter.removeListener(keys.getRemoveKey(id), callback); | ||
} | ||
}; | ||
} | ||
// src/managers/fileManager/FileManager.ts | ||
var FileManager = class { | ||
cache = /* @__PURE__ */ new Map(); | ||
emitter = new EventEmitter(); | ||
events = getFileManagerEvents(this.emitter); | ||
constructor() { | ||
this.events.onStatusChange(() => { | ||
if (this.getCountByStatus("removed") === this.cache.size) { | ||
this.events.emitSuccess(); | ||
} | ||
}); | ||
task; | ||
queue; | ||
constructor(options) { | ||
if (options) { | ||
this.task = options.task; | ||
this.queue = options.queue; | ||
} | ||
} | ||
@@ -297,11 +266,2 @@ getCache(id) { | ||
} | ||
getCountByStatus(status) { | ||
let count = 0; | ||
this.cache.forEach((item) => { | ||
if (item.status === status) { | ||
count++; | ||
} | ||
}); | ||
return count; | ||
} | ||
getSource(file) { | ||
@@ -343,15 +303,11 @@ if (!file.fileName.endsWith(".ts")) { | ||
} | ||
async add(file, timeoutMs = 0) { | ||
if (timeoutMs) { | ||
await timeout(timeoutMs); | ||
} | ||
async add(file) { | ||
const cacheItem = { id: v4(), file, status: "new" }; | ||
this.cache.set(cacheItem.id, cacheItem); | ||
this.events.emitFile(cacheItem.id, file); | ||
return new Promise((resolve) => { | ||
const unsubscribe = this.events.onRemove(cacheItem.id, (file2) => { | ||
resolve(file2); | ||
unsubscribe(); | ||
if (this.queue) { | ||
await this.queue.run(async () => { | ||
await this.task?.(cacheItem.id, file); | ||
}); | ||
}); | ||
} | ||
return file; | ||
} | ||
@@ -395,4 +351,2 @@ addOrAppend(file) { | ||
this.cache.set(id, cacheItem); | ||
this.events.emitStatusChange(cacheItem.file); | ||
this.events.emitStatusChangeById(id, status); | ||
} | ||
@@ -409,3 +363,2 @@ get(id) { | ||
this.setStatus(id, "removed"); | ||
this.events.emitRemove(id, cacheItem.file); | ||
} | ||
@@ -516,6 +469,8 @@ async write(...params) { | ||
core; | ||
queue; | ||
constructor(config, options) { | ||
this.logger = options.logger; | ||
this.config = config; | ||
this.fileManager = new FileManager(); | ||
this.queue = new Queue(10); | ||
this.fileManager = new FileManager({ task: options.task, queue: this.queue }); | ||
this.core = definePlugin({ | ||
@@ -706,12 +661,3 @@ config, | ||
} | ||
const pluginManager = new PluginManager(config, { logger }); | ||
const { plugins, fileManager } = pluginManager; | ||
await pluginManager.hookParallel("validate", [plugins]); | ||
fileManager.events.onSuccess(async () => { | ||
await pluginManager.hookParallel("buildEnd"); | ||
setTimeout(() => { | ||
done({ files: fileManager.files.map((file) => ({ ...file, source: fileManager.getSource(file) })) }); | ||
}, 500); | ||
}); | ||
fileManager.events.onAdd(async (id, file) => { | ||
const queueTask = async (id, file) => { | ||
const { path } = file; | ||
@@ -729,6 +675,11 @@ let code = fileManager.getSource(file); | ||
} | ||
fileManager.setStatus(id, "success"); | ||
fileManager.remove(id); | ||
}); | ||
}; | ||
const pluginManager = new PluginManager(config, { logger, task: queueTask }); | ||
const { plugins, fileManager } = pluginManager; | ||
await pluginManager.hookParallel("validate", [plugins]); | ||
await pluginManager.hookParallel("buildStart", [config]); | ||
await pluginManager.hookParallel("buildEnd"); | ||
setTimeout(() => { | ||
done({ files: fileManager.files.map((file) => ({ ...file, source: fileManager.getSource(file) })) }); | ||
}, 500); | ||
pluginManager.fileManager.add({ | ||
@@ -777,4 +728,4 @@ path: isURL(config.input.path) ? config.input.path : pathParser2.resolve(config.root, config.input.path), | ||
export { FileManager, Generator, PluginManager, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getPathMode, getRelativePath, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, timeout, validatePlugins, write }; | ||
export { FileManager, Generator, PluginManager, Queue, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getPathMode, getRelativePath, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, timeout, validatePlugins, write }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@kubb/core", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-alpha.5", | ||
"description": "Generator core", | ||
@@ -5,0 +5,0 @@ "repository": { |
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
151013
1882