@kubb/core
Advanced tools
Comparing version 0.28.0 to 0.28.1
import EventEmitter from 'events'; | ||
import { DirectoryTreeOptions } from 'directory-tree'; | ||
type GenerateTreeNodeOptions = { | ||
extensions?: RegExp; | ||
exclude?: RegExp[]; | ||
}; | ||
declare class TreeNode<T = unknown> { | ||
@@ -17,3 +14,3 @@ data: T; | ||
forEach(callback: (treeNode: TreeNode<T>) => void): this; | ||
static generate(path: string, options?: GenerateTreeNodeOptions): TreeNode<{ | ||
static generate(path: string, options?: DirectoryTreeOptions): TreeNode<{ | ||
name: string; | ||
@@ -69,4 +66,2 @@ path: string; | ||
private cache; | ||
private readonly root; | ||
private readonly output; | ||
emitter: EventEmitter; | ||
@@ -79,21 +74,12 @@ events: { | ||
emitRemove: (id: string, file: File) => void; | ||
onAdd: (callback: (id: string, file: File) => void) => VoidFunction; | ||
onStatusChange: (callback: (file: File) => void) => VoidFunction; | ||
onStatusChangeById: (id: string, callback: (status: Status) => void) => VoidFunction; | ||
onSuccess: (callback: () => void) => VoidFunction; | ||
onRemove: (id: string, callback: (file: File) => void) => VoidFunction; | ||
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(options?: { | ||
root?: string; | ||
output?: string; | ||
}); | ||
constructor(); | ||
private getCache; | ||
private getCountOfStatus; | ||
get files(): File[]; | ||
get tree(): TreeNode<{ | ||
name: string; | ||
path: string; | ||
type: "file" | "directory"; | ||
}>; | ||
generateRootFiles(options: GenerateTreeNodeOptions): Promise<void>[]; | ||
add(file: File): Promise<File>; | ||
@@ -103,2 +89,3 @@ setStatus(id: UUID, status: Status): void; | ||
remove(id: UUID): void; | ||
static writeIndexes(root: string, output: string, options: Parameters<typeof TreeNode.generate>[1]): Promise<void>[]; | ||
} | ||
@@ -133,6 +120,6 @@ | ||
root: string; | ||
mode?: 'single'; | ||
input: { | ||
/** | ||
* Path or link to the input file | ||
* Path to be used as the input. Can be an absolute path, or a path relative from | ||
* the defined root option. | ||
*/ | ||
@@ -143,5 +130,9 @@ path: string; | ||
/** | ||
* Path to export folder | ||
* Path to be used to export all generated files. Can be an absolute path, or a path relative from | ||
* the defined root option. | ||
*/ | ||
path: string; | ||
/** | ||
* Remove previous generated files and folders. | ||
*/ | ||
clean?: boolean; | ||
@@ -151,10 +142,12 @@ }; | ||
* Array of Kubb plugins to use. | ||
* The plugin/package can forsee some options that you need to pass through. | ||
* Sometimes a plugin is depended on another plugin, if that's the case you will get an error back from the plugin you installed. | ||
*/ | ||
plugins?: KubbPlugin[]; | ||
/** | ||
* Hooks that can be called when a specific action is done in Kubb. | ||
* Hooks that will be called when a specific action is triggered in Kubb. | ||
*/ | ||
hooks?: { | ||
/** | ||
* Hook that will be called at the end of all executions. | ||
* Hook that will be triggerend at the end of all executions. | ||
*/ | ||
@@ -183,8 +176,37 @@ done?: string | string[]; | ||
type PluginLifecycle = { | ||
/** | ||
* Valdiate all plugins to see if their depended plugins are installed and configured. | ||
* @type hookParallel | ||
*/ | ||
validate: (this: PluginContext, plugins: KubbPlugin[]) => MaybePromise<ValidationResult>; | ||
/** | ||
* Start of the lifecycle of a plugin. | ||
* @type hookParallel | ||
*/ | ||
buildStart: (this: PluginContext, kubbConfig: KubbConfig) => MaybePromise<void>; | ||
resolveId: (this: PluginContext, importee: string, importer?: string, options?: Record<string, any>) => Path; | ||
load: (this: PluginContext, id: string) => MaybePromise<TransformResult | null>; | ||
transform: (this: PluginContext, code: string, id: string) => MaybePromise<TransformResult>; | ||
writeFile: (this: PluginContext, code: string | undefined, id: string) => MaybePromise<void>; | ||
/** | ||
* Resolve to an id based on importee(example: `./Pet.ts`) and directory(example: `./models`). | ||
* @type hookFirst | ||
* @example ('./Pet.ts', './src/gen/') | ||
*/ | ||
resolveId: (this: PluginContext, fileName: string, directory?: string, options?: Record<string, any>) => OptionalPath; | ||
/** | ||
* Makes it possible to run async logic to override the path defined previously by `resolveId`. | ||
* @type hookFirst | ||
*/ | ||
load: (this: PluginContext, path: Path) => MaybePromise<TransformResult | null>; | ||
/** | ||
* Transform the source-code. | ||
* @type hookReduceArg0 | ||
*/ | ||
transform: (this: PluginContext, source: string, path: Path) => MaybePromise<TransformResult>; | ||
/** | ||
* Write the result to the file-system based on the id(defined by `resolveId` or changed by `load`). | ||
* @type hookParallel | ||
*/ | ||
writeFile: (this: PluginContext, source: string | undefined, path: Path) => MaybePromise<void>; | ||
/** | ||
* End of the plugin lifecycle. | ||
* @type hookParallel | ||
*/ | ||
buildEnd: (this: PluginContext) => MaybePromise<void>; | ||
@@ -194,4 +216,4 @@ }; | ||
type ResolveIdParams = { | ||
importee: string; | ||
importer?: string | undefined; | ||
fileName: string; | ||
directory?: string | undefined; | ||
/** | ||
@@ -202,2 +224,5 @@ * When set, resolveId will only call resolveId of the name of the plugin set here. | ||
pluginName?: string; | ||
/** | ||
* Options to be passed to 'resolveId' 3th parameter | ||
*/ | ||
options?: Record<string, any>; | ||
@@ -212,3 +237,3 @@ }; | ||
}) => Promise<File>; | ||
resolveId: (params: ResolveIdParams) => MaybePromise<Path>; | ||
resolveId: (params: ResolveIdParams) => MaybePromise<OptionalPath>; | ||
load: (id: string) => MaybePromise<TransformResult | void>; | ||
@@ -223,3 +248,4 @@ }; | ||
*/ | ||
type Path = string | null | undefined; | ||
type Path = string; | ||
type OptionalPath = Path | null | undefined; | ||
type FileName = string | null | undefined; | ||
@@ -296,3 +322,3 @@ type LogType = 'error' | 'warn' | 'info'; | ||
}); | ||
resolveId: (params: ResolveIdParams) => Promise<Path>; | ||
resolveId: (params: ResolveIdParams) => Promise<OptionalPath>; | ||
load: (id: string) => Promise<TransformResult>; | ||
@@ -322,2 +348,2 @@ hookForPlugin<H extends PluginLifecycleHooks>(pluginName: string, hookName: H, parameters: Parameters<PluginLifecycle[H]>, skipped?: ReadonlySet<KubbPlugin> | null): Promise<ReturnType<PluginLifecycle[H]> | null>; | ||
export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, EmittedFile, File, FileManager, FileName, GenerateTreeNodeOptions, KubbConfig, KubbPlugin, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, Path, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, ResolveIdParams, Status, Strategy, TransformResult, TreeNode, UUID, ValidationResult, build, createPlugin, createPluginCache, build as default, defineConfig, format, getRelativePath, hooks, isPromise, write }; | ||
export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, EmittedFile, File, FileManager, FileName, KubbConfig, KubbPlugin, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, OptionalPath, Path, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, ResolveIdParams, Status, Strategy, TransformResult, TreeNode, UUID, ValidationResult, build, createPlugin, createPluginCache, build as default, defineConfig, format, getRelativePath, hooks, isPromise, write }; |
@@ -5,4 +5,4 @@ 'use strict'; | ||
var path3 = require('path'); | ||
var fse = require('fs-extra'); | ||
var pathParser2 = require('path'); | ||
var prettier = require('prettier'); | ||
@@ -33,7 +33,7 @@ var EventEmitter = require('events'); | ||
// src/utils/write.ts | ||
var write = async (data, path5, options = { format: false }) => { | ||
var write = async (data, path, options = { format: false }) => { | ||
const formattedData = options.format ? format(data) : data; | ||
try { | ||
await fse.stat(path5); | ||
const oldContent = await fse.readFile(path5, { encoding: "utf-8" }); | ||
await fse.stat(path); | ||
const oldContent = await fse.readFile(path, { encoding: "utf-8" }); | ||
if (oldContent?.toString() === formattedData) { | ||
@@ -43,5 +43,5 @@ return; | ||
} catch (_err) { | ||
return fse.outputFile(path5, formattedData); | ||
return fse.outputFile(path, formattedData); | ||
} | ||
return fse.outputFile(path5, formattedData); | ||
return fse.outputFile(path, formattedData); | ||
}; | ||
@@ -78,64 +78,6 @@ | ||
} | ||
const newPath = path3.relative(root, file).replace("../", "").replace(".ts", "").trimEnd(); | ||
const newPath = pathParser2.relative(root, file).replace("../", "").replace(".ts", "").trimEnd(); | ||
return `./${newPath}`; | ||
}; | ||
// src/plugin.ts | ||
function createPlugin(factory) { | ||
return (options) => { | ||
const plugin = factory(options); | ||
if (Array.isArray(plugin)) { | ||
throw new Error("Not implemented"); | ||
} | ||
if (!plugin.transform) { | ||
plugin.transform = function transform(code) { | ||
return code; | ||
}; | ||
} | ||
return plugin; | ||
}; | ||
} | ||
var name = "core"; | ||
var isEmittedFile = (result) => { | ||
return !!result.id; | ||
}; | ||
var definePlugin = createPlugin((options) => { | ||
const { fileManager, resolveId, load } = options; | ||
const api = { | ||
get config() { | ||
return options.config; | ||
}, | ||
fileManager, | ||
async addFile(file, options2) { | ||
if (isEmittedFile(file)) { | ||
const resolvedId = await resolveId({ importee: file.id, importer: file.importer, options: file.options }); | ||
const filePath = resolvedId || file.importer || file.id; | ||
return fileManager.add({ | ||
path: filePath, | ||
fileName: file.name || file.id, | ||
source: file.source || "" | ||
}); | ||
} | ||
if (options2?.root) ; | ||
return fileManager.add(file); | ||
}, | ||
resolveId, | ||
load, | ||
cache: createPluginCache(/* @__PURE__ */ Object.create(null)) | ||
}; | ||
return { | ||
name, | ||
api, | ||
async buildEnd() { | ||
await this.fileManager.generateRootFiles({ exclude: [/schemas/, /json/] }); | ||
}, | ||
resolveId(importee, importer) { | ||
if (!importer) { | ||
return null; | ||
} | ||
return path3.resolve(importer, importee); | ||
} | ||
}; | ||
}); | ||
// src/managers/fileManager/events.ts | ||
@@ -249,4 +191,4 @@ var keys = { | ||
} | ||
static generate(path5, options = {}) { | ||
const filteredTree = dirTree(path5, { extensions: options?.extensions, exclude: options.exclude }); | ||
static generate(path, options = {}) { | ||
const filteredTree = dirTree(path, { extensions: options?.extensions, exclude: options.exclude }); | ||
const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type }); | ||
@@ -269,9 +211,5 @@ const recurse = (node, item) => { | ||
cache = /* @__PURE__ */ new Map(); | ||
root; | ||
output; | ||
emitter = new EventEmitter(); | ||
events = getFileManagerEvents(this.emitter); | ||
constructor(options = {}) { | ||
this.root = path3.resolve(options.root || "."); | ||
this.output = options?.output || process.cwd(); | ||
constructor() { | ||
this.events.onStatusChange(() => { | ||
@@ -302,7 +240,37 @@ if (this.getCountOfStatus("removed") === this.cache.size) { | ||
} | ||
get tree() { | ||
return TreeNode.generate(this.output, { extensions: /\.ts/ }); | ||
add(file) { | ||
const cacheItem = { id: crypto.randomUUID(), 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(); | ||
}); | ||
}); | ||
} | ||
generateRootFiles(options) { | ||
const tree = TreeNode.generate(this.output, { extensions: /\.ts/, ...options }); | ||
setStatus(id, status) { | ||
const cacheItem = this.getCache(id); | ||
if (!cacheItem) { | ||
return; | ||
} | ||
cacheItem.status = status; | ||
this.cache.set(id, cacheItem); | ||
this.events.emitStatusChange(cacheItem.file); | ||
this.events.emitStatusChangeById(id, status); | ||
} | ||
get(id) { | ||
const cacheItem = this.getCache(id); | ||
return cacheItem?.file; | ||
} | ||
remove(id) { | ||
const cacheItem = this.getCache(id); | ||
if (!cacheItem) { | ||
return; | ||
} | ||
this.setStatus(id, "removed"); | ||
this.events.emitRemove(id, cacheItem.file); | ||
} | ||
static writeIndexes(root, output, options) { | ||
const tree = TreeNode.generate(output, { extensions: /\.ts/, ...options }); | ||
const fileReducer = (files2, item) => { | ||
@@ -322,3 +290,3 @@ if (!item.children) { | ||
files2.push({ | ||
path: path3.resolve(this.root, item.data.path, "index.ts"), | ||
path: pathParser2.resolve(root, item.data.path, "index.ts"), | ||
fileName: "index.ts", | ||
@@ -331,3 +299,3 @@ source | ||
files2.push({ | ||
path: path3.resolve(this.root, item.data.path, "index.ts"), | ||
path: pathParser2.resolve(root, item.data.path, "index.ts"), | ||
fileName: "index.ts", | ||
@@ -347,36 +315,61 @@ source: `export * from "${importPath}"; | ||
} | ||
add(file) { | ||
const cacheItem = { id: crypto.randomUUID(), 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(); | ||
}); | ||
}); | ||
} | ||
setStatus(id, status) { | ||
const cacheItem = this.getCache(id); | ||
if (!cacheItem) { | ||
return; | ||
}; | ||
// src/plugin.ts | ||
function createPlugin(factory) { | ||
return (options) => { | ||
const plugin = factory(options); | ||
if (Array.isArray(plugin)) { | ||
throw new Error("Not implemented"); | ||
} | ||
cacheItem.status = status; | ||
this.cache.set(id, cacheItem); | ||
this.events.emitStatusChange(cacheItem.file); | ||
this.events.emitStatusChangeById(id, status); | ||
} | ||
get(id) { | ||
const cacheItem = this.getCache(id); | ||
return cacheItem?.file; | ||
} | ||
remove(id) { | ||
const cacheItem = this.getCache(id); | ||
if (!cacheItem) { | ||
return; | ||
if (!plugin.transform) { | ||
plugin.transform = function transform(code) { | ||
return code; | ||
}; | ||
} | ||
this.setStatus(id, "removed"); | ||
this.events.emitRemove(id, cacheItem.file); | ||
} | ||
return plugin; | ||
}; | ||
} | ||
var name = "core"; | ||
var isEmittedFile = (result) => { | ||
return !!result.id; | ||
}; | ||
var definePlugin = createPlugin((options) => { | ||
const { fileManager, resolveId, load } = options; | ||
const api = { | ||
get config() { | ||
return options.config; | ||
}, | ||
fileManager, | ||
async addFile(file, options2) { | ||
if (isEmittedFile(file)) { | ||
const resolvedId = await resolveId({ fileName: file.id, directory: file.importer, options: file.options }); | ||
const path = resolvedId || file.importer || file.id; | ||
return fileManager.add({ | ||
path, | ||
fileName: file.name || file.id, | ||
source: file.source || "" | ||
}); | ||
} | ||
if (options2?.root) ; | ||
return fileManager.add(file); | ||
}, | ||
resolveId, | ||
load, | ||
cache: createPluginCache(/* @__PURE__ */ Object.create(null)) | ||
}; | ||
return { | ||
name, | ||
api, | ||
async buildEnd() { | ||
await FileManager.writeIndexes(this.config.root, this.config.output.path, { exclude: [/schemas/, /json/] }); | ||
}, | ||
resolveId(fileName, directory) { | ||
if (!directory) { | ||
return null; | ||
} | ||
return pathParser2.resolve(directory, fileName); | ||
} | ||
}; | ||
}); | ||
@@ -403,3 +396,3 @@ // src/managers/pluginManager/PluginManager.ts | ||
this.config = config; | ||
this.fileManager = new FileManager({ root: this.config.root, output: this.config.output.path }); | ||
this.fileManager = new FileManager(); | ||
this.core = definePlugin({ | ||
@@ -415,5 +408,5 @@ config, | ||
if (params.pluginName) { | ||
return this.hookForPlugin(params.pluginName, "resolveId", [params.importee, params.importer, params.options]); | ||
return this.hookForPlugin(params.pluginName, "resolveId", [params.fileName, params.directory, params.options]); | ||
} | ||
return this.hookFirst("resolveId", [params.importee, params.importer, params.options]); | ||
return this.hookFirst("resolveId", [params.fileName, params.directory, params.options]); | ||
}; | ||
@@ -548,3 +541,2 @@ load = async (id) => { | ||
const { plugins, fileManager } = pluginManager; | ||
fse.readFileSync(path3.resolve(config.root, config.input.path), "utf-8"); | ||
const validations = await pluginManager.hookParallel("validate", [plugins]); | ||
@@ -551,0 +543,0 @@ const validationsWithMessage = validations.filter(Boolean); |
{ | ||
"name": "@kubb/core", | ||
"version": "0.28.0", | ||
"version": "0.28.1", | ||
"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
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
1529
129402
26