@kubb/core
Advanced tools
Comparing version
@@ -204,3 +204,3 @@ import { DirectoryTreeOptions } from 'directory-tree'; | ||
name: string; | ||
}>(a: T, b: T): 1 | -1 | 0; | ||
}>(a: T, b: T): 0 | 1 | -1; | ||
@@ -262,7 +262,5 @@ declare function createJSDocBlockText({ comments }: { | ||
private getCacheByPath; | ||
getSource(file: File): string; | ||
get files(): File[]; | ||
add(file: File): Promise<File>; | ||
addOrAppend(file: File): Promise<File>; | ||
combine(files: Array<File | null>): File[]; | ||
setStatus(id: UUID, status: Status): void; | ||
@@ -289,2 +287,5 @@ get(id: UUID): File | undefined; | ||
declare function combineFiles(files: Array<File | null>): File[]; | ||
declare function getFileSource(file: File): string; | ||
type BuildOutput = { | ||
@@ -400,2 +401,2 @@ files: FileManager['files']; | ||
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, TreeNodeOptions, 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, TreeNodeOptions, UUID, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getFileSource, getPathMode, getRelativePath, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, timeout, validatePlugins, write }; |
@@ -5,5 +5,5 @@ import { createRequire } from 'module'; | ||
import rimraf from 'rimraf'; | ||
import uniq from 'lodash.uniq'; | ||
import { v4 } from 'uuid'; | ||
import dirTree from 'directory-tree'; | ||
import uniq from 'lodash.uniq'; | ||
@@ -265,38 +265,2 @@ createRequire(import.meta.url); | ||
} | ||
getSource(file) { | ||
if (!file.fileName.endsWith(".ts")) { | ||
return file.source; | ||
} | ||
const imports = []; | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
imports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? uniq(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 = uniq([...exists.name, ...curr.name]); | ||
} | ||
} | ||
}); | ||
const importSource = imports.reduce((prev, curr) => { | ||
if (Array.isArray(curr.name)) { | ||
return `${prev} | ||
import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`; | ||
} | ||
return `${prev} | ||
import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`; | ||
}, ""); | ||
if (importSource) { | ||
return `${importSource} | ||
${file.source}`; | ||
} | ||
return file.source; | ||
} | ||
get files() { | ||
@@ -332,22 +296,2 @@ const files = []; | ||
} | ||
combine(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 || []] | ||
}; | ||
} else { | ||
acc.push(curr); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
setStatus(id, status) { | ||
@@ -458,2 +402,58 @@ const cacheItem = this.getCache(id); | ||
}; | ||
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 || []] | ||
}; | ||
} else { | ||
acc.push(curr); | ||
} | ||
return acc; | ||
}, []); | ||
} | ||
function getFileSource(file) { | ||
if (!file.fileName.endsWith(".ts")) { | ||
return file.source; | ||
} | ||
const imports = []; | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
imports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? uniq(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 = uniq([...exists.name, ...curr.name]); | ||
} | ||
} | ||
}); | ||
const importSource = imports.reduce((prev, curr) => { | ||
if (Array.isArray(curr.name)) { | ||
return `${prev} | ||
import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`; | ||
} | ||
return `${prev} | ||
import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`; | ||
}, ""); | ||
if (importSource) { | ||
return `${importSource} | ||
${file.source}`; | ||
} | ||
return file.source; | ||
} | ||
@@ -670,3 +670,3 @@ // src/managers/pluginManager/PluginManager.ts | ||
const { path } = file; | ||
let code = fileManager.getSource(file); | ||
let code = getFileSource(file); | ||
const loadedResult = await pluginManager.hookFirst("load", [path]); | ||
@@ -689,3 +689,3 @@ if (loadedResult) { | ||
setTimeout(() => { | ||
done({ files: fileManager.files.map((file) => ({ ...file, source: fileManager.getSource(file) })) }); | ||
done({ files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) }); | ||
}, 500); | ||
@@ -735,4 +735,4 @@ pluginManager.fileManager.add({ | ||
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 }; | ||
export { FileManager, Generator, PluginManager, Queue, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getFileSource, 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-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "Generator core", | ||
@@ -54,3 +54,3 @@ "repository": { | ||
"tsup": "^6.7.0", | ||
"typescript": "^5.0.2" | ||
"typescript": "^5.0.3" | ||
}, | ||
@@ -57,0 +57,0 @@ "publishConfig": { |
@@ -9,3 +9,3 @@ /* eslint-disable no-async-promise-executor */ | ||
import type { QueueTask } from './utils' | ||
import type { FileManager, File } from './managers/fileManager' | ||
import { FileManager, File, getFileSource } from './managers/fileManager' | ||
import type { PluginContext, TransformResult, LogLevel, KubbPlugin } from './types' | ||
@@ -59,3 +59,3 @@ | ||
let code = fileManager.getSource(file) | ||
let code = getFileSource(file) | ||
@@ -85,3 +85,3 @@ const loadedResult = await pluginManager.hookFirst('load', [path]) | ||
setTimeout(() => { | ||
done({ files: fileManager.files.map((file) => ({ ...file, source: fileManager.getSource(file) })) }) | ||
done({ files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) }) | ||
}, 500) | ||
@@ -88,0 +88,0 @@ |
@@ -1,2 +0,1 @@ | ||
import uniq from 'lodash.uniq' | ||
import { v4 as uuidv4 } from 'uuid' | ||
@@ -38,44 +37,2 @@ | ||
public getSource(file: File) { | ||
// TODO make generic check | ||
if (!file.fileName.endsWith('.ts')) { | ||
return file.source | ||
} | ||
const imports: File['imports'] = [] | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path) | ||
if (!exists) { | ||
imports.push({ | ||
...curr, | ||
name: Array.isArray(curr.name) ? uniq(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 = uniq([...exists.name, ...curr.name]) | ||
} | ||
} | ||
}) | ||
const importSource = imports.reduce((prev, curr) => { | ||
if (Array.isArray(curr.name)) { | ||
return `${prev}\nimport ${curr.isTypeOnly ? 'type ' : ''}{ ${curr.name.join(', ')} } from "${curr.path}";` | ||
} | ||
return `${prev}\nimport ${curr.isTypeOnly ? 'type ' : ''}${curr.name} from "${curr.path}";` | ||
}, '') | ||
if (importSource) { | ||
return `${importSource}\n${file.source}` | ||
} | ||
return file.source | ||
} | ||
get files() { | ||
@@ -118,24 +75,2 @@ const files: File[] = [] | ||
combine(files: Array<File | null>) { | ||
return files.filter(Boolean).reduce((acc, curr: File | null) => { | ||
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}\n${curr.source}`, | ||
imports: [...(prev.imports || []), ...(curr.imports || [])], | ||
} | ||
} else { | ||
acc.push(curr) | ||
} | ||
return acc | ||
}, [] as File[]) | ||
} | ||
setStatus(id: UUID, status: Status) { | ||
@@ -142,0 +77,0 @@ const cacheItem = this.getCache(id) |
export * from './FileManager' | ||
export * from './types' | ||
export * from './TreeNode' | ||
export * from './utils' |
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
39
2.63%2977
0.13%190156
-0.04%