@catbee/utils 0.0.6 → 0.0.8-next.0

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.
Files changed (249) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +77 -1107
  3. package/build/index.cjs +6888 -0
  4. package/build/index.d.ts +5242 -0
  5. package/build/index.mjs +6615 -0
  6. package/package.json +26 -95
  7. package/build/esm/config.d.ts +0 -130
  8. package/build/esm/config.js +0 -145
  9. package/build/esm/config.js.map +0 -1
  10. package/build/esm/index.d.ts +0 -27
  11. package/build/esm/index.js +0 -50
  12. package/build/esm/index.js.map +0 -1
  13. package/build/esm/servers/server.builder.d.ts +0 -625
  14. package/build/esm/servers/server.builder.js +0 -722
  15. package/build/esm/servers/server.builder.js.map +0 -1
  16. package/build/esm/servers/server.d.ts +0 -256
  17. package/build/esm/servers/server.js +0 -1211
  18. package/build/esm/servers/server.js.map +0 -1
  19. package/build/esm/types/api-response.d.ts +0 -124
  20. package/build/esm/types/api-response.js +0 -25
  21. package/build/esm/types/api-response.js.map +0 -1
  22. package/build/esm/types/index.d.ts +0 -125
  23. package/build/esm/types/index.js +0 -25
  24. package/build/esm/types/index.js.map +0 -1
  25. package/build/esm/types/server.d.ts +0 -285
  26. package/build/esm/types/server.js +0 -25
  27. package/build/esm/types/server.js.map +0 -1
  28. package/build/esm/utils/array.utils.d.ts +0 -168
  29. package/build/esm/utils/array.utils.js +0 -382
  30. package/build/esm/utils/array.utils.js.map +0 -1
  31. package/build/esm/utils/async.utils.d.ts +0 -174
  32. package/build/esm/utils/async.utils.js +0 -652
  33. package/build/esm/utils/async.utils.js.map +0 -1
  34. package/build/esm/utils/cache.utils.d.ts +0 -153
  35. package/build/esm/utils/cache.utils.js +0 -499
  36. package/build/esm/utils/cache.utils.js.map +0 -1
  37. package/build/esm/utils/context-store.utils.d.ts +0 -186
  38. package/build/esm/utils/context-store.utils.js +0 -304
  39. package/build/esm/utils/context-store.utils.js.map +0 -1
  40. package/build/esm/utils/crypto.utils.d.ts +0 -160
  41. package/build/esm/utils/crypto.utils.js +0 -375
  42. package/build/esm/utils/crypto.utils.js.map +0 -1
  43. package/build/esm/utils/decorators.utils.d.ts +0 -315
  44. package/build/esm/utils/decorators.utils.js +0 -666
  45. package/build/esm/utils/decorators.utils.js.map +0 -1
  46. package/build/esm/utils/dir.utils.d.ts +0 -196
  47. package/build/esm/utils/dir.utils.js +0 -998
  48. package/build/esm/utils/dir.utils.js.map +0 -1
  49. package/build/esm/utils/env.utils.d.ts +0 -377
  50. package/build/esm/utils/env.utils.js +0 -873
  51. package/build/esm/utils/env.utils.js.map +0 -1
  52. package/build/esm/utils/exception.utils.d.ts +0 -230
  53. package/build/esm/utils/exception.utils.js +0 -559
  54. package/build/esm/utils/exception.utils.js.map +0 -1
  55. package/build/esm/utils/fs.utils.d.ts +0 -164
  56. package/build/esm/utils/fs.utils.js +0 -580
  57. package/build/esm/utils/fs.utils.js.map +0 -1
  58. package/build/esm/utils/http-status-codes.d.ts +0 -266
  59. package/build/esm/utils/http-status-codes.js +0 -295
  60. package/build/esm/utils/http-status-codes.js.map +0 -1
  61. package/build/esm/utils/id.utils.d.ts +0 -36
  62. package/build/esm/utils/id.utils.js +0 -87
  63. package/build/esm/utils/id.utils.js.map +0 -1
  64. package/build/esm/utils/logger.utils.d.ts +0 -166
  65. package/build/esm/utils/logger.utils.js +0 -347
  66. package/build/esm/utils/logger.utils.js.map +0 -1
  67. package/build/esm/utils/middleware.utils.d.ts +0 -83
  68. package/build/esm/utils/middleware.utils.js +0 -205
  69. package/build/esm/utils/middleware.utils.js.map +0 -1
  70. package/build/esm/utils/obj.utils.d.ts +0 -124
  71. package/build/esm/utils/obj.utils.js +0 -519
  72. package/build/esm/utils/obj.utils.js.map +0 -1
  73. package/build/esm/utils/request.utils.d.ts +0 -77
  74. package/build/esm/utils/request.utils.js +0 -196
  75. package/build/esm/utils/request.utils.js.map +0 -1
  76. package/build/esm/utils/response.utils.d.ts +0 -163
  77. package/build/esm/utils/response.utils.js +0 -284
  78. package/build/esm/utils/response.utils.js.map +0 -1
  79. package/build/esm/utils/string.utils.d.ts +0 -93
  80. package/build/esm/utils/string.utils.js +0 -169
  81. package/build/esm/utils/string.utils.js.map +0 -1
  82. package/build/esm/utils/url.utils.d.ts +0 -141
  83. package/build/esm/utils/url.utils.js +0 -381
  84. package/build/esm/utils/url.utils.js.map +0 -1
  85. package/build/esm/utils/validate.utils.d.ts +0 -177
  86. package/build/esm/utils/validate.utils.js +0 -336
  87. package/build/esm/utils/validate.utils.js.map +0 -1
  88. package/build/esnext/config.d.ts +0 -130
  89. package/build/esnext/config.js +0 -145
  90. package/build/esnext/config.js.map +0 -1
  91. package/build/esnext/index.d.ts +0 -27
  92. package/build/esnext/index.js +0 -50
  93. package/build/esnext/index.js.map +0 -1
  94. package/build/esnext/servers/server.builder.d.ts +0 -625
  95. package/build/esnext/servers/server.builder.js +0 -677
  96. package/build/esnext/servers/server.builder.js.map +0 -1
  97. package/build/esnext/servers/server.d.ts +0 -256
  98. package/build/esnext/servers/server.js +0 -918
  99. package/build/esnext/servers/server.js.map +0 -1
  100. package/build/esnext/types/api-response.d.ts +0 -124
  101. package/build/esnext/types/api-response.js +0 -25
  102. package/build/esnext/types/api-response.js.map +0 -1
  103. package/build/esnext/types/index.d.ts +0 -125
  104. package/build/esnext/types/index.js +0 -25
  105. package/build/esnext/types/index.js.map +0 -1
  106. package/build/esnext/types/server.d.ts +0 -285
  107. package/build/esnext/types/server.js +0 -25
  108. package/build/esnext/types/server.js.map +0 -1
  109. package/build/esnext/utils/array.utils.d.ts +0 -168
  110. package/build/esnext/utils/array.utils.js +0 -345
  111. package/build/esnext/utils/array.utils.js.map +0 -1
  112. package/build/esnext/utils/async.utils.d.ts +0 -174
  113. package/build/esnext/utils/async.utils.js +0 -430
  114. package/build/esnext/utils/async.utils.js.map +0 -1
  115. package/build/esnext/utils/cache.utils.d.ts +0 -153
  116. package/build/esnext/utils/cache.utils.js +0 -293
  117. package/build/esnext/utils/cache.utils.js.map +0 -1
  118. package/build/esnext/utils/context-store.utils.d.ts +0 -186
  119. package/build/esnext/utils/context-store.utils.js +0 -286
  120. package/build/esnext/utils/context-store.utils.js.map +0 -1
  121. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  122. package/build/esnext/utils/crypto.utils.js +0 -276
  123. package/build/esnext/utils/crypto.utils.js.map +0 -1
  124. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  125. package/build/esnext/utils/decorators.utils.js +0 -504
  126. package/build/esnext/utils/decorators.utils.js.map +0 -1
  127. package/build/esnext/utils/dir.utils.d.ts +0 -196
  128. package/build/esnext/utils/dir.utils.js +0 -477
  129. package/build/esnext/utils/dir.utils.js.map +0 -1
  130. package/build/esnext/utils/env.utils.d.ts +0 -377
  131. package/build/esnext/utils/env.utils.js +0 -785
  132. package/build/esnext/utils/env.utils.js.map +0 -1
  133. package/build/esnext/utils/exception.utils.d.ts +0 -230
  134. package/build/esnext/utils/exception.utils.js +0 -381
  135. package/build/esnext/utils/exception.utils.js.map +0 -1
  136. package/build/esnext/utils/fs.utils.d.ts +0 -164
  137. package/build/esnext/utils/fs.utils.js +0 -349
  138. package/build/esnext/utils/fs.utils.js.map +0 -1
  139. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  140. package/build/esnext/utils/http-status-codes.js +0 -295
  141. package/build/esnext/utils/http-status-codes.js.map +0 -1
  142. package/build/esnext/utils/id.utils.d.ts +0 -36
  143. package/build/esnext/utils/id.utils.js +0 -84
  144. package/build/esnext/utils/id.utils.js.map +0 -1
  145. package/build/esnext/utils/logger.utils.d.ts +0 -166
  146. package/build/esnext/utils/logger.utils.js +0 -306
  147. package/build/esnext/utils/logger.utils.js.map +0 -1
  148. package/build/esnext/utils/middleware.utils.d.ts +0 -83
  149. package/build/esnext/utils/middleware.utils.js +0 -198
  150. package/build/esnext/utils/middleware.utils.js.map +0 -1
  151. package/build/esnext/utils/obj.utils.d.ts +0 -124
  152. package/build/esnext/utils/obj.utils.js +0 -413
  153. package/build/esnext/utils/obj.utils.js.map +0 -1
  154. package/build/esnext/utils/request.utils.d.ts +0 -77
  155. package/build/esnext/utils/request.utils.js +0 -168
  156. package/build/esnext/utils/request.utils.js.map +0 -1
  157. package/build/esnext/utils/response.utils.d.ts +0 -163
  158. package/build/esnext/utils/response.utils.js +0 -247
  159. package/build/esnext/utils/response.utils.js.map +0 -1
  160. package/build/esnext/utils/string.utils.d.ts +0 -93
  161. package/build/esnext/utils/string.utils.js +0 -165
  162. package/build/esnext/utils/string.utils.js.map +0 -1
  163. package/build/esnext/utils/url.utils.d.ts +0 -141
  164. package/build/esnext/utils/url.utils.js +0 -304
  165. package/build/esnext/utils/url.utils.js.map +0 -1
  166. package/build/esnext/utils/validate.utils.d.ts +0 -177
  167. package/build/esnext/utils/validate.utils.js +0 -320
  168. package/build/esnext/utils/validate.utils.js.map +0 -1
  169. package/build/src/config.d.ts +0 -130
  170. package/build/src/config.js +0 -150
  171. package/build/src/config.js.map +0 -1
  172. package/build/src/index.d.ts +0 -27
  173. package/build/src/index.js +0 -69
  174. package/build/src/index.js.map +0 -1
  175. package/build/src/servers/server.builder.d.ts +0 -625
  176. package/build/src/servers/server.builder.js +0 -681
  177. package/build/src/servers/server.builder.js.map +0 -1
  178. package/build/src/servers/server.d.ts +0 -256
  179. package/build/src/servers/server.js +0 -958
  180. package/build/src/servers/server.js.map +0 -1
  181. package/build/src/types/api-response.d.ts +0 -124
  182. package/build/src/types/api-response.js +0 -26
  183. package/build/src/types/api-response.js.map +0 -1
  184. package/build/src/types/index.d.ts +0 -125
  185. package/build/src/types/index.js +0 -26
  186. package/build/src/types/index.js.map +0 -1
  187. package/build/src/types/server.d.ts +0 -285
  188. package/build/src/types/server.js +0 -26
  189. package/build/src/types/server.js.map +0 -1
  190. package/build/src/utils/array.utils.d.ts +0 -168
  191. package/build/src/utils/array.utils.js +0 -364
  192. package/build/src/utils/array.utils.js.map +0 -1
  193. package/build/src/utils/async.utils.d.ts +0 -174
  194. package/build/src/utils/async.utils.js +0 -447
  195. package/build/src/utils/async.utils.js.map +0 -1
  196. package/build/src/utils/cache.utils.d.ts +0 -153
  197. package/build/src/utils/cache.utils.js +0 -297
  198. package/build/src/utils/cache.utils.js.map +0 -1
  199. package/build/src/utils/context-store.utils.d.ts +0 -186
  200. package/build/src/utils/context-store.utils.js +0 -293
  201. package/build/src/utils/context-store.utils.js.map +0 -1
  202. package/build/src/utils/crypto.utils.d.ts +0 -160
  203. package/build/src/utils/crypto.utils.js +0 -293
  204. package/build/src/utils/crypto.utils.js.map +0 -1
  205. package/build/src/utils/decorators.utils.d.ts +0 -315
  206. package/build/src/utils/decorators.utils.js +0 -516
  207. package/build/src/utils/decorators.utils.js.map +0 -1
  208. package/build/src/utils/dir.utils.d.ts +0 -196
  209. package/build/src/utils/dir.utils.js +0 -501
  210. package/build/src/utils/dir.utils.js.map +0 -1
  211. package/build/src/utils/env.utils.d.ts +0 -377
  212. package/build/src/utils/env.utils.js +0 -789
  213. package/build/src/utils/env.utils.js.map +0 -1
  214. package/build/src/utils/exception.utils.d.ts +0 -230
  215. package/build/src/utils/exception.utils.js +0 -407
  216. package/build/src/utils/exception.utils.js.map +0 -1
  217. package/build/src/utils/fs.utils.d.ts +0 -164
  218. package/build/src/utils/fs.utils.js +0 -372
  219. package/build/src/utils/fs.utils.js.map +0 -1
  220. package/build/src/utils/http-status-codes.d.ts +0 -266
  221. package/build/src/utils/http-status-codes.js +0 -298
  222. package/build/src/utils/http-status-codes.js.map +0 -1
  223. package/build/src/utils/id.utils.d.ts +0 -36
  224. package/build/src/utils/id.utils.js +0 -91
  225. package/build/src/utils/id.utils.js.map +0 -1
  226. package/build/src/utils/logger.utils.d.ts +0 -166
  227. package/build/src/utils/logger.utils.js +0 -352
  228. package/build/src/utils/logger.utils.js.map +0 -1
  229. package/build/src/utils/middleware.utils.d.ts +0 -83
  230. package/build/src/utils/middleware.utils.js +0 -205
  231. package/build/src/utils/middleware.utils.js.map +0 -1
  232. package/build/src/utils/obj.utils.d.ts +0 -124
  233. package/build/src/utils/obj.utils.js +0 -429
  234. package/build/src/utils/obj.utils.js.map +0 -1
  235. package/build/src/utils/request.utils.d.ts +0 -77
  236. package/build/src/utils/request.utils.js +0 -175
  237. package/build/src/utils/request.utils.js.map +0 -1
  238. package/build/src/utils/response.utils.d.ts +0 -163
  239. package/build/src/utils/response.utils.js +0 -260
  240. package/build/src/utils/response.utils.js.map +0 -1
  241. package/build/src/utils/string.utils.d.ts +0 -93
  242. package/build/src/utils/string.utils.js +0 -179
  243. package/build/src/utils/string.utils.js.map +0 -1
  244. package/build/src/utils/url.utils.d.ts +0 -141
  245. package/build/src/utils/url.utils.js +0 -317
  246. package/build/src/utils/url.utils.js.map +0 -1
  247. package/build/src/utils/validate.utils.d.ts +0 -177
  248. package/build/src/utils/validate.utils.js +0 -345
  249. package/build/src/utils/validate.utils.js.map +0 -1
@@ -1,196 +0,0 @@
1
- import fs from 'fs';
2
- /**
3
- * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
4
- *
5
- * @param {string} dirPath - The directory path to ensure.
6
- * @returns {Promise<void>} Resolves when the directory exists.
7
- * @throws {Error} If directory cannot be created.
8
- */
9
- export declare function ensureDir(dirPath: string): Promise<void>;
10
- /**
11
- * Recursively lists all files in a directory.
12
- *
13
- * @param {string} dirPath - The base directory.
14
- * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
15
- * @returns {Promise<string[]>} Array of absolute file paths.
16
- * @throws {Error} If the directory cannot be read.
17
- */
18
- export declare function listFiles(dirPath: string, recursive?: boolean): Promise<string[]>;
19
- /**
20
- * Deletes a directory and all its contents recursively (like `rm -rf`).
21
- *
22
- * @param {string} dirPath - Directory to delete.
23
- * @returns {Promise<void>} Resolves when deletion is complete.
24
- * @throws {Error} If deletion fails.
25
- */
26
- export declare function deleteDirRecursive(dirPath: string): Promise<void>;
27
- /**
28
- * Checks whether a given path is a directory.
29
- *
30
- * @param {string} pathStr - Path to check.
31
- * @returns {Promise<boolean>} True if the path is a directory, else false.
32
- */
33
- export declare function isDirectory(pathStr: string): Promise<boolean>;
34
- /**
35
- * Recursively copies a directory and all its contents to a destination.
36
- *
37
- * @param {string} src - Source directory path.
38
- * @param {string} dest - Destination directory path.
39
- * @returns {Promise<void>} Resolves when copy is complete.
40
- * @throws {Error} If source does not exist or copy fails.
41
- */
42
- export declare function copyDir(src: string, dest: string): Promise<void>;
43
- /**
44
- * Moves a directory to a new location by copying and deleting the original.
45
- *
46
- * @param {string} src - Source directory path.
47
- * @param {string} dest - Destination directory path.
48
- * @returns {Promise<void>} Resolves when move is complete.
49
- * @throws {Error} If copy or deletion fails.
50
- */
51
- export declare function moveDir(src: string, dest: string): Promise<void>;
52
- /**
53
- * Empties a directory by deleting all files and subdirectories inside it.
54
- *
55
- * @param {string} dirPath - Path to the directory to empty.
56
- * @returns {Promise<void>} Resolves when the directory has been emptied.
57
- * @throws {Error} If files or subdirectories cannot be removed.
58
- */
59
- export declare function emptyDir(dirPath: string): Promise<void>;
60
- /**
61
- * Calculates the total size (in bytes) of all files in a directory (recursive).
62
- *
63
- * @param {string} dirPath - Path to the directory.
64
- * @returns {Promise<number>} Total size in bytes.
65
- * @throws {Error} If any file stats cannot be read.
66
- */
67
- export declare function getDirSize(dirPath: string): Promise<number>;
68
- /**
69
- * Watches a directory for file changes and calls a callback on each event.
70
- *
71
- * @param {string} dirPath - Directory path to watch.
72
- * @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
73
- * @returns {() => void} A function to stop watching the directory.
74
- */
75
- export declare function watchDir(dirPath: string, callback: (eventType: 'rename' | 'change', filename: string | null) => void): () => void;
76
- /**
77
- * Finds files matching a glob pattern.
78
- *
79
- * @param {string} pattern - Glob pattern to match files.
80
- * @param {object} [options] - Options for glob pattern matching.
81
- * @param {string} [options.cwd] - Current working directory for relative patterns.
82
- * @param {boolean} [options.dot=false] - Include dotfiles in matches.
83
- * @param {boolean} [options.nodir=true] - Only match files, not directories.
84
- * @returns {Promise<string[]>} Array of matched file paths.
85
- * @throws {Error} If pattern matching fails.
86
- */
87
- export declare function findFilesByPattern(pattern: string, options?: {
88
- cwd?: string;
89
- dot?: boolean;
90
- nodir?: boolean;
91
- }): Promise<string[]>;
92
- /**
93
- * Gets all subdirectories in a directory.
94
- *
95
- * @param {string} dirPath - The directory to search in.
96
- * @param {boolean} [recursive=false] - Whether to include subdirectories recursively.
97
- * @returns {Promise<string[]>} Array of absolute subdirectory paths.
98
- * @throws {Error} If directory cannot be read.
99
- */
100
- export declare function getSubdirectories(dirPath: string, recursive?: boolean): Promise<string[]>;
101
- /**
102
- * Ensures a directory exists and is empty.
103
- *
104
- * @param {string} dirPath - Path to the directory.
105
- * @returns {Promise<void>} Resolves when the directory exists and is empty.
106
- * @throws {Error} If directory cannot be created or emptied.
107
- */
108
- export declare function ensureEmptyDir(dirPath: string): Promise<void>;
109
- /**
110
- * Creates a temporary directory with optional auto-cleanup.
111
- *
112
- * @param {object} [options] - Options for the temporary directory.
113
- * @param {string} [options.prefix='tmp-'] - Prefix for the directory name.
114
- * @param {string} [options.parentDir=os.tmpdir()] - Parent directory.
115
- * @param {boolean} [options.cleanup=false] - Whether to register cleanup on process exit.
116
- * @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
117
- * @throws {Error} If directory cannot be created.
118
- */
119
- export declare function createTempDir(options?: {
120
- prefix?: string;
121
- parentDir?: string;
122
- cleanup?: boolean;
123
- }): Promise<{
124
- path: string;
125
- cleanup: () => Promise<void>;
126
- }>;
127
- /**
128
- * Finds the newest file in a directory.
129
- *
130
- * @param {string} dirPath - Directory to search.
131
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
132
- * @returns {Promise<string | null>} Path to the newest file or null if no files.
133
- * @throws {Error} If directory cannot be read.
134
- */
135
- export declare function findNewestFile(dirPath: string, recursive?: boolean): Promise<string | null>;
136
- /**
137
- * Finds the oldest file in a directory.
138
- *
139
- * @param {string} dirPath - Directory to search.
140
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
141
- * @returns {Promise<string | null>} Path to the oldest file or null if no files.
142
- * @throws {Error} If directory cannot be read.
143
- */
144
- export declare function findOldestFile(dirPath: string, recursive?: boolean): Promise<string | null>;
145
- /**
146
- * Finds files or directories in a directory matching a predicate function.
147
- *
148
- * @param {string} dirPath - Directory to search.
149
- * @param {(path: string, stat: fs.Stats) => boolean | Promise<boolean>} predicate - Function to test each path.
150
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
151
- * @returns {Promise<string[]>} Array of matching paths.
152
- * @throws {Error} If directory cannot be read.
153
- */
154
- export declare function findInDir(dirPath: string, predicate: (path: string, stat: fs.Stats) => boolean | Promise<boolean>, recursive?: boolean): Promise<string[]>;
155
- /**
156
- * Watches a directory recursively for file changes.
157
- *
158
- * @param {string} dirPath - Base directory path to watch.
159
- * @param {(eventType: "rename" | "change", filename: string) => void} callback - Callback for each change event.
160
- * @param {boolean} [includeSubdirs=true] - Whether to watch subdirectories.
161
- * @returns {Promise<() => void>} A function to stop watching the directory.
162
- * @throws {Error} If directory cannot be watched.
163
- */
164
- export declare function watchDirRecursive(dirPath: string, callback: (eventType: 'rename' | 'change', filename: string) => void, includeSubdirs?: boolean): Promise<() => void>;
165
- /**
166
- * Gets detailed directory statistics including file count, directory count, and size.
167
- *
168
- * @param {string} dirPath - Path to the directory.
169
- * @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
170
- * @throws {Error} If directory cannot be read.
171
- */
172
- export declare function getDirStats(dirPath: string): Promise<{
173
- fileCount: number;
174
- dirCount: number;
175
- totalSize: number;
176
- }>;
177
- /**
178
- * Walks through a directory hierarchy, calling a visitor function for each entry.
179
- *
180
- * @param {string} dirPath - Starting directory path.
181
- * @param {object} options - Options for walking the directory.
182
- * @param {(entry: { path: string, name: string, isDirectory: boolean, stats: fs.Stats }) => boolean | void | Promise<boolean | void>} options.visitorFn -
183
- * Function called for each file/directory. Return false to skip a directory.
184
- * @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
185
- * @throws {Error} If directory cannot be read.
186
- */
187
- export declare function walkDir(dirPath: string, options: {
188
- visitorFn: (entry: {
189
- path: string;
190
- name: string;
191
- isDirectory: boolean;
192
- stats: fs.Stats;
193
- }) => boolean | void | Promise<boolean | void>;
194
- traversalOrder?: 'pre' | 'post';
195
- }): Promise<void>;
196
- //# sourceMappingURL=dir.utils.d.ts.map
@@ -1,477 +0,0 @@
1
- /*
2
- * The MIT License
3
- *
4
- * Copyright (c) 2025 Catbee Technologies
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- import os from 'os';
25
- import fs from 'fs';
26
- import fsp from 'fs/promises';
27
- import path from 'path';
28
- import { glob } from 'glob';
29
- import { randomBytes } from 'crypto';
30
- /**
31
- * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
32
- *
33
- * @param {string} dirPath - The directory path to ensure.
34
- * @returns {Promise<void>} Resolves when the directory exists.
35
- * @throws {Error} If directory cannot be created.
36
- */
37
- export async function ensureDir(dirPath) {
38
- await fsp.mkdir(dirPath, { recursive: true });
39
- }
40
- /**
41
- * Recursively lists all files in a directory.
42
- *
43
- * @param {string} dirPath - The base directory.
44
- * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
45
- * @returns {Promise<string[]>} Array of absolute file paths.
46
- * @throws {Error} If the directory cannot be read.
47
- */
48
- export async function listFiles(dirPath, recursive = false) {
49
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
50
- const files = [];
51
- for (const entry of entries) {
52
- const fullPath = path.join(dirPath, entry.name);
53
- if (entry.isFile()) {
54
- files.push(fullPath);
55
- }
56
- else if (recursive && entry.isDirectory()) {
57
- const nestedFiles = await listFiles(fullPath, true);
58
- files.push(...nestedFiles);
59
- }
60
- }
61
- return files;
62
- }
63
- /**
64
- * Deletes a directory and all its contents recursively (like `rm -rf`).
65
- *
66
- * @param {string} dirPath - Directory to delete.
67
- * @returns {Promise<void>} Resolves when deletion is complete.
68
- * @throws {Error} If deletion fails.
69
- */
70
- export async function deleteDirRecursive(dirPath) {
71
- await fsp.rm(dirPath, { recursive: true, force: true });
72
- }
73
- /**
74
- * Checks whether a given path is a directory.
75
- *
76
- * @param {string} pathStr - Path to check.
77
- * @returns {Promise<boolean>} True if the path is a directory, else false.
78
- */
79
- export async function isDirectory(pathStr) {
80
- try {
81
- const stat = await fsp.stat(pathStr);
82
- return stat.isDirectory();
83
- }
84
- catch (_a) {
85
- return false;
86
- }
87
- }
88
- /**
89
- * Recursively copies a directory and all its contents to a destination.
90
- *
91
- * @param {string} src - Source directory path.
92
- * @param {string} dest - Destination directory path.
93
- * @returns {Promise<void>} Resolves when copy is complete.
94
- * @throws {Error} If source does not exist or copy fails.
95
- */
96
- export async function copyDir(src, dest) {
97
- await ensureDir(dest);
98
- const entries = await fsp.readdir(src, { withFileTypes: true });
99
- for (const entry of entries) {
100
- const srcPath = path.join(src, entry.name);
101
- const destPath = path.join(dest, entry.name);
102
- if (entry.isDirectory()) {
103
- await copyDir(srcPath, destPath);
104
- }
105
- else {
106
- await fsp.copyFile(srcPath, destPath);
107
- }
108
- }
109
- }
110
- /**
111
- * Moves a directory to a new location by copying and deleting the original.
112
- *
113
- * @param {string} src - Source directory path.
114
- * @param {string} dest - Destination directory path.
115
- * @returns {Promise<void>} Resolves when move is complete.
116
- * @throws {Error} If copy or deletion fails.
117
- */
118
- export async function moveDir(src, dest) {
119
- await copyDir(src, dest);
120
- await deleteDirRecursive(src);
121
- }
122
- /**
123
- * Empties a directory by deleting all files and subdirectories inside it.
124
- *
125
- * @param {string} dirPath - Path to the directory to empty.
126
- * @returns {Promise<void>} Resolves when the directory has been emptied.
127
- * @throws {Error} If files or subdirectories cannot be removed.
128
- */
129
- export async function emptyDir(dirPath) {
130
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
131
- for (const entry of entries) {
132
- const entryPath = path.join(dirPath, entry.name);
133
- if (entry.isDirectory()) {
134
- await fsp.rm(entryPath, { recursive: true, force: true });
135
- }
136
- else {
137
- await fsp.unlink(entryPath);
138
- }
139
- }
140
- }
141
- /**
142
- * Calculates the total size (in bytes) of all files in a directory (recursive).
143
- *
144
- * @param {string} dirPath - Path to the directory.
145
- * @returns {Promise<number>} Total size in bytes.
146
- * @throws {Error} If any file stats cannot be read.
147
- */
148
- export async function getDirSize(dirPath) {
149
- let total = 0;
150
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
151
- for (const entry of entries) {
152
- const entryPath = path.join(dirPath, entry.name);
153
- if (entry.isDirectory()) {
154
- total += await getDirSize(entryPath);
155
- }
156
- else {
157
- const stat = await fsp.stat(entryPath);
158
- total += stat.size;
159
- }
160
- }
161
- return total;
162
- }
163
- /**
164
- * Watches a directory for file changes and calls a callback on each event.
165
- *
166
- * @param {string} dirPath - Directory path to watch.
167
- * @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
168
- * @returns {() => void} A function to stop watching the directory.
169
- */
170
- export function watchDir(dirPath, callback) {
171
- const watcher = fs.watch(dirPath, (eventType, filename) => {
172
- callback(eventType, filename);
173
- });
174
- return () => watcher.close();
175
- }
176
- /**
177
- * Finds files matching a glob pattern.
178
- *
179
- * @param {string} pattern - Glob pattern to match files.
180
- * @param {object} [options] - Options for glob pattern matching.
181
- * @param {string} [options.cwd] - Current working directory for relative patterns.
182
- * @param {boolean} [options.dot=false] - Include dotfiles in matches.
183
- * @param {boolean} [options.nodir=true] - Only match files, not directories.
184
- * @returns {Promise<string[]>} Array of matched file paths.
185
- * @throws {Error} If pattern matching fails.
186
- */
187
- export async function findFilesByPattern(pattern, options = {}) {
188
- const defaultOptions = Object.assign({ nodir: true }, options);
189
- return glob(pattern, defaultOptions);
190
- }
191
- /**
192
- * Gets all subdirectories in a directory.
193
- *
194
- * @param {string} dirPath - The directory to search in.
195
- * @param {boolean} [recursive=false] - Whether to include subdirectories recursively.
196
- * @returns {Promise<string[]>} Array of absolute subdirectory paths.
197
- * @throws {Error} If directory cannot be read.
198
- */
199
- export async function getSubdirectories(dirPath, recursive = false) {
200
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
201
- const dirs = entries.filter(entry => entry.isDirectory()).map(entry => path.join(dirPath, entry.name));
202
- if (recursive && dirs.length > 0) {
203
- const nestedDirs = await Promise.all(dirs.map(dir => getSubdirectories(dir, true)));
204
- return [...dirs, ...nestedDirs.flat()];
205
- }
206
- return dirs;
207
- }
208
- /**
209
- * Ensures a directory exists and is empty.
210
- *
211
- * @param {string} dirPath - Path to the directory.
212
- * @returns {Promise<void>} Resolves when the directory exists and is empty.
213
- * @throws {Error} If directory cannot be created or emptied.
214
- */
215
- export async function ensureEmptyDir(dirPath) {
216
- if (await isDirectory(dirPath)) {
217
- await emptyDir(dirPath);
218
- }
219
- else {
220
- try {
221
- await fsp.unlink(dirPath);
222
- }
223
- catch (error) {
224
- // Ignore if file doesn't exist
225
- if (error.code !== 'ENOENT') {
226
- throw error;
227
- }
228
- }
229
- await ensureDir(dirPath);
230
- }
231
- }
232
- /**
233
- * Creates a temporary directory with optional auto-cleanup.
234
- *
235
- * @param {object} [options] - Options for the temporary directory.
236
- * @param {string} [options.prefix='tmp-'] - Prefix for the directory name.
237
- * @param {string} [options.parentDir=os.tmpdir()] - Parent directory.
238
- * @param {boolean} [options.cleanup=false] - Whether to register cleanup on process exit.
239
- * @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
240
- * @throws {Error} If directory cannot be created.
241
- */
242
- export async function createTempDir(options = {}) {
243
- const prefix = options.prefix || 'tmp-';
244
- const parentDir = options.parentDir || os.tmpdir();
245
- const dirName = `${prefix}${randomBytes(6).toString('hex')}`;
246
- const tempDirPath = path.join(parentDir, dirName);
247
- await ensureDir(tempDirPath);
248
- const cleanup = async () => {
249
- try {
250
- await deleteDirRecursive(tempDirPath);
251
- }
252
- catch (_a) {
253
- // Ignore cleanup errors
254
- }
255
- };
256
- if (options.cleanup) {
257
- process.once('exit', () => {
258
- try {
259
- fs.rmSync(tempDirPath, { recursive: true, force: true });
260
- }
261
- catch (_a) {
262
- // Ignore cleanup errors on exit
263
- }
264
- });
265
- // Handle signals for better cleanup
266
- ['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2'].forEach(signal => {
267
- process.once(signal, () => {
268
- cleanup().then(() => process.exit());
269
- });
270
- });
271
- }
272
- return { path: tempDirPath, cleanup };
273
- }
274
- /**
275
- * Finds the newest file in a directory.
276
- *
277
- * @param {string} dirPath - Directory to search.
278
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
279
- * @returns {Promise<string | null>} Path to the newest file or null if no files.
280
- * @throws {Error} If directory cannot be read.
281
- */
282
- export async function findNewestFile(dirPath, recursive = false) {
283
- const files = await listFiles(dirPath, recursive);
284
- if (files.length === 0)
285
- return null;
286
- const stats = await Promise.all(files.map(async (file) => ({
287
- path: file,
288
- mtime: (await fsp.stat(file)).mtime
289
- })));
290
- return stats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime())[0].path;
291
- }
292
- /**
293
- * Finds the oldest file in a directory.
294
- *
295
- * @param {string} dirPath - Directory to search.
296
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
297
- * @returns {Promise<string | null>} Path to the oldest file or null if no files.
298
- * @throws {Error} If directory cannot be read.
299
- */
300
- export async function findOldestFile(dirPath, recursive = false) {
301
- const files = await listFiles(dirPath, recursive);
302
- if (files.length === 0)
303
- return null;
304
- const stats = await Promise.all(files.map(async (file) => ({
305
- path: file,
306
- mtime: (await fsp.stat(file)).mtime
307
- })));
308
- return stats.sort((a, b) => a.mtime.getTime() - b.mtime.getTime())[0].path;
309
- }
310
- /**
311
- * Finds files or directories in a directory matching a predicate function.
312
- *
313
- * @param {string} dirPath - Directory to search.
314
- * @param {(path: string, stat: fs.Stats) => boolean | Promise<boolean>} predicate - Function to test each path.
315
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
316
- * @returns {Promise<string[]>} Array of matching paths.
317
- * @throws {Error} If directory cannot be read.
318
- */
319
- export async function findInDir(dirPath, predicate, recursive = false) {
320
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
321
- const results = [];
322
- for (const entry of entries) {
323
- const entryPath = path.join(dirPath, entry.name);
324
- const stat = await fsp.stat(entryPath);
325
- if (await predicate(entryPath, stat)) {
326
- results.push(entryPath);
327
- }
328
- if (recursive && entry.isDirectory()) {
329
- const nestedResults = await findInDir(entryPath, predicate, true);
330
- results.push(...nestedResults);
331
- }
332
- }
333
- return results;
334
- }
335
- /**
336
- * Watches a directory recursively for file changes.
337
- *
338
- * @param {string} dirPath - Base directory path to watch.
339
- * @param {(eventType: "rename" | "change", filename: string) => void} callback - Callback for each change event.
340
- * @param {boolean} [includeSubdirs=true] - Whether to watch subdirectories.
341
- * @returns {Promise<() => void>} A function to stop watching the directory.
342
- * @throws {Error} If directory cannot be watched.
343
- */
344
- export async function watchDirRecursive(dirPath, callback, includeSubdirs = true) {
345
- // Normalize the path to ensure consistent path separators
346
- const normalizedBasePath = path.normalize(dirPath);
347
- // Create watchers for the base dir and all subdirectories
348
- const watchers = [];
349
- // Helper function to add a watcher for a directory
350
- const addWatcher = (dir) => {
351
- try {
352
- const watcher = fs.watch(dir, (eventType, filename) => {
353
- if (!filename)
354
- return;
355
- const fullPath = path.join(dir, filename);
356
- // Make the path relative to the base directory
357
- const relativePath = path.relative(normalizedBasePath, fullPath);
358
- callback(eventType, relativePath);
359
- // If a new directory is created, we should watch it
360
- if (eventType === 'rename' && includeSubdirs) {
361
- setTimeout(async () => {
362
- try {
363
- if (await isDirectory(fullPath)) {
364
- addWatcher(fullPath);
365
- }
366
- }
367
- catch (_a) {
368
- // Ignore errors checking newly created items
369
- }
370
- }, 100);
371
- }
372
- });
373
- watchers.push(watcher);
374
- }
375
- catch (_a) {
376
- // Silently ignore directories we can't watch
377
- }
378
- };
379
- // Add watcher for the base directory
380
- addWatcher(normalizedBasePath);
381
- // If watching subdirectories, add watchers for all existing subdirectories
382
- if (includeSubdirs) {
383
- const subdirs = await getSubdirectories(normalizedBasePath, true);
384
- for (const subdir of subdirs) {
385
- addWatcher(subdir);
386
- }
387
- }
388
- // Return a function to close all watchers
389
- return () => {
390
- watchers.forEach(watcher => watcher.close());
391
- };
392
- }
393
- /**
394
- * Gets detailed directory statistics including file count, directory count, and size.
395
- *
396
- * @param {string} dirPath - Path to the directory.
397
- * @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
398
- * @throws {Error} If directory cannot be read.
399
- */
400
- export async function getDirStats(dirPath) {
401
- let fileCount = 0;
402
- let dirCount = 0;
403
- let totalSize = 0;
404
- async function processDir(currentPath) {
405
- const entries = await fsp.readdir(currentPath, { withFileTypes: true });
406
- for (const entry of entries) {
407
- const entryPath = path.join(currentPath, entry.name);
408
- if (entry.isDirectory()) {
409
- dirCount++;
410
- await processDir(entryPath);
411
- }
412
- else if (entry.isFile()) {
413
- fileCount++;
414
- const stat = await fsp.stat(entryPath);
415
- totalSize += stat.size;
416
- }
417
- }
418
- }
419
- await processDir(dirPath);
420
- return { fileCount, dirCount, totalSize };
421
- }
422
- /**
423
- * Walks through a directory hierarchy, calling a visitor function for each entry.
424
- *
425
- * @param {string} dirPath - Starting directory path.
426
- * @param {object} options - Options for walking the directory.
427
- * @param {(entry: { path: string, name: string, isDirectory: boolean, stats: fs.Stats }) => boolean | void | Promise<boolean | void>} options.visitorFn -
428
- * Function called for each file/directory. Return false to skip a directory.
429
- * @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
430
- * @throws {Error} If directory cannot be read.
431
- */
432
- export async function walkDir(dirPath, options) {
433
- const traversalOrder = options.traversalOrder || 'pre';
434
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
435
- for (const entry of entries) {
436
- const entryPath = path.join(dirPath, entry.name);
437
- const stats = await fsp.stat(entryPath);
438
- const entryInfo = {
439
- path: entryPath,
440
- name: entry.name,
441
- isDirectory: entry.isDirectory(),
442
- stats
443
- };
444
- if (entry.isDirectory()) {
445
- // Pre-order: visit directory before its contents
446
- if (traversalOrder === 'pre') {
447
- const shouldContinue = await options.visitorFn(entryInfo);
448
- // If the visitor returns false, don't recurse into this directory
449
- if (shouldContinue !== false) {
450
- await walkDir(entryPath, options);
451
- }
452
- }
453
- else {
454
- // Post-order: visit directory after its contents
455
- await walkDir(entryPath, options);
456
- await options.visitorFn(entryInfo);
457
- }
458
- }
459
- else {
460
- // Files are always visited when encountered
461
- await options.visitorFn(entryInfo);
462
- }
463
- }
464
- if (traversalOrder === 'post') {
465
- // Only visit the root if this is the top-level call (not for subdirectories)
466
- // But since this function is always called recursively, always visit after children
467
- const stats = await fsp.stat(dirPath);
468
- const entryInfo = {
469
- path: dirPath,
470
- name: path.basename(dirPath),
471
- isDirectory: true,
472
- stats
473
- };
474
- await options.visitorFn(entryInfo);
475
- }
476
- }
477
- //# sourceMappingURL=dir.utils.js.map