@kubb/core 1.1.7 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -27
- package/dist/index.cjs +79 -68
- package/dist/index.d.ts +8 -6
- package/dist/index.js +77 -67
- package/package.json +29 -23
- package/src/build.ts +2 -2
- package/src/index.ts +0 -1
- package/src/managers/fileManager/utils.ts +23 -23
- package/src/managers/pluginManager/PluginManager.ts +10 -9
- package/src/plugin.ts +6 -2
- package/src/utils/Queue.ts +4 -4
- package/src/utils/TreeNode.ts +32 -21
- package/src/utils/cache.ts +6 -5
- package/src/utils/getStackTrace.ts +3 -2
- package/src/utils/getUniqueName.ts +0 -1
- package/src/utils/index.ts +1 -0
- package/src/utils/read.ts +12 -15
- package/src/utils/uniqueId.ts +5 -0
- package/src/utils/write.ts +1 -2
package/src/utils/TreeNode.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import dirTree from 'directory-tree'
|
|
2
2
|
|
|
3
|
+
import { getPathMode } from '../utils/read.ts'
|
|
4
|
+
|
|
3
5
|
import type { DirectoryTree, DirectoryTreeOptions } from 'directory-tree'
|
|
4
6
|
|
|
5
7
|
export type TreeNodeOptions = DirectoryTreeOptions
|
|
@@ -26,13 +28,17 @@ export class TreeNode<T = unknown> {
|
|
|
26
28
|
return child
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
find(data
|
|
31
|
+
find(data?: T): TreeNode<T> | null {
|
|
32
|
+
if (!data) {
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
|
|
30
36
|
if (data === this.data) {
|
|
31
37
|
return this
|
|
32
38
|
}
|
|
33
39
|
|
|
34
|
-
if (this.children) {
|
|
35
|
-
for (let i = 0, { length } = this.children, target:
|
|
40
|
+
if (this.children?.length) {
|
|
41
|
+
for (let i = 0, { length } = this.children, target: TreeNode<T> | null = null; i < length; i++) {
|
|
36
42
|
target = this.children[i].find(data)
|
|
37
43
|
if (target) {
|
|
38
44
|
return target
|
|
@@ -43,7 +49,7 @@ export class TreeNode<T = unknown> {
|
|
|
43
49
|
return null
|
|
44
50
|
}
|
|
45
51
|
|
|
46
|
-
leaves(): TreeNode<T>[] {
|
|
52
|
+
get leaves(): TreeNode<T>[] {
|
|
47
53
|
if (!this.children || this.children.length === 0) {
|
|
48
54
|
// this is a leaf
|
|
49
55
|
return [this]
|
|
@@ -54,17 +60,17 @@ export class TreeNode<T = unknown> {
|
|
|
54
60
|
if (this.children) {
|
|
55
61
|
for (let i = 0, { length } = this.children; i < length; i++) {
|
|
56
62
|
// eslint-disable-next-line prefer-spread
|
|
57
|
-
leaves.push.apply(leaves, this.children[i].leaves
|
|
63
|
+
leaves.push.apply(leaves, this.children[i].leaves)
|
|
58
64
|
}
|
|
59
65
|
}
|
|
60
66
|
return leaves
|
|
61
67
|
}
|
|
62
68
|
|
|
63
|
-
root(): TreeNode<T> {
|
|
69
|
+
get root(): TreeNode<T> {
|
|
64
70
|
if (!this.parent) {
|
|
65
71
|
return this
|
|
66
72
|
}
|
|
67
|
-
return this.parent.root
|
|
73
|
+
return this.parent.root
|
|
68
74
|
}
|
|
69
75
|
|
|
70
76
|
forEach(callback: (treeNode: TreeNode<T>) => void): this {
|
|
@@ -86,26 +92,31 @@ export class TreeNode<T = unknown> {
|
|
|
86
92
|
}
|
|
87
93
|
|
|
88
94
|
public static build<T = unknown>(path: string, options: TreeNodeOptions = {}): TreeNode<T> | null {
|
|
89
|
-
|
|
95
|
+
try {
|
|
96
|
+
const exclude = Array.isArray(options.exclude) ? options.exclude : ([options.exclude].filter(Boolean) as RegExp[])
|
|
97
|
+
const filteredTree = dirTree(path, { extensions: options.extensions, exclude: [/node_modules/, ...exclude] })
|
|
90
98
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
99
|
+
if (!filteredTree) {
|
|
100
|
+
return null
|
|
101
|
+
}
|
|
94
102
|
|
|
95
|
-
|
|
103
|
+
const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type || getPathMode(filteredTree.path) })
|
|
96
104
|
|
|
97
|
-
|
|
98
|
-
|
|
105
|
+
const recurse = (node: typeof treeNode, item: DirectoryTree) => {
|
|
106
|
+
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type || getPathMode(item.path) })
|
|
99
107
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
108
|
+
if (item.children?.length) {
|
|
109
|
+
item.children?.forEach((child) => {
|
|
110
|
+
recurse(subNode, child)
|
|
111
|
+
})
|
|
112
|
+
}
|
|
104
113
|
}
|
|
105
|
-
}
|
|
106
114
|
|
|
107
|
-
|
|
115
|
+
filteredTree.children?.forEach((child) => recurse(treeNode, child))
|
|
108
116
|
|
|
109
|
-
|
|
117
|
+
return treeNode as TreeNode<T>
|
|
118
|
+
} catch (e) {
|
|
119
|
+
throw new Error('Something went wrong with creating index files with the TreehNode class', { cause: e })
|
|
120
|
+
}
|
|
110
121
|
}
|
|
111
122
|
}
|
package/src/utils/cache.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/* eslint-disable no-param-reassign */
|
|
2
|
-
/* eslint-disable consistent-return */
|
|
3
|
-
|
|
4
1
|
export interface Cache<T extends object = object> {
|
|
5
2
|
delete(id: keyof T): boolean
|
|
6
3
|
get(id: keyof T): T[keyof T] | null
|
|
@@ -15,13 +12,17 @@ export function createPluginCache<T extends Record<string, [number, unknown]>>(c
|
|
|
15
12
|
},
|
|
16
13
|
get(id) {
|
|
17
14
|
const item = cache[id]
|
|
18
|
-
if (!item)
|
|
15
|
+
if (!item) {
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
19
18
|
item[0] = 0
|
|
20
19
|
return item[1] as T[keyof T]
|
|
21
20
|
},
|
|
22
21
|
has(id) {
|
|
23
22
|
const item = cache[id]
|
|
24
|
-
if (!item)
|
|
23
|
+
if (!item) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
25
26
|
item[0] = 0
|
|
26
27
|
return true
|
|
27
28
|
},
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
2
3
|
export function getStackTrace(belowFn?: Function): NodeJS.CallSite[] {
|
|
3
4
|
const oldLimit = Error.stackTraceLimit
|
|
4
5
|
Error.stackTraceLimit = Infinity
|
|
@@ -15,5 +16,5 @@ export function getStackTrace(belowFn?: Function): NodeJS.CallSite[] {
|
|
|
15
16
|
Error.prepareStackTrace = v8Handler
|
|
16
17
|
Error.stackTraceLimit = oldLimit
|
|
17
18
|
|
|
18
|
-
return v8StackTrace
|
|
19
|
+
return v8StackTrace as NodeJS.CallSite[]
|
|
19
20
|
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/read.ts
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
|
-
import { promises as fs } from 'node:fs'
|
|
2
1
|
import pathParser from 'node:path'
|
|
2
|
+
import fs from 'fs-extra'
|
|
3
3
|
|
|
4
|
-
function slash(path: string) {
|
|
5
|
-
const
|
|
4
|
+
function slash(path: string, platform: 'windows' | 'mac' | 'linux' = 'linux') {
|
|
5
|
+
const isWindowsPath = /^\\\\\?\\/.test(path)
|
|
6
6
|
|
|
7
|
-
if (
|
|
8
|
-
|
|
7
|
+
if (['linux', 'mac'].includes(platform) && !isWindowsPath) {
|
|
8
|
+
// linux and mac
|
|
9
|
+
return path.replaceAll(/\\/g, '/').replace('../', '').trimEnd()
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
// windows
|
|
13
|
+
return path.replaceAll(/\\/g, '/').replace('../', '').trimEnd()
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
export function getRelativePath(rootDir?: string | null, filePath?: string | null) {
|
|
16
|
+
export function getRelativePath(rootDir?: string | null, filePath?: string | null, platform: 'windows' | 'mac' | 'linux' = 'linux') {
|
|
15
17
|
if (!rootDir || !filePath) {
|
|
16
|
-
throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir} ${filePath}`)
|
|
18
|
+
throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
const relativePath = pathParser.relative(rootDir, filePath)
|
|
20
22
|
|
|
21
23
|
// On Windows, paths are separated with a "\"
|
|
22
24
|
// However, web browsers use "/" no matter the platform
|
|
23
|
-
const path = slash(relativePath
|
|
25
|
+
const path = slash(relativePath, platform)
|
|
24
26
|
|
|
25
27
|
if (path.startsWith('../')) {
|
|
26
28
|
return path.replace(pathParser.basename(path), pathParser.basename(path, pathParser.extname(filePath)))
|
|
@@ -39,10 +41,5 @@ export function getPathMode(path: string | undefined | null): PathMode {
|
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
export async function read(path: string) {
|
|
42
|
-
|
|
43
|
-
return fs.readFile(path, { encoding: 'utf8' })
|
|
44
|
-
} catch (err) {
|
|
45
|
-
console.error(err)
|
|
46
|
-
throw err
|
|
47
|
-
}
|
|
44
|
+
return fs.readFile(path, { encoding: 'utf8' })
|
|
48
45
|
}
|
package/src/utils/write.ts
CHANGED