@kubb/core
Advanced tools
Comparing version 1.1.6 to 1.1.7
import { createRequire } from 'module'; | ||
import crypto from 'node:crypto'; | ||
import { promises } from 'node:fs'; | ||
import pathParser2 from 'node:path'; | ||
import { promises } from 'node:fs'; | ||
import { camelCase, camelCaseTransformMerge } from 'change-case'; | ||
import { rimraf } from 'rimraf'; | ||
import dirTree from 'directory-tree'; | ||
import crypto from 'node:crypto'; | ||
import { createImportDeclaration, print, createExportDeclaration } from '@kubb/ts-codegen'; | ||
@@ -12,22 +12,2 @@ | ||
// src/managers/pluginManager/PluginError.ts | ||
var PluginError = class extends Error { | ||
pluginManager; | ||
constructor(message, options) { | ||
super(message, { cause: options.cause }); | ||
this.pluginManager = options.pluginManager; | ||
} | ||
}; | ||
// src/managers/pluginManager/ParallelPluginError.ts | ||
var ParallelPluginError = class extends Error { | ||
errors; | ||
pluginManager; | ||
constructor(message, options) { | ||
super(message, { cause: options.cause }); | ||
this.errors = options.errors; | ||
this.pluginManager = options.pluginManager; | ||
} | ||
}; | ||
// src/utils/isPromise.ts | ||
@@ -418,72 +398,3 @@ function isPromise(result) { | ||
// 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 definePlugin = createPlugin((options) => { | ||
const { fileManager, resolvePath, resolveName, load } = options; | ||
const api = { | ||
get config() { | ||
return options.config; | ||
}, | ||
fileManager, | ||
async addFile(...files) { | ||
const trace = getStackTrace(); | ||
const plugins = options.config.plugins?.filter((plugin) => trace[1].getFileName()?.includes(plugin.name)).sort((a, b) => { | ||
if (a.name.length < b.name.length) | ||
return 1; | ||
if (a.name.length > b.name.length) | ||
return -1; | ||
return 0; | ||
}); | ||
const pluginName = plugins?.[0].name; | ||
return Promise.all( | ||
files.map((file) => { | ||
const fileWithMeta = { | ||
...file, | ||
meta: { | ||
...file.meta || {}, | ||
pluginName | ||
} | ||
}; | ||
if (file.override) { | ||
return fileManager.add(fileWithMeta); | ||
} | ||
return fileManager.addOrAppend(fileWithMeta); | ||
}) | ||
); | ||
}, | ||
resolvePath, | ||
resolveName: (params) => { | ||
const name2 = resolveName(params); | ||
return transformReservedWord(name2); | ||
}, | ||
load, | ||
cache: createPluginCache(/* @__PURE__ */ Object.create(null)) | ||
}; | ||
return { | ||
name, | ||
options, | ||
api, | ||
resolvePath(fileName) { | ||
const root = pathParser2.resolve(this.config.root, this.config.output.path); | ||
return pathParser2.resolve(root, fileName); | ||
}, | ||
resolveName(name2) { | ||
return name2; | ||
} | ||
}; | ||
}); | ||
// src/managers/fileManager/FileManager.ts | ||
var FileManager = class { | ||
@@ -580,3 +491,219 @@ cache = /* @__PURE__ */ new Map(); | ||
}; | ||
function writeIndexes(root, options) { | ||
const tree = TreeNode.build(root, { extensions: /\.ts/, ...options }); | ||
if (!tree) { | ||
return void 0; | ||
} | ||
const fileReducer = (files2, item) => { | ||
if (!item.children) { | ||
return []; | ||
} | ||
if (item.children?.length > 1) { | ||
const path = pathParser2.resolve(item.data.path, "index.ts"); | ||
const exports = item.children.map((file) => { | ||
if (!file) { | ||
return void 0; | ||
} | ||
const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`; | ||
if (importPath.includes("index") && path.includes("index")) { | ||
return void 0; | ||
} | ||
return { path: importPath }; | ||
}).filter(Boolean); | ||
files2.push({ | ||
path, | ||
fileName: "index.ts", | ||
source: "", | ||
exports | ||
}); | ||
} else { | ||
item.children?.forEach((child) => { | ||
const path = pathParser2.resolve(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: "", | ||
exports: [{ path: importPath }] | ||
}); | ||
}); | ||
} | ||
item.children.forEach((childItem) => { | ||
fileReducer(files2, childItem); | ||
}); | ||
return files2; | ||
}; | ||
const files = fileReducer([], tree); | ||
return files; | ||
} | ||
function combineFiles(files) { | ||
return files.filter(Boolean).reduce((acc, curr) => { | ||
if (!curr) { | ||
return acc; | ||
} | ||
const prevIndex = acc.findIndex((item) => item.path === curr.path); | ||
if (prevIndex !== -1) { | ||
const prev = acc[prevIndex]; | ||
acc[prevIndex] = { | ||
...curr, | ||
source: `${prev.source} | ||
${curr.source}`, | ||
imports: [...prev.imports || [], ...curr.imports || []], | ||
exports: [...prev.exports || [], ...curr.exports || []] | ||
}; | ||
} else { | ||
acc.push(curr); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
function getFileSource(file) { | ||
let { source } = file; | ||
if (!file.fileName.endsWith(".ts")) { | ||
return file.source; | ||
} | ||
const imports = []; | ||
const exports = []; | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
imports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name | ||
}); | ||
} | ||
if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) { | ||
imports.push(curr); | ||
} | ||
if (exists && Array.isArray(exists.name)) { | ||
if (Array.isArray(curr.name)) { | ||
exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])]; | ||
} | ||
} | ||
}); | ||
file.exports?.forEach((curr) => { | ||
const exists = exports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
exports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name | ||
}); | ||
} | ||
if (exists && !Array.isArray(exists.name) && exists.name !== curr.name && exists.asAlias === curr.asAlias) { | ||
exports.push(curr); | ||
} | ||
if (exists && Array.isArray(exists.name)) { | ||
if (Array.isArray(curr.name)) { | ||
exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])]; | ||
} | ||
} | ||
}); | ||
const importNodes = imports.reduce((prev, curr) => { | ||
return [...prev, createImportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly })]; | ||
}, []); | ||
const importSource = print(importNodes); | ||
const exportNodes = exports.reduce((prev, curr) => { | ||
return [...prev, createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })]; | ||
}, []); | ||
const exportSource = print(exportNodes); | ||
if (importSource) { | ||
source = `${importSource} | ||
${source}`; | ||
} | ||
if (exportSource) { | ||
source = `${exportSource} | ||
${source}`; | ||
} | ||
return source; | ||
} | ||
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 definePlugin = createPlugin((options) => { | ||
const { fileManager, resolvePath, resolveName, load } = options; | ||
const api = { | ||
get config() { | ||
return options.config; | ||
}, | ||
fileManager, | ||
async addFile(...files) { | ||
const trace = getStackTrace(); | ||
const plugins = options.config.plugins?.filter((plugin) => trace[1].getFileName()?.includes(plugin.name)).sort((a, b) => { | ||
if (a.name.length < b.name.length) | ||
return 1; | ||
if (a.name.length > b.name.length) | ||
return -1; | ||
return 0; | ||
}); | ||
const pluginName = plugins?.[0].name; | ||
return Promise.all( | ||
files.map((file) => { | ||
const fileWithMeta = { | ||
...file, | ||
meta: { | ||
...file.meta || {}, | ||
pluginName | ||
} | ||
}; | ||
if (file.override) { | ||
return fileManager.add(fileWithMeta); | ||
} | ||
return fileManager.addOrAppend(fileWithMeta); | ||
}) | ||
); | ||
}, | ||
resolvePath, | ||
resolveName: (params) => { | ||
const name2 = resolveName(params); | ||
return transformReservedWord(name2); | ||
}, | ||
load, | ||
cache: createPluginCache(/* @__PURE__ */ Object.create(null)) | ||
}; | ||
return { | ||
name, | ||
options, | ||
api, | ||
resolvePath(fileName) { | ||
const root = pathParser2.resolve(this.config.root, this.config.output.path); | ||
return pathParser2.resolve(root, fileName); | ||
}, | ||
resolveName(name2) { | ||
return name2; | ||
} | ||
}; | ||
}); | ||
// src/managers/pluginManager/ParallelPluginError.ts | ||
var ParallelPluginError = class extends Error { | ||
errors; | ||
pluginManager; | ||
constructor(message, options) { | ||
super(message, { cause: options.cause }); | ||
this.errors = options.errors; | ||
this.pluginManager = options.pluginManager; | ||
} | ||
}; | ||
// src/managers/pluginManager/PluginError.ts | ||
var PluginError = class extends Error { | ||
pluginManager; | ||
constructor(message, options) { | ||
super(message, { cause: options.cause }); | ||
this.pluginManager = options.pluginManager; | ||
} | ||
}; | ||
// src/managers/pluginManager/PluginManager.ts | ||
@@ -938,129 +1065,2 @@ var hookNames = { | ||
} | ||
function writeIndexes(root, options) { | ||
const tree = TreeNode.build(root, { extensions: /\.ts/, ...options }); | ||
if (!tree) { | ||
return void 0; | ||
} | ||
const fileReducer = (files2, item) => { | ||
if (!item.children) { | ||
return []; | ||
} | ||
if (item.children?.length > 1) { | ||
const path = pathParser2.resolve(item.data.path, "index.ts"); | ||
const exports = item.children.map((file) => { | ||
if (!file) { | ||
return void 0; | ||
} | ||
const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`; | ||
if (importPath.includes("index") && path.includes("index")) { | ||
return void 0; | ||
} | ||
return { path: importPath }; | ||
}).filter(Boolean); | ||
files2.push({ | ||
path, | ||
fileName: "index.ts", | ||
source: "", | ||
exports | ||
}); | ||
} else { | ||
item.children?.forEach((child) => { | ||
const path = pathParser2.resolve(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: "", | ||
exports: [{ path: importPath }] | ||
}); | ||
}); | ||
} | ||
item.children.forEach((childItem) => { | ||
fileReducer(files2, childItem); | ||
}); | ||
return files2; | ||
}; | ||
const files = fileReducer([], tree); | ||
return files; | ||
} | ||
function combineFiles(files) { | ||
return files.filter(Boolean).reduce((acc, curr) => { | ||
if (!curr) { | ||
return acc; | ||
} | ||
const prevIndex = acc.findIndex((item) => item.path === curr.path); | ||
if (prevIndex !== -1) { | ||
const prev = acc[prevIndex]; | ||
acc[prevIndex] = { | ||
...curr, | ||
source: `${prev.source} | ||
${curr.source}`, | ||
imports: [...prev.imports || [], ...curr.imports || []], | ||
exports: [...prev.exports || [], ...curr.exports || []] | ||
}; | ||
} else { | ||
acc.push(curr); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
function getFileSource(file) { | ||
let { source } = file; | ||
if (!file.fileName.endsWith(".ts")) { | ||
return file.source; | ||
} | ||
const imports = []; | ||
const exports = []; | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
imports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name | ||
}); | ||
} | ||
if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) { | ||
imports.push(curr); | ||
} | ||
if (exists && Array.isArray(exists.name)) { | ||
if (Array.isArray(curr.name)) { | ||
exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])]; | ||
} | ||
} | ||
}); | ||
file.exports?.forEach((curr) => { | ||
const exists = exports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
exports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name | ||
}); | ||
} | ||
if (exists && !Array.isArray(exists.name) && exists.name !== curr.name && exists.asAlias === curr.asAlias) { | ||
exports.push(curr); | ||
} | ||
if (exists && Array.isArray(exists.name)) { | ||
if (Array.isArray(curr.name)) { | ||
exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])]; | ||
} | ||
} | ||
}); | ||
const importNodes = imports.reduce((prev, curr) => { | ||
return [...prev, createImportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly })]; | ||
}, []); | ||
const importSource = print(importNodes); | ||
const exportNodes = exports.reduce((prev, curr) => { | ||
return [...prev, createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })]; | ||
}, []); | ||
const exportSource = print(exportNodes); | ||
if (importSource) { | ||
source = `${importSource} | ||
${source}`; | ||
} | ||
if (exportSource) { | ||
source = `${exportSource} | ||
${source}`; | ||
} | ||
return source; | ||
} | ||
@@ -1067,0 +1067,0 @@ // src/build.ts |
{ | ||
"name": "@kubb/core", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"description": "Generator core", | ||
@@ -46,8 +46,11 @@ "repository": { | ||
"rimraf": "^5.0.1", | ||
"@kubb/ts-codegen": "1.1.6" | ||
"@kubb/ts-codegen": "1.1.7" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.42.0", | ||
"tsup": "^6.7.0", | ||
"ora": "^6.3.1", | ||
"typescript": "^5.1.3" | ||
"typescript": "^5.1.3", | ||
"@kubb/tsup-config": "0.1.0", | ||
"@kubb/eslint-config": "0.1.0" | ||
}, | ||
@@ -66,4 +69,4 @@ "publishConfig": { | ||
"release": "pnpm publish --no-git-check", | ||
"pre-commit": "echo 'pre-commit not configured'", | ||
"pre-push": "pnpm typecheck", | ||
"lint": "eslint \"**/*.{ts,tsx}\"", | ||
"lint-fix": "eslint \"**/*.{ts,tsx}\" --quiet --fix", | ||
"test": "vitest --passWithNoTests", | ||
@@ -70,0 +73,0 @@ "upgrade": "pnpm update", |
/* eslint-disable no-async-promise-executor */ | ||
import { getFileSource } from './managers/fileManager/index.ts' | ||
import { PluginManager } from './managers/pluginManager/index.ts' | ||
import { clean, isURL, read } from './utils/index.ts' | ||
import { getFileSource } from './managers/fileManager/index.ts' | ||
import { isPromise } from './utils/isPromise.ts' | ||
import type { Ora } from 'ora' | ||
import type { File } from './managers/fileManager/index.ts' | ||
import type { OnExecute } from './managers/pluginManager/index.ts' | ||
import type { File } from './managers/fileManager/index.ts' | ||
import type { BuildOutput, KubbPlugin, LogLevel, PluginContext, TransformResult } from './types.ts' | ||
import type { QueueTask } from './utils/index.ts' | ||
import type { PluginContext, TransformResult, LogLevel, KubbPlugin, BuildOutput } from './types.ts' | ||
import type { Ora } from 'ora' | ||
@@ -13,0 +13,0 @@ export type Logger = { |
@@ -1,2 +0,2 @@ | ||
import type { MaybePromise, KubbUserConfig, CLIOptions } from './types.ts' | ||
import type { CLIOptions, KubbUserConfig, MaybePromise } from './types.ts' | ||
@@ -3,0 +3,0 @@ /** |
import { Generator } from './Generator.ts' | ||
/** | ||
@@ -3,0 +4,0 @@ * Abstract class that contains the building blocks for plugins to create their own SchemaGenerator |
import crypto from 'node:crypto' | ||
import { write, read } from '../../utils/index.ts' | ||
import { read, write } from '../../utils/index.ts' | ||
import type { QueueTask, Queue } from '../../utils/index.ts' | ||
import type { CacheStore, UUID, Status, File } from './types.ts' | ||
import type { Queue, QueueTask } from '../../utils/index.ts' | ||
import type { CacheStore, File, Status, UUID } from './types.ts' | ||
@@ -8,0 +8,0 @@ export class FileManager { |
import pathParser from 'node:path' | ||
import { createImportDeclaration, createExportDeclaration, print } from '@kubb/ts-codegen' | ||
import { createExportDeclaration, createImportDeclaration, print } from '@kubb/ts-codegen' | ||
import { TreeNode } from '../../utils/index.ts' | ||
import type ts from 'typescript' | ||
import type { Path } from '../../types.ts' | ||
import type { PathMode, TreeNodeOptions } from '../../utils/index.ts' | ||
import type { Path } from '../../types.ts' | ||
import type ts from 'typescript' | ||
import type { File } from './types.ts' | ||
@@ -11,0 +11,0 @@ |
@@ -5,14 +5,13 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
import { PluginError } from './PluginError.ts' | ||
import { ParallelPluginError } from './ParallelPluginError.ts' | ||
import { definePlugin } from '../../plugin.ts' | ||
import { isPromise } from '../../utils/isPromise.ts' | ||
import { Queue } from '../../utils/Queue.ts' | ||
import { FileManager } from '../fileManager/FileManager.ts' | ||
import { Queue } from '../../utils/Queue.ts' | ||
import { isPromise } from '../../utils/isPromise.ts' | ||
import { ParallelPluginError } from './ParallelPluginError.ts' | ||
import { PluginError } from './PluginError.ts' | ||
import type { CorePluginOptions } from '../../plugin.ts' | ||
import type { KubbConfig, KubbPlugin, MaybePromise, PluginLifecycle, PluginLifecycleHooks, ResolveNameParams, ResolvePathParams } from '../../types.ts' | ||
import type { QueueTask } from '../../utils/Queue.ts' | ||
import type { Argument0, Strategy, Executer, OnExecute, ParseResult, SafeParseResult } from './types.ts' | ||
import type { KubbConfig, KubbPlugin, PluginLifecycleHooks, PluginLifecycle, MaybePromise, ResolvePathParams, ResolveNameParams } from '../../types.ts' | ||
import type { CorePluginOptions } from '../../plugin.ts' | ||
import type { Argument0, Executer, OnExecute, ParseResult, SafeParseResult, Strategy } from './types.ts' | ||
@@ -222,3 +221,3 @@ // inspired by: https://github.com/rollup/rollup/blob/master/src/utils/PluginDriver.ts# | ||
} | ||
return parseResult as SafeParseResult<H> | ||
return parseResult | ||
} | ||
@@ -225,0 +224,0 @@ |
@@ -0,3 +1,4 @@ | ||
import type { KubbPlugin, PluginLifecycle, PluginLifecycleHooks } from '../../types.ts' | ||
import type { PluginManager } from './PluginManager.ts' | ||
import type { KubbPlugin, PluginLifecycle, PluginLifecycleHooks } from '../../types.ts' | ||
/** | ||
@@ -4,0 +5,0 @@ * Get the type of the first argument in a function. |
@@ -5,5 +5,5 @@ import pathParser from 'node:path' | ||
import type { FileManager } from './managers/fileManager/FileManager.ts' | ||
import type { Executer } from './managers/index.ts' | ||
import type { FileManager } from './managers/fileManager/FileManager.ts' | ||
import type { PluginContext, KubbPlugin, PluginFactoryOptions, PluginLifecycleHooks } from './types.ts' | ||
import type { KubbPlugin, PluginContext, PluginFactoryOptions, PluginLifecycleHooks } from './types.ts' | ||
@@ -10,0 +10,0 @@ type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = ( |
/* eslint-disable @typescript-eslint/no-empty-interface */ | ||
import type { File, FileManager } from './managers/fileManager/index.ts' | ||
import type { PluginManager, SafeParseResult } from './managers/index.ts' | ||
import type { FileManager, File } from './managers/fileManager/index.ts' | ||
import type { Cache } from './utils/cache.ts' | ||
@@ -5,0 +5,0 @@ |
@@ -12,3 +12,3 @@ // eslint-disable-next-line @typescript-eslint/ban-types, no-undef | ||
} | ||
Error.captureStackTrace(dummyObject, belowFn || getStackTrace) | ||
Error.captureStackTrace(dummyObject as object, belowFn || getStackTrace) | ||
@@ -15,0 +15,0 @@ const v8StackTrace = dummyObject.stack |
@@ -0,3 +1,3 @@ | ||
import { promises as fs } from 'node:fs' | ||
import pathParser from 'node:path' | ||
import { promises as fs } from 'node:fs' | ||
@@ -4,0 +4,0 @@ function slash(path: string) { |
Sorry, the diff of this file is not supported yet
137445
6
+ Added@kubb/ts-codegen@1.1.7(transitive)
- Removed@kubb/ts-codegen@1.1.6(transitive)
Updated@kubb/ts-codegen@1.1.7