@kubb/core
Advanced tools
Comparing version
import EventEmitter from 'events'; | ||
import { DirectoryTreeOptions } from 'directory-tree'; | ||
declare class TreeNode<T = unknown> { | ||
data: T; | ||
parent?: TreeNode<T>; | ||
children: Array<TreeNode<T>>; | ||
constructor(data: T, parent?: TreeNode<T>); | ||
addChild(data: T): any; | ||
find(data: T): any; | ||
leaves(): this[]; | ||
root(): any; | ||
forEach(callback: (treeNode: TreeNode<T>) => void): this; | ||
static generate(path: string, options?: DirectoryTreeOptions): TreeNode<{ | ||
name: string; | ||
path: string; | ||
type: "file" | "directory"; | ||
}>; | ||
} | ||
/** | ||
@@ -88,5 +71,21 @@ * @deprecated Use userFile instead | ||
remove(id: UUID): void; | ||
static writeIndexes(root: string, output: string, options: Parameters<typeof TreeNode.generate>[1]): Promise<void>[]; | ||
} | ||
declare class TreeNode<T = unknown> { | ||
data: T; | ||
parent?: TreeNode<T>; | ||
children: Array<TreeNode<T>>; | ||
constructor(data: T, parent?: TreeNode<T>); | ||
addChild(data: T): any; | ||
find(data: T): any; | ||
leaves(): this[]; | ||
root(): any; | ||
forEach(callback: (treeNode: TreeNode<T>) => void): this; | ||
static generate(path: string, options?: DirectoryTreeOptions): TreeNode<{ | ||
name: string; | ||
path: string; | ||
type: "file" | "directory"; | ||
}>; | ||
} | ||
interface Cache<TCache = any> { | ||
@@ -93,0 +92,0 @@ delete(id: string): boolean; |
@@ -85,2 +85,57 @@ 'use strict'; | ||
// 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({ 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.addOrAppend(file); | ||
}, | ||
resolveId, | ||
load, | ||
cache: createPluginCache(/* @__PURE__ */ Object.create(null)) | ||
}; | ||
return { | ||
name, | ||
api, | ||
resolveId(fileName, directory) { | ||
if (!directory) { | ||
return null; | ||
} | ||
return pathParser.resolve(directory, fileName); | ||
} | ||
}; | ||
}); | ||
// src/managers/fileManager/events.ts | ||
@@ -133,78 +188,2 @@ var keys = { | ||
}; | ||
var TreeNode = class { | ||
data; | ||
parent; | ||
children; | ||
constructor(data, parent) { | ||
this.data = data; | ||
this.parent = parent; | ||
return this; | ||
} | ||
addChild(data) { | ||
const child = new TreeNode(data, this); | ||
if (!this.children) { | ||
this.children = []; | ||
} | ||
this.children.push(child); | ||
return child; | ||
} | ||
find(data) { | ||
if (data === this.data) { | ||
return this; | ||
} | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children, target = null; i < length; i++) { | ||
target = this.children[i].find(data); | ||
if (target) { | ||
return target; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
leaves() { | ||
if (!this.children || this.children.length === 0) { | ||
return [this]; | ||
} | ||
const leaves = []; | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children; i < length; i++) { | ||
leaves.push.apply(leaves, this.children[i].leaves()); | ||
} | ||
} | ||
return leaves; | ||
} | ||
root() { | ||
if (!this.parent) { | ||
return this; | ||
} | ||
return this.parent.root(); | ||
} | ||
forEach(callback) { | ||
if (typeof callback !== "function") { | ||
throw new TypeError("forEach() callback must be a function"); | ||
} | ||
callback(this); | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children; i < length; i++) { | ||
this.children[i].forEach(callback); | ||
} | ||
} | ||
return this; | ||
} | ||
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 }); | ||
const recurse = (node, item) => { | ||
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type }); | ||
if (item.children?.length) { | ||
item.children?.forEach((child) => { | ||
recurse(subNode, child); | ||
}); | ||
} | ||
}; | ||
filteredTree.children?.forEach((child) => recurse(treeNode, child)); | ||
return treeNode; | ||
} | ||
}; | ||
@@ -296,106 +275,80 @@ // src/managers/fileManager/FileManager.ts | ||
} | ||
static writeIndexes(root, output, options) { | ||
const tree = TreeNode.generate(output, { extensions: /\.ts/, ...options }); | ||
const fileReducer = (files2, item) => { | ||
if (!item.children) { | ||
return []; | ||
}; | ||
var TreeNode = class { | ||
data; | ||
parent; | ||
children; | ||
constructor(data, parent) { | ||
this.data = data; | ||
this.parent = parent; | ||
return this; | ||
} | ||
addChild(data) { | ||
const child = new TreeNode(data, this); | ||
if (!this.children) { | ||
this.children = []; | ||
} | ||
this.children.push(child); | ||
return child; | ||
} | ||
find(data) { | ||
if (data === this.data) { | ||
return this; | ||
} | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children, target = null; i < length; i++) { | ||
target = this.children[i].find(data); | ||
if (target) { | ||
return target; | ||
} | ||
} | ||
if (item.children?.length > 1) { | ||
const path = pathParser.resolve(root, item.data.path, "index.ts"); | ||
const source = item.children.reduce((source2, file) => { | ||
if (!file) { | ||
return source2; | ||
} | ||
const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`; | ||
if (importPath.includes("index") && path.includes("index")) { | ||
return source2; | ||
} | ||
return `${source2}export * from "${importPath}"; | ||
`; | ||
}, ""); | ||
files2.push({ | ||
path, | ||
fileName: "index.ts", | ||
source | ||
}); | ||
} else { | ||
} | ||
return null; | ||
} | ||
leaves() { | ||
if (!this.children || this.children.length === 0) { | ||
return [this]; | ||
} | ||
const leaves = []; | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children; i < length; i++) { | ||
leaves.push.apply(leaves, this.children[i].leaves()); | ||
} | ||
} | ||
return leaves; | ||
} | ||
root() { | ||
if (!this.parent) { | ||
return this; | ||
} | ||
return this.parent.root(); | ||
} | ||
forEach(callback) { | ||
if (typeof callback !== "function") { | ||
throw new TypeError("forEach() callback must be a function"); | ||
} | ||
callback(this); | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children; i < length; i++) { | ||
this.children[i].forEach(callback); | ||
} | ||
} | ||
return this; | ||
} | ||
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 }); | ||
const recurse = (node, item) => { | ||
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type }); | ||
if (item.children?.length) { | ||
item.children?.forEach((child) => { | ||
const path = pathParser.resolve(root, item.data.path, "index.ts"); | ||
const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`; | ||
files2.push({ | ||
path, | ||
fileName: "index.ts", | ||
source: `export * from "${importPath}"; | ||
` | ||
}); | ||
recurse(subNode, child); | ||
}); | ||
} | ||
item.children.forEach((childItem) => { | ||
fileReducer(files2, childItem); | ||
}); | ||
return files2; | ||
}; | ||
const files = fileReducer([], tree); | ||
return files.map((file) => write(file.source, file.path)); | ||
filteredTree.children?.forEach((child) => recurse(treeNode, child)); | ||
return treeNode; | ||
} | ||
}; | ||
// 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({ 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.addOrAppend(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 pathParser.resolve(directory, fileName); | ||
} | ||
}; | ||
}); | ||
// src/managers/pluginManager/PluginManager.ts | ||
@@ -402,0 +355,0 @@ var hookNames = { |
{ | ||
"name": "@kubb/core", | ||
"version": "0.30.0", | ||
"version": "0.30.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
125778
-7.51%1520
-5.88%