@kubb/core
Advanced tools
Comparing version 1.1.7 to 1.1.8
@@ -16,3 +16,3 @@ import { Ora } from 'ora'; | ||
declare function getRelativePath(rootDir?: string | null, filePath?: string | null): string; | ||
declare function getRelativePath(rootDir?: string | null, filePath?: string | null, platform?: 'windows' | 'mac' | 'linux'): string; | ||
type PathMode = 'file' | 'directory'; | ||
@@ -71,5 +71,5 @@ declare function getPathMode(path: string | undefined | null): PathMode; | ||
addChild(data: T): TreeNode<T>; | ||
find(data: T): {} | null; | ||
leaves(): TreeNode<T>[]; | ||
root(): TreeNode<T>; | ||
find(data?: T): TreeNode<T> | null; | ||
get leaves(): TreeNode<T>[]; | ||
get root(): TreeNode<T>; | ||
forEach(callback: (treeNode: TreeNode<T>) => void): this; | ||
@@ -83,2 +83,4 @@ static build<T = unknown>(path: string, options?: TreeNodeOptions): TreeNode<T> | null; | ||
declare const uniqueId: (str?: string) => string; | ||
type Import = { | ||
@@ -145,3 +147,3 @@ name: string | string[]; | ||
declare function writeIndexes(root: string, options: TreeNodeOptions): File[] | undefined; | ||
declare function writeIndexes(root: string, options?: TreeNodeOptions): File[] | null; | ||
declare function combineFiles(files: Array<File | null>): File[]; | ||
@@ -560,2 +562,2 @@ declare function getFileSource(file: File): string; | ||
export { Argument0, BuildOutput, CLIOptions, Cache, CacheStore, CorePluginOptions, Executer, File, FileManager, FileName, Generator, KubbConfig, KubbJSONPlugin, KubbObjectPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, Logger, MaybePromise, OnExecute, OptionalPath, ParallelPluginError, ParseResult, Path, PathMode, PluginContext, PluginError, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, Queue, QueueTask, Register, ResolveNameParams, ResolvePathParams, SafeParseResult, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, TreeNodeOptions, UUID, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, validatePlugins, write, writeIndexes }; | ||
export { Argument0, BuildOutput, CLIOptions, Cache, CacheStore, CorePluginOptions, Executer, File, FileManager, FileName, Generator, KubbConfig, KubbJSONPlugin, KubbObjectPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, Logger, MaybePromise, OnExecute, OptionalPath, ParallelPluginError, ParseResult, Path, PathMode, PluginContext, PluginError, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, Queue, QueueTask, Register, ResolveNameParams, ResolvePathParams, SafeParseResult, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, TreeNodeOptions, UUID, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, build as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes }; |
@@ -59,10 +59,10 @@ import { createRequire } from 'module'; | ||
} | ||
function slash(path) { | ||
function slash(path, platform = "linux") { | ||
const isExtendedLengthPath = /^\\\\\?\\/.test(path); | ||
if (isExtendedLengthPath) { | ||
return path; | ||
if (isExtendedLengthPath || platform === "linux" || platform === "mac") { | ||
return path.replace("../", "").trimEnd(); | ||
} | ||
return path.replace(/\\/g, "/"); | ||
return path.replace(/\\/g, "/").replace("../", "").trimEnd(); | ||
} | ||
function getRelativePath(rootDir, filePath) { | ||
function getRelativePath(rootDir, filePath, platform = "linux") { | ||
if (!rootDir || !filePath) { | ||
@@ -72,3 +72,3 @@ throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir} ${filePath}`); | ||
const relativePath = pathParser2.relative(rootDir, filePath); | ||
const path = slash(relativePath).replace("../", "").trimEnd(); | ||
const path = slash(relativePath, platform); | ||
if (path.startsWith("../")) { | ||
@@ -86,8 +86,3 @@ return path.replace(pathParser2.basename(path), pathParser2.basename(path, pathParser2.extname(filePath))); | ||
async function read(path) { | ||
try { | ||
return promises.readFile(path, { encoding: "utf8" }); | ||
} catch (err) { | ||
console.error(err); | ||
throw err; | ||
} | ||
return promises.readFile(path, { encoding: "utf8" }); | ||
} | ||
@@ -229,6 +224,9 @@ | ||
find(data) { | ||
if (!data) { | ||
return null; | ||
} | ||
if (data === this.data) { | ||
return this; | ||
} | ||
if (this.children) { | ||
if (this.children?.length) { | ||
for (let i = 0, { length } = this.children, target = null; i < length; i++) { | ||
@@ -243,3 +241,3 @@ target = this.children[i].find(data); | ||
} | ||
leaves() { | ||
get leaves() { | ||
if (!this.children || this.children.length === 0) { | ||
@@ -251,3 +249,3 @@ return [this]; | ||
for (let i = 0, { length } = this.children; i < length; i++) { | ||
leaves.push.apply(leaves, this.children[i].leaves()); | ||
leaves.push.apply(leaves, this.children[i].leaves); | ||
} | ||
@@ -257,7 +255,7 @@ } | ||
} | ||
root() { | ||
get root() { | ||
if (!this.parent) { | ||
return this; | ||
} | ||
return this.parent.root(); | ||
return this.parent.root; | ||
} | ||
@@ -277,17 +275,22 @@ forEach(callback) { | ||
static build(path, options = {}) { | ||
const filteredTree = dirTree(path, { extensions: options?.extensions, exclude: options.exclude }); | ||
if (!filteredTree) { | ||
return null; | ||
try { | ||
const exclude = Array.isArray(options.exclude) ? options.exclude : [options.exclude].filter(Boolean); | ||
const filteredTree = dirTree(path, { extensions: options.extensions, exclude: [/node_modules/, ...exclude] }); | ||
if (!filteredTree) { | ||
return null; | ||
} | ||
const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type || getPathMode(filteredTree.path) }); | ||
const recurse = (node, item) => { | ||
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type || getPathMode(item.path) }); | ||
if (item.children?.length) { | ||
item.children?.forEach((child) => { | ||
recurse(subNode, child); | ||
}); | ||
} | ||
}; | ||
filteredTree.children?.forEach((child) => recurse(treeNode, child)); | ||
return treeNode; | ||
} catch (e) { | ||
throw new Error("Something went wrong with creating index files with the TreehNode class", { cause: e }); | ||
} | ||
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; | ||
} | ||
@@ -405,2 +408,5 @@ }; | ||
// src/utils/uniqueId.ts | ||
var uniqueId = ((counter) => (str = "") => `${str}${++counter}`)(0); | ||
// src/managers/fileManager/FileManager.ts | ||
@@ -498,14 +504,14 @@ var FileManager = class { | ||
}; | ||
function writeIndexes(root, options) { | ||
function writeIndexes(root, options = {}) { | ||
const tree = TreeNode.build(root, { extensions: /\.ts/, ...options }); | ||
if (!tree) { | ||
return void 0; | ||
return null; | ||
} | ||
const fileReducer = (files2, item) => { | ||
if (!item.children) { | ||
const fileReducer = (files2, currentTree) => { | ||
if (!currentTree.children) { | ||
return []; | ||
} | ||
if (item.children?.length > 1) { | ||
const path = pathParser2.resolve(item.data.path, "index.ts"); | ||
const exports = item.children.map((file) => { | ||
if (currentTree.children?.length > 1) { | ||
const path = pathParser2.resolve(currentTree.data.path, "index.ts"); | ||
const exports = currentTree.children.map((file) => { | ||
if (!file) { | ||
@@ -527,4 +533,4 @@ return void 0; | ||
} else { | ||
item.children?.forEach((child) => { | ||
const path = pathParser2.resolve(item.data.path, "index.ts"); | ||
currentTree.children?.forEach((child) => { | ||
const path = pathParser2.resolve(currentTree.data.path, "index.ts"); | ||
const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`; | ||
@@ -539,3 +545,3 @@ files2.push({ | ||
} | ||
item.children.forEach((childItem) => { | ||
currentTree.children.forEach((childItem) => { | ||
fileReducer(files2, childItem); | ||
@@ -550,5 +556,2 @@ }); | ||
return files.filter(Boolean).reduce((acc, curr) => { | ||
if (!curr) { | ||
return acc; | ||
} | ||
const prevIndex = acc.findIndex((item) => item.path === curr.path); | ||
@@ -578,4 +581,4 @@ if (prevIndex !== -1) { | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path); | ||
if (!exists) { | ||
const existingImport = imports.find((imp) => imp.path === curr.path); | ||
if (!existingImport) { | ||
imports.push({ | ||
@@ -586,8 +589,8 @@ ...curr, | ||
} | ||
if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) { | ||
if (existingImport && !Array.isArray(existingImport.name) && existingImport.name !== curr.name) { | ||
imports.push(curr); | ||
} | ||
if (exists && Array.isArray(exists.name)) { | ||
if (existingImport && Array.isArray(existingImport.name)) { | ||
if (Array.isArray(curr.name)) { | ||
exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])]; | ||
existingImport.name = [.../* @__PURE__ */ new Set([...existingImport.name, ...curr.name])]; | ||
} | ||
@@ -618,3 +621,3 @@ } | ||
const exportNodes = exports.reduce((prev, curr) => { | ||
return [...prev, createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })]; | ||
return [...prev, createExportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly, asAlias: curr.asAlias })]; | ||
}, []); | ||
@@ -1090,3 +1093,3 @@ const exportSource = print(exportNodes); | ||
} catch (e) { | ||
throw new Error("Cannot read file defined in `input.path` or set with --input in the CLI of your Kubb config", { cause: e }); | ||
throw new Error("Cannot read file/URL defined in `input.path` or set with --input in the CLI of your Kubb config", { cause: e }); | ||
} | ||
@@ -1174,2 +1177,2 @@ if (config.output.clean) { | ||
export { FileManager, Generator, ParallelPluginError, PluginError, PluginManager, Queue, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, validatePlugins, write, writeIndexes }; | ||
export { FileManager, Generator, ParallelPluginError, PluginError, PluginManager, Queue, SchemaGenerator, TreeNode, ValidationPluginError, build, clean, combineFiles, createJSDocBlockText, createPlugin, createPluginCache, src_default as default, defineConfig, getEncodedText, getFileSource, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, isPromise, isURL, name, nameSorter, objectToParameters, read, renderTemplate, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes }; |
{ | ||
"name": "@kubb/core", | ||
"version": "1.1.7", | ||
"version": "1.1.8", | ||
"description": "Generator core", | ||
"keywords": [ | ||
"typescript", | ||
"plugins", | ||
"kubb", | ||
"codegen" | ||
], | ||
"repository": { | ||
@@ -12,13 +18,4 @@ "type": "git", | ||
"author": "Stijn Van Hulle <stijn@stijnvanhulle.be", | ||
"keywords": [ | ||
"typescript", | ||
"plugins", | ||
"kubb", | ||
"codegen" | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
@@ -33,2 +30,5 @@ ".": { | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"typesVersions": { | ||
@@ -48,12 +48,18 @@ "*": {} | ||
"rimraf": "^5.0.1", | ||
"@kubb/ts-codegen": "1.1.7" | ||
"@kubb/ts-codegen": "1.1.8" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.42.0", | ||
"ora": "^6.3.1", | ||
"tsup": "^6.7.0", | ||
"ora": "^6.3.1", | ||
"typescript": "^5.1.3", | ||
"@kubb/eslint-config": "0.1.0", | ||
"@kubb/tsup-config": "0.1.0", | ||
"@kubb/eslint-config": "0.1.0" | ||
"@kubb/typescript-config": "0.1.0" | ||
}, | ||
"packageManager": "pnpm@8.3.0", | ||
"engines": { | ||
"node": ">=18", | ||
"pnpm": ">=8.3.0" | ||
}, | ||
"publishConfig": { | ||
@@ -63,16 +69,13 @@ "access": "public", | ||
}, | ||
"engines": { | ||
"node": ">=18", | ||
"pnpm": ">=8" | ||
}, | ||
"scripts": { | ||
"build": "tsup", | ||
"start": "tsup --watch", | ||
"release": "pnpm publish --no-git-check", | ||
"clean": "rimraf ./dist", | ||
"lint": "eslint \"**/*.{ts,tsx}\"", | ||
"lint-fix": "eslint \"**/*.{ts,tsx}\" --quiet --fix", | ||
"release": "pnpm publish --no-git-check", | ||
"start": "tsup --watch", | ||
"test": "vitest --passWithNoTests", | ||
"upgrade": "pnpm update", | ||
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false" | ||
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false", | ||
"upgrade": "pnpm update" | ||
} | ||
} |
@@ -8,36 +8,43 @@ <div align="center"> | ||
Core utils for other packages. | ||
</p> | ||
</p> | ||
<img src="https://raw.githubusercontent.com/kubb-project/kubb/main/assets/banner.png" alt="logo" height="auto" /> | ||
<!-- Badges --> | ||
<p> | ||
<a href="https://www.npmjs.com/package/@kubb/core"> | ||
<!-- Badges --> | ||
<p> | ||
<a href="https://www.npmjs.com/package/@kubb/core" target="_blank"> | ||
<img alt="npm version" src="https://img.shields.io/npm/v/@kubb/core?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/core"> | ||
<img alt="npm downloads" src="https://img.shields.io/bundlephobia/min/@kubb/core?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/core"> | ||
<a href="https://www.npmjs.com/package/@kubb/core" target="_blank"> | ||
<img alt="npm downloads" src="https://img.shields.io/npm/dm/@kubb/core?style=for-the-badge"/> | ||
</a> | ||
</p> | ||
</p> | ||
<p> | ||
<a href="https://www.npmjs.com/package/@kubb/core" target="_blank"> | ||
<img alt="Minified size" src="https://img.shields.io/bundlephobia/min/@kubb/core?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/core" target="_blank"> | ||
<img alt="Coverage" src="https://img.shields.io/codecov/c/github/kubb-project/kubb?style=for-the-badge"/> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@kubb/core" target="_blank"> | ||
<img alt="Build status" src="https://img.shields.io/github/actions/workflow/status/kubb-project/kubb/ci.yaml?style=for-the-badge"/> | ||
</a> | ||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
</p> | ||
<h4> | ||
<a href="https://codesandbox.io/s/github/kubb-project/kubb/tree/main/examples/simple">View Demo</a> | ||
<span> · </span> | ||
<a href="https://kubb.dev/" target="_blank">Documentation</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/">Report Bug</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/">Request Feature</a> | ||
<h4> | ||
<a href="https://codesandbox.io/s/github/kubb-project/kubb/tree/main/examples/simple" target="_blank">View Demo</a> | ||
<span> · </span> | ||
<a href="https://kubb.dev/" target="_blank">Documentation</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/" target="_blank">Report Bug</a> | ||
<span> · </span> | ||
<a href="https://github.com/kubb-project/kubb/issues/" target="_blank">Request Feature</a> | ||
</h4> | ||
</div> | ||
<br /> | ||
<!-- About the Project | ||
## :star2: About the Project | ||
<div align="center"> | ||
<img src="assets/screenshot.jpg" alt="screenshot" /> | ||
</div> | ||
--> |
@@ -39,3 +39,3 @@ /* eslint-disable no-async-promise-executor */ | ||
} catch (e: any) { | ||
throw new Error('Cannot read file defined in `input.path` or set with --input in the CLI of your Kubb config', { cause: e }) | ||
throw new Error('Cannot read file/URL defined in `input.path` or set with --input in the CLI of your Kubb config', { cause: e }) | ||
} | ||
@@ -42,0 +42,0 @@ |
@@ -12,17 +12,19 @@ import pathParser from 'node:path' | ||
export function writeIndexes(root: string, options: TreeNodeOptions) { | ||
const tree = TreeNode.build<{ type: PathMode; path: Path; name: string }>(root, { extensions: /\.ts/, ...options }) | ||
type TreeNodeData = { type: PathMode; path: Path; name: string } | ||
export function writeIndexes(root: string, options: TreeNodeOptions = {}): File[] | null { | ||
const tree = TreeNode.build<TreeNodeData>(root, { extensions: /\.ts/, ...options }) | ||
if (!tree) { | ||
return undefined | ||
return null | ||
} | ||
const fileReducer = (files: File[], item: typeof tree) => { | ||
if (!item.children) { | ||
const fileReducer = (files: File[], currentTree: typeof tree) => { | ||
if (!currentTree.children) { | ||
return [] | ||
} | ||
if (item.children?.length > 1) { | ||
const path = pathParser.resolve(item.data.path, 'index.ts') | ||
const exports = item.children | ||
if (currentTree.children?.length > 1) { | ||
const path = pathParser.resolve(currentTree.data.path, 'index.ts') | ||
const exports = currentTree.children | ||
.map((file) => { | ||
@@ -51,4 +53,4 @@ if (!file) { | ||
} else { | ||
item.children?.forEach((child) => { | ||
const path = pathParser.resolve(item.data.path, 'index.ts') | ||
currentTree.children?.forEach((child) => { | ||
const path = pathParser.resolve(currentTree.data.path, 'index.ts') | ||
const importPath = child.data.type === 'directory' ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, '')}` | ||
@@ -65,3 +67,3 @@ | ||
item.children.forEach((childItem) => { | ||
currentTree.children.forEach((childItem) => { | ||
fileReducer(files, childItem) | ||
@@ -78,7 +80,4 @@ }) | ||
export function combineFiles(files: Array<File | null>) { | ||
return files.filter(Boolean).reduce((acc, curr: File | null) => { | ||
if (!curr) { | ||
return acc | ||
} | ||
export function combineFiles(files: Array<File | null>): File[] { | ||
return (files.filter(Boolean) as File[]).reduce((acc, curr: File) => { | ||
const prevIndex = acc.findIndex((item) => item.path === curr.path) | ||
@@ -102,3 +101,3 @@ | ||
export function getFileSource(file: File) { | ||
export function getFileSource(file: File): string { | ||
let { source } = file | ||
@@ -114,4 +113,5 @@ | ||
file.imports?.forEach((curr) => { | ||
const exists = imports.find((imp) => imp.path === curr.path) | ||
if (!exists) { | ||
const existingImport = imports.find((imp) => imp.path === curr.path) | ||
if (!existingImport) { | ||
imports.push({ | ||
@@ -123,9 +123,9 @@ ...curr, | ||
if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) { | ||
if (existingImport && !Array.isArray(existingImport.name) && existingImport.name !== curr.name) { | ||
imports.push(curr) | ||
} | ||
if (exists && Array.isArray(exists.name)) { | ||
if (existingImport && Array.isArray(existingImport.name)) { | ||
if (Array.isArray(curr.name)) { | ||
exists.name = [...new Set([...exists.name, ...curr.name])] | ||
existingImport.name = [...new Set([...existingImport.name, ...curr.name])] | ||
} | ||
@@ -161,3 +161,3 @@ } | ||
const exportNodes = exports.reduce((prev, curr) => { | ||
return [...prev, createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })] | ||
return [...prev, createExportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly, asAlias: curr.asAlias })] | ||
}, [] as ts.ExportDeclaration[]) | ||
@@ -164,0 +164,0 @@ const exportSource = print(exportNodes) |
@@ -18,1 +18,2 @@ export * from './isPromise.ts' | ||
export * from './getStackTrace.ts' | ||
export * from './uniqueId.ts' |
import { promises as fs } from 'node:fs' | ||
import pathParser from 'node:path' | ||
function slash(path: string) { | ||
function slash(path: string, platform: 'windows' | 'mac' | 'linux' = 'linux') { | ||
const isExtendedLengthPath = /^\\\\\?\\/.test(path) | ||
if (isExtendedLengthPath) { | ||
return path | ||
if (isExtendedLengthPath || platform === 'linux' || platform === 'mac') { | ||
// linux and mac | ||
return path.replace('../', '').trimEnd() | ||
} | ||
return path.replace(/\\/g, '/') | ||
// windows | ||
return path.replace(/\\/g, '/').replace('../', '').trimEnd() | ||
} | ||
export function getRelativePath(rootDir?: string | null, filePath?: string | null) { | ||
export function getRelativePath(rootDir?: string | null, filePath?: string | null, platform: 'windows' | 'mac' | 'linux' = 'linux') { | ||
if (!rootDir || !filePath) { | ||
@@ -23,3 +25,3 @@ throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir} ${filePath}`) | ||
// However, web browsers use "/" no matter the platform | ||
const path = slash(relativePath).replace('../', '').trimEnd() | ||
const path = slash(relativePath, platform) | ||
@@ -43,8 +45,3 @@ if (path.startsWith('../')) { | ||
export async function read(path: string) { | ||
try { | ||
return fs.readFile(path, { encoding: 'utf8' }) | ||
} catch (err) { | ||
console.error(err) | ||
throw err | ||
} | ||
return fs.readFile(path, { encoding: 'utf8' }) | ||
} |
import dirTree from 'directory-tree' | ||
import { getPathMode } from '../utils/read.ts' | ||
import type { DirectoryTree, DirectoryTreeOptions } from 'directory-tree' | ||
@@ -29,3 +31,7 @@ | ||
find(data: T) { | ||
find(data?: T): TreeNode<T> | null { | ||
if (!data) { | ||
return null | ||
} | ||
if (data === this.data) { | ||
@@ -35,4 +41,4 @@ return this | ||
if (this.children) { | ||
for (let i = 0, { length } = this.children, target: unknown = null; i < length; i++) { | ||
if (this.children?.length) { | ||
for (let i = 0, { length } = this.children, target: TreeNode<T> | null = null; i < length; i++) { | ||
target = this.children[i].find(data) | ||
@@ -48,3 +54,3 @@ if (target) { | ||
leaves(): TreeNode<T>[] { | ||
get leaves(): TreeNode<T>[] { | ||
if (!this.children || this.children.length === 0) { | ||
@@ -60,3 +66,3 @@ // this is a leaf | ||
// eslint-disable-next-line prefer-spread | ||
leaves.push.apply(leaves, this.children[i].leaves()) | ||
leaves.push.apply(leaves, this.children[i].leaves) | ||
} | ||
@@ -67,7 +73,7 @@ } | ||
root(): TreeNode<T> { | ||
get root(): TreeNode<T> { | ||
if (!this.parent) { | ||
return this | ||
} | ||
return this.parent.root() | ||
return this.parent.root | ||
} | ||
@@ -94,24 +100,29 @@ | ||
public static build<T = unknown>(path: string, options: TreeNodeOptions = {}): TreeNode<T> | null { | ||
const filteredTree = dirTree(path, { extensions: options?.extensions, exclude: options.exclude }) | ||
try { | ||
const exclude = Array.isArray(options.exclude) ? options.exclude : ([options.exclude].filter(Boolean) as RegExp[]) | ||
const filteredTree = dirTree(path, { extensions: options.extensions, exclude: [/node_modules/, ...exclude] }) | ||
if (!filteredTree) { | ||
return null | ||
} | ||
if (!filteredTree) { | ||
return null | ||
} | ||
const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type }) | ||
const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type || getPathMode(filteredTree.path) }) | ||
const recurse = (node: typeof treeNode, item: DirectoryTree) => { | ||
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type }) | ||
const recurse = (node: typeof treeNode, item: DirectoryTree) => { | ||
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type || getPathMode(item.path) }) | ||
if (item.children?.length) { | ||
item.children?.forEach((child) => { | ||
recurse(subNode, child) | ||
}) | ||
if (item.children?.length) { | ||
item.children?.forEach((child) => { | ||
recurse(subNode, child) | ||
}) | ||
} | ||
} | ||
} | ||
filteredTree.children?.forEach((child) => recurse(treeNode, child)) | ||
filteredTree.children?.forEach((child) => recurse(treeNode, child)) | ||
return treeNode as TreeNode<T> | ||
return treeNode as TreeNode<T> | ||
} catch (e) { | ||
throw new Error('Something went wrong with creating index files with the TreehNode class', { cause: e }) | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
140667
45
4548
50
7
+ Added@kubb/ts-codegen@1.1.8(transitive)
- Removed@kubb/ts-codegen@1.1.7(transitive)
Updated@kubb/ts-codegen@1.1.8