@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.
@@ -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: T) {
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: unknown = null; i < length; i++) {
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
- const filteredTree = dirTree(path, { extensions: options?.extensions, exclude: options.exclude })
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
- if (!filteredTree) {
92
- return null
93
- }
99
+ if (!filteredTree) {
100
+ return null
101
+ }
94
102
 
95
- const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type })
103
+ const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type || getPathMode(filteredTree.path) })
96
104
 
97
- const recurse = (node: typeof treeNode, item: DirectoryTree) => {
98
- const subNode = node.addChild({ name: item.name, path: item.path, type: item.type })
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
- if (item.children?.length) {
101
- item.children?.forEach((child) => {
102
- recurse(subNode, child)
103
- })
108
+ if (item.children?.length) {
109
+ item.children?.forEach((child) => {
110
+ recurse(subNode, child)
111
+ })
112
+ }
104
113
  }
105
- }
106
114
 
107
- filteredTree.children?.forEach((child) => recurse(treeNode, child))
115
+ filteredTree.children?.forEach((child) => recurse(treeNode, child))
108
116
 
109
- return treeNode as TreeNode<T>
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
  }
@@ -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) return null
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) return false
23
+ if (!item) {
24
+ return false
25
+ }
25
26
  item[0] = 0
26
27
  return true
27
28
  },
@@ -1,4 +1,5 @@
1
- // eslint-disable-next-line @typescript-eslint/ban-types, no-undef
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
  }
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-param-reassign */
2
1
  export function getUniqueName(originalName: string, data: Record<string, number>) {
3
2
  let used = data[originalName] || 0
4
3
  if (used) {
@@ -15,3 +15,4 @@ export * from './clean.ts'
15
15
  export * from './TreeNode.ts'
16
16
  export * from './transformReservedWord.ts'
17
17
  export * from './getStackTrace.ts'
18
+ export * from './uniqueId.ts'
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 isExtendedLengthPath = /^\\\\\?\\/.test(path)
4
+ function slash(path: string, platform: 'windows' | 'mac' | 'linux' = 'linux') {
5
+ const isWindowsPath = /^\\\\\?\\/.test(path)
6
6
 
7
- if (isExtendedLengthPath) {
8
- return path
7
+ if (['linux', 'mac'].includes(platform) && !isWindowsPath) {
8
+ // linux and mac
9
+ return path.replaceAll(/\\/g, '/').replace('../', '').trimEnd()
9
10
  }
10
11
 
11
- return path.replace(/\\/g, '/')
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).replace('../', '').trimEnd()
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
- try {
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
  }
@@ -0,0 +1,5 @@
1
+ export const uniqueId = (
2
+ (counter) =>
3
+ (str = '') =>
4
+ `${str}${++counter}`
5
+ )(0)
@@ -1,5 +1,4 @@
1
- /* eslint-disable consistent-return */
2
- import { promises as fs } from 'node:fs'
1
+ import fs from 'fs-extra'
3
2
  import pathParser from 'node:path'
4
3
 
5
4
  async function safeWriteFileToPath(path: string, data: string) {