@catbee/utils 0.0.7 → 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 (285) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +26 -1
  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 -99
  7. package/build/esm/config.d.ts +0 -122
  8. package/build/esm/config.js +0 -133
  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 -508
  14. package/build/esm/servers/server.builder.js +0 -702
  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 -1201
  18. package/build/esm/servers/server.js.map +0 -1
  19. package/build/esm/types/api-response.d.ts +0 -152
  20. package/build/esm/types/api-response.js +0 -34
  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 -268
  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 -265
  32. package/build/esm/utils/async.utils.js +0 -896
  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 -189
  38. package/build/esm/utils/context-store.utils.js +0 -307
  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/date.utils.d.ts +0 -159
  44. package/build/esm/utils/date.utils.js +0 -389
  45. package/build/esm/utils/date.utils.js.map +0 -1
  46. package/build/esm/utils/decorators.utils.d.ts +0 -315
  47. package/build/esm/utils/decorators.utils.js +0 -666
  48. package/build/esm/utils/decorators.utils.js.map +0 -1
  49. package/build/esm/utils/dir.utils.d.ts +0 -196
  50. package/build/esm/utils/dir.utils.js +0 -998
  51. package/build/esm/utils/dir.utils.js.map +0 -1
  52. package/build/esm/utils/env.utils.d.ts +0 -377
  53. package/build/esm/utils/env.utils.js +0 -873
  54. package/build/esm/utils/env.utils.js.map +0 -1
  55. package/build/esm/utils/exception.utils.d.ts +0 -230
  56. package/build/esm/utils/exception.utils.js +0 -559
  57. package/build/esm/utils/exception.utils.js.map +0 -1
  58. package/build/esm/utils/fs.utils.d.ts +0 -164
  59. package/build/esm/utils/fs.utils.js +0 -580
  60. package/build/esm/utils/fs.utils.js.map +0 -1
  61. package/build/esm/utils/http-status-codes.d.ts +0 -266
  62. package/build/esm/utils/http-status-codes.js +0 -295
  63. package/build/esm/utils/http-status-codes.js.map +0 -1
  64. package/build/esm/utils/id.utils.d.ts +0 -36
  65. package/build/esm/utils/id.utils.js +0 -87
  66. package/build/esm/utils/id.utils.js.map +0 -1
  67. package/build/esm/utils/logger.utils.d.ts +0 -160
  68. package/build/esm/utils/logger.utils.js +0 -341
  69. package/build/esm/utils/logger.utils.js.map +0 -1
  70. package/build/esm/utils/middleware.utils.d.ts +0 -83
  71. package/build/esm/utils/middleware.utils.js +0 -206
  72. package/build/esm/utils/middleware.utils.js.map +0 -1
  73. package/build/esm/utils/obj.utils.d.ts +0 -124
  74. package/build/esm/utils/obj.utils.js +0 -519
  75. package/build/esm/utils/obj.utils.js.map +0 -1
  76. package/build/esm/utils/performance.utils.d.ts +0 -136
  77. package/build/esm/utils/performance.utils.js +0 -376
  78. package/build/esm/utils/performance.utils.js.map +0 -1
  79. package/build/esm/utils/request.utils.d.ts +0 -86
  80. package/build/esm/utils/request.utils.js +0 -223
  81. package/build/esm/utils/request.utils.js.map +0 -1
  82. package/build/esm/utils/response.utils.d.ts +0 -163
  83. package/build/esm/utils/response.utils.js +0 -284
  84. package/build/esm/utils/response.utils.js.map +0 -1
  85. package/build/esm/utils/stream.utils.d.ts +0 -88
  86. package/build/esm/utils/stream.utils.js +0 -284
  87. package/build/esm/utils/stream.utils.js.map +0 -1
  88. package/build/esm/utils/string.utils.d.ts +0 -93
  89. package/build/esm/utils/string.utils.js +0 -169
  90. package/build/esm/utils/string.utils.js.map +0 -1
  91. package/build/esm/utils/type.utils.d.ts +0 -90
  92. package/build/esm/utils/type.utils.js +0 -190
  93. package/build/esm/utils/type.utils.js.map +0 -1
  94. package/build/esm/utils/url.utils.d.ts +0 -141
  95. package/build/esm/utils/url.utils.js +0 -381
  96. package/build/esm/utils/url.utils.js.map +0 -1
  97. package/build/esm/utils/validate.utils.d.ts +0 -177
  98. package/build/esm/utils/validate.utils.js +0 -336
  99. package/build/esm/utils/validate.utils.js.map +0 -1
  100. package/build/esnext/config.d.ts +0 -122
  101. package/build/esnext/config.js +0 -133
  102. package/build/esnext/config.js.map +0 -1
  103. package/build/esnext/index.d.ts +0 -27
  104. package/build/esnext/index.js +0 -50
  105. package/build/esnext/index.js.map +0 -1
  106. package/build/esnext/servers/server.builder.d.ts +0 -508
  107. package/build/esnext/servers/server.builder.js +0 -657
  108. package/build/esnext/servers/server.builder.js.map +0 -1
  109. package/build/esnext/servers/server.d.ts +0 -256
  110. package/build/esnext/servers/server.js +0 -908
  111. package/build/esnext/servers/server.js.map +0 -1
  112. package/build/esnext/types/api-response.d.ts +0 -152
  113. package/build/esnext/types/api-response.js +0 -34
  114. package/build/esnext/types/api-response.js.map +0 -1
  115. package/build/esnext/types/index.d.ts +0 -125
  116. package/build/esnext/types/index.js +0 -25
  117. package/build/esnext/types/index.js.map +0 -1
  118. package/build/esnext/types/server.d.ts +0 -268
  119. package/build/esnext/types/server.js +0 -25
  120. package/build/esnext/types/server.js.map +0 -1
  121. package/build/esnext/utils/array.utils.d.ts +0 -168
  122. package/build/esnext/utils/array.utils.js +0 -345
  123. package/build/esnext/utils/array.utils.js.map +0 -1
  124. package/build/esnext/utils/async.utils.d.ts +0 -265
  125. package/build/esnext/utils/async.utils.js +0 -620
  126. package/build/esnext/utils/async.utils.js.map +0 -1
  127. package/build/esnext/utils/cache.utils.d.ts +0 -153
  128. package/build/esnext/utils/cache.utils.js +0 -293
  129. package/build/esnext/utils/cache.utils.js.map +0 -1
  130. package/build/esnext/utils/context-store.utils.d.ts +0 -189
  131. package/build/esnext/utils/context-store.utils.js +0 -289
  132. package/build/esnext/utils/context-store.utils.js.map +0 -1
  133. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  134. package/build/esnext/utils/crypto.utils.js +0 -276
  135. package/build/esnext/utils/crypto.utils.js.map +0 -1
  136. package/build/esnext/utils/date.utils.d.ts +0 -159
  137. package/build/esnext/utils/date.utils.js +0 -384
  138. package/build/esnext/utils/date.utils.js.map +0 -1
  139. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  140. package/build/esnext/utils/decorators.utils.js +0 -504
  141. package/build/esnext/utils/decorators.utils.js.map +0 -1
  142. package/build/esnext/utils/dir.utils.d.ts +0 -196
  143. package/build/esnext/utils/dir.utils.js +0 -477
  144. package/build/esnext/utils/dir.utils.js.map +0 -1
  145. package/build/esnext/utils/env.utils.d.ts +0 -377
  146. package/build/esnext/utils/env.utils.js +0 -785
  147. package/build/esnext/utils/env.utils.js.map +0 -1
  148. package/build/esnext/utils/exception.utils.d.ts +0 -230
  149. package/build/esnext/utils/exception.utils.js +0 -381
  150. package/build/esnext/utils/exception.utils.js.map +0 -1
  151. package/build/esnext/utils/fs.utils.d.ts +0 -164
  152. package/build/esnext/utils/fs.utils.js +0 -349
  153. package/build/esnext/utils/fs.utils.js.map +0 -1
  154. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  155. package/build/esnext/utils/http-status-codes.js +0 -295
  156. package/build/esnext/utils/http-status-codes.js.map +0 -1
  157. package/build/esnext/utils/id.utils.d.ts +0 -36
  158. package/build/esnext/utils/id.utils.js +0 -84
  159. package/build/esnext/utils/id.utils.js.map +0 -1
  160. package/build/esnext/utils/logger.utils.d.ts +0 -160
  161. package/build/esnext/utils/logger.utils.js +0 -300
  162. package/build/esnext/utils/logger.utils.js.map +0 -1
  163. package/build/esnext/utils/middleware.utils.d.ts +0 -83
  164. package/build/esnext/utils/middleware.utils.js +0 -199
  165. package/build/esnext/utils/middleware.utils.js.map +0 -1
  166. package/build/esnext/utils/obj.utils.d.ts +0 -124
  167. package/build/esnext/utils/obj.utils.js +0 -413
  168. package/build/esnext/utils/obj.utils.js.map +0 -1
  169. package/build/esnext/utils/performance.utils.d.ts +0 -136
  170. package/build/esnext/utils/performance.utils.js +0 -271
  171. package/build/esnext/utils/performance.utils.js.map +0 -1
  172. package/build/esnext/utils/request.utils.d.ts +0 -86
  173. package/build/esnext/utils/request.utils.js +0 -186
  174. package/build/esnext/utils/request.utils.js.map +0 -1
  175. package/build/esnext/utils/response.utils.d.ts +0 -163
  176. package/build/esnext/utils/response.utils.js +0 -247
  177. package/build/esnext/utils/response.utils.js.map +0 -1
  178. package/build/esnext/utils/stream.utils.d.ts +0 -88
  179. package/build/esnext/utils/stream.utils.js +0 -210
  180. package/build/esnext/utils/stream.utils.js.map +0 -1
  181. package/build/esnext/utils/string.utils.d.ts +0 -93
  182. package/build/esnext/utils/string.utils.js +0 -165
  183. package/build/esnext/utils/string.utils.js.map +0 -1
  184. package/build/esnext/utils/type.utils.d.ts +0 -90
  185. package/build/esnext/utils/type.utils.js +0 -187
  186. package/build/esnext/utils/type.utils.js.map +0 -1
  187. package/build/esnext/utils/url.utils.d.ts +0 -141
  188. package/build/esnext/utils/url.utils.js +0 -304
  189. package/build/esnext/utils/url.utils.js.map +0 -1
  190. package/build/esnext/utils/validate.utils.d.ts +0 -177
  191. package/build/esnext/utils/validate.utils.js +0 -320
  192. package/build/esnext/utils/validate.utils.js.map +0 -1
  193. package/build/src/config.d.ts +0 -122
  194. package/build/src/config.js +0 -138
  195. package/build/src/config.js.map +0 -1
  196. package/build/src/index.d.ts +0 -27
  197. package/build/src/index.js +0 -69
  198. package/build/src/index.js.map +0 -1
  199. package/build/src/servers/server.builder.d.ts +0 -508
  200. package/build/src/servers/server.builder.js +0 -661
  201. package/build/src/servers/server.builder.js.map +0 -1
  202. package/build/src/servers/server.d.ts +0 -256
  203. package/build/src/servers/server.js +0 -948
  204. package/build/src/servers/server.js.map +0 -1
  205. package/build/src/types/api-response.d.ts +0 -152
  206. package/build/src/types/api-response.js +0 -37
  207. package/build/src/types/api-response.js.map +0 -1
  208. package/build/src/types/index.d.ts +0 -125
  209. package/build/src/types/index.js +0 -26
  210. package/build/src/types/index.js.map +0 -1
  211. package/build/src/types/server.d.ts +0 -268
  212. package/build/src/types/server.js +0 -26
  213. package/build/src/types/server.js.map +0 -1
  214. package/build/src/utils/array.utils.d.ts +0 -168
  215. package/build/src/utils/array.utils.js +0 -364
  216. package/build/src/utils/array.utils.js.map +0 -1
  217. package/build/src/utils/async.utils.d.ts +0 -265
  218. package/build/src/utils/async.utils.js +0 -641
  219. package/build/src/utils/async.utils.js.map +0 -1
  220. package/build/src/utils/cache.utils.d.ts +0 -153
  221. package/build/src/utils/cache.utils.js +0 -297
  222. package/build/src/utils/cache.utils.js.map +0 -1
  223. package/build/src/utils/context-store.utils.d.ts +0 -189
  224. package/build/src/utils/context-store.utils.js +0 -296
  225. package/build/src/utils/context-store.utils.js.map +0 -1
  226. package/build/src/utils/crypto.utils.d.ts +0 -160
  227. package/build/src/utils/crypto.utils.js +0 -293
  228. package/build/src/utils/crypto.utils.js.map +0 -1
  229. package/build/src/utils/date.utils.d.ts +0 -159
  230. package/build/src/utils/date.utils.js +0 -396
  231. package/build/src/utils/date.utils.js.map +0 -1
  232. package/build/src/utils/decorators.utils.d.ts +0 -315
  233. package/build/src/utils/decorators.utils.js +0 -516
  234. package/build/src/utils/decorators.utils.js.map +0 -1
  235. package/build/src/utils/dir.utils.d.ts +0 -196
  236. package/build/src/utils/dir.utils.js +0 -501
  237. package/build/src/utils/dir.utils.js.map +0 -1
  238. package/build/src/utils/env.utils.d.ts +0 -377
  239. package/build/src/utils/env.utils.js +0 -789
  240. package/build/src/utils/env.utils.js.map +0 -1
  241. package/build/src/utils/exception.utils.d.ts +0 -230
  242. package/build/src/utils/exception.utils.js +0 -407
  243. package/build/src/utils/exception.utils.js.map +0 -1
  244. package/build/src/utils/fs.utils.d.ts +0 -164
  245. package/build/src/utils/fs.utils.js +0 -372
  246. package/build/src/utils/fs.utils.js.map +0 -1
  247. package/build/src/utils/http-status-codes.d.ts +0 -266
  248. package/build/src/utils/http-status-codes.js +0 -298
  249. package/build/src/utils/http-status-codes.js.map +0 -1
  250. package/build/src/utils/id.utils.d.ts +0 -36
  251. package/build/src/utils/id.utils.js +0 -91
  252. package/build/src/utils/id.utils.js.map +0 -1
  253. package/build/src/utils/logger.utils.d.ts +0 -160
  254. package/build/src/utils/logger.utils.js +0 -346
  255. package/build/src/utils/logger.utils.js.map +0 -1
  256. package/build/src/utils/middleware.utils.d.ts +0 -83
  257. package/build/src/utils/middleware.utils.js +0 -206
  258. package/build/src/utils/middleware.utils.js.map +0 -1
  259. package/build/src/utils/obj.utils.d.ts +0 -124
  260. package/build/src/utils/obj.utils.js +0 -429
  261. package/build/src/utils/obj.utils.js.map +0 -1
  262. package/build/src/utils/performance.utils.d.ts +0 -136
  263. package/build/src/utils/performance.utils.js +0 -278
  264. package/build/src/utils/performance.utils.js.map +0 -1
  265. package/build/src/utils/request.utils.d.ts +0 -86
  266. package/build/src/utils/request.utils.js +0 -195
  267. package/build/src/utils/request.utils.js.map +0 -1
  268. package/build/src/utils/response.utils.d.ts +0 -163
  269. package/build/src/utils/response.utils.js +0 -260
  270. package/build/src/utils/response.utils.js.map +0 -1
  271. package/build/src/utils/stream.utils.d.ts +0 -88
  272. package/build/src/utils/stream.utils.js +0 -218
  273. package/build/src/utils/stream.utils.js.map +0 -1
  274. package/build/src/utils/string.utils.d.ts +0 -93
  275. package/build/src/utils/string.utils.js +0 -179
  276. package/build/src/utils/string.utils.js.map +0 -1
  277. package/build/src/utils/type.utils.d.ts +0 -90
  278. package/build/src/utils/type.utils.js +0 -196
  279. package/build/src/utils/type.utils.js.map +0 -1
  280. package/build/src/utils/url.utils.d.ts +0 -141
  281. package/build/src/utils/url.utils.js +0 -317
  282. package/build/src/utils/url.utils.js.map +0 -1
  283. package/build/src/utils/validate.utils.d.ts +0 -177
  284. package/build/src/utils/validate.utils.js +0 -345
  285. package/build/src/utils/validate.utils.js.map +0 -1
@@ -1,998 +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
- var __assign = (this && this.__assign) || function () {
25
- __assign = Object.assign || function(t) {
26
- for (var s, i = 1, n = arguments.length; i < n; i++) {
27
- s = arguments[i];
28
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
29
- t[p] = s[p];
30
- }
31
- return t;
32
- };
33
- return __assign.apply(this, arguments);
34
- };
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
43
- };
44
- var __generator = (this && this.__generator) || function (thisArg, body) {
45
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
46
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
- function verb(n) { return function (v) { return step([n, v]); }; }
48
- function step(op) {
49
- if (f) throw new TypeError("Generator is already executing.");
50
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
52
- if (y = 0, t) op = [op[0] & 2, t.value];
53
- switch (op[0]) {
54
- case 0: case 1: t = op; break;
55
- case 4: _.label++; return { value: op[1], done: false };
56
- case 5: _.label++; y = op[1]; op = [0]; continue;
57
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
- default:
59
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
- if (t[2]) _.ops.pop();
64
- _.trys.pop(); continue;
65
- }
66
- op = body.call(thisArg, _);
67
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
- }
70
- };
71
- var __values = (this && this.__values) || function(o) {
72
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
73
- if (m) return m.call(o);
74
- if (o && typeof o.length === "number") return {
75
- next: function () {
76
- if (o && i >= o.length) o = void 0;
77
- return { value: o && o[i++], done: !o };
78
- }
79
- };
80
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
81
- };
82
- var __read = (this && this.__read) || function (o, n) {
83
- var m = typeof Symbol === "function" && o[Symbol.iterator];
84
- if (!m) return o;
85
- var i = m.call(o), r, ar = [], e;
86
- try {
87
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
88
- }
89
- catch (error) { e = { error: error }; }
90
- finally {
91
- try {
92
- if (r && !r.done && (m = i["return"])) m.call(i);
93
- }
94
- finally { if (e) throw e.error; }
95
- }
96
- return ar;
97
- };
98
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
99
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
100
- if (ar || !(i in from)) {
101
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
102
- ar[i] = from[i];
103
- }
104
- }
105
- return to.concat(ar || Array.prototype.slice.call(from));
106
- };
107
- import os from 'os';
108
- import fs from 'fs';
109
- import fsp from 'fs/promises';
110
- import path from 'path';
111
- import { glob } from 'glob';
112
- import { randomBytes } from 'crypto';
113
- /**
114
- * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
115
- *
116
- * @param {string} dirPath - The directory path to ensure.
117
- * @returns {Promise<void>} Resolves when the directory exists.
118
- * @throws {Error} If directory cannot be created.
119
- */
120
- export function ensureDir(dirPath) {
121
- return __awaiter(this, void 0, void 0, function () {
122
- return __generator(this, function (_a) {
123
- switch (_a.label) {
124
- case 0: return [4 /*yield*/, fsp.mkdir(dirPath, { recursive: true })];
125
- case 1:
126
- _a.sent();
127
- return [2 /*return*/];
128
- }
129
- });
130
- });
131
- }
132
- /**
133
- * Recursively lists all files in a directory.
134
- *
135
- * @param {string} dirPath - The base directory.
136
- * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
137
- * @returns {Promise<string[]>} Array of absolute file paths.
138
- * @throws {Error} If the directory cannot be read.
139
- */
140
- export function listFiles(dirPath_1) {
141
- return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
142
- var entries, files, entries_1, entries_1_1, entry, fullPath, nestedFiles, e_1_1;
143
- var e_1, _a;
144
- if (recursive === void 0) { recursive = false; }
145
- return __generator(this, function (_b) {
146
- switch (_b.label) {
147
- case 0: return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
148
- case 1:
149
- entries = _b.sent();
150
- files = [];
151
- _b.label = 2;
152
- case 2:
153
- _b.trys.push([2, 8, 9, 10]);
154
- entries_1 = __values(entries), entries_1_1 = entries_1.next();
155
- _b.label = 3;
156
- case 3:
157
- if (!!entries_1_1.done) return [3 /*break*/, 7];
158
- entry = entries_1_1.value;
159
- fullPath = path.join(dirPath, entry.name);
160
- if (!entry.isFile()) return [3 /*break*/, 4];
161
- files.push(fullPath);
162
- return [3 /*break*/, 6];
163
- case 4:
164
- if (!(recursive && entry.isDirectory())) return [3 /*break*/, 6];
165
- return [4 /*yield*/, listFiles(fullPath, true)];
166
- case 5:
167
- nestedFiles = _b.sent();
168
- files.push.apply(files, __spreadArray([], __read(nestedFiles), false));
169
- _b.label = 6;
170
- case 6:
171
- entries_1_1 = entries_1.next();
172
- return [3 /*break*/, 3];
173
- case 7: return [3 /*break*/, 10];
174
- case 8:
175
- e_1_1 = _b.sent();
176
- e_1 = { error: e_1_1 };
177
- return [3 /*break*/, 10];
178
- case 9:
179
- try {
180
- if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
181
- }
182
- finally { if (e_1) throw e_1.error; }
183
- return [7 /*endfinally*/];
184
- case 10: return [2 /*return*/, files];
185
- }
186
- });
187
- });
188
- }
189
- /**
190
- * Deletes a directory and all its contents recursively (like `rm -rf`).
191
- *
192
- * @param {string} dirPath - Directory to delete.
193
- * @returns {Promise<void>} Resolves when deletion is complete.
194
- * @throws {Error} If deletion fails.
195
- */
196
- export function deleteDirRecursive(dirPath) {
197
- return __awaiter(this, void 0, void 0, function () {
198
- return __generator(this, function (_a) {
199
- switch (_a.label) {
200
- case 0: return [4 /*yield*/, fsp.rm(dirPath, { recursive: true, force: true })];
201
- case 1:
202
- _a.sent();
203
- return [2 /*return*/];
204
- }
205
- });
206
- });
207
- }
208
- /**
209
- * Checks whether a given path is a directory.
210
- *
211
- * @param {string} pathStr - Path to check.
212
- * @returns {Promise<boolean>} True if the path is a directory, else false.
213
- */
214
- export function isDirectory(pathStr) {
215
- return __awaiter(this, void 0, void 0, function () {
216
- var stat, _a;
217
- return __generator(this, function (_b) {
218
- switch (_b.label) {
219
- case 0:
220
- _b.trys.push([0, 2, , 3]);
221
- return [4 /*yield*/, fsp.stat(pathStr)];
222
- case 1:
223
- stat = _b.sent();
224
- return [2 /*return*/, stat.isDirectory()];
225
- case 2:
226
- _a = _b.sent();
227
- return [2 /*return*/, false];
228
- case 3: return [2 /*return*/];
229
- }
230
- });
231
- });
232
- }
233
- /**
234
- * Recursively copies a directory and all its contents to a destination.
235
- *
236
- * @param {string} src - Source directory path.
237
- * @param {string} dest - Destination directory path.
238
- * @returns {Promise<void>} Resolves when copy is complete.
239
- * @throws {Error} If source does not exist or copy fails.
240
- */
241
- export function copyDir(src, dest) {
242
- return __awaiter(this, void 0, void 0, function () {
243
- var entries, entries_2, entries_2_1, entry, srcPath, destPath, e_2_1;
244
- var e_2, _a;
245
- return __generator(this, function (_b) {
246
- switch (_b.label) {
247
- case 0: return [4 /*yield*/, ensureDir(dest)];
248
- case 1:
249
- _b.sent();
250
- return [4 /*yield*/, fsp.readdir(src, { withFileTypes: true })];
251
- case 2:
252
- entries = _b.sent();
253
- _b.label = 3;
254
- case 3:
255
- _b.trys.push([3, 10, 11, 12]);
256
- entries_2 = __values(entries), entries_2_1 = entries_2.next();
257
- _b.label = 4;
258
- case 4:
259
- if (!!entries_2_1.done) return [3 /*break*/, 9];
260
- entry = entries_2_1.value;
261
- srcPath = path.join(src, entry.name);
262
- destPath = path.join(dest, entry.name);
263
- if (!entry.isDirectory()) return [3 /*break*/, 6];
264
- return [4 /*yield*/, copyDir(srcPath, destPath)];
265
- case 5:
266
- _b.sent();
267
- return [3 /*break*/, 8];
268
- case 6: return [4 /*yield*/, fsp.copyFile(srcPath, destPath)];
269
- case 7:
270
- _b.sent();
271
- _b.label = 8;
272
- case 8:
273
- entries_2_1 = entries_2.next();
274
- return [3 /*break*/, 4];
275
- case 9: return [3 /*break*/, 12];
276
- case 10:
277
- e_2_1 = _b.sent();
278
- e_2 = { error: e_2_1 };
279
- return [3 /*break*/, 12];
280
- case 11:
281
- try {
282
- if (entries_2_1 && !entries_2_1.done && (_a = entries_2.return)) _a.call(entries_2);
283
- }
284
- finally { if (e_2) throw e_2.error; }
285
- return [7 /*endfinally*/];
286
- case 12: return [2 /*return*/];
287
- }
288
- });
289
- });
290
- }
291
- /**
292
- * Moves a directory to a new location by copying and deleting the original.
293
- *
294
- * @param {string} src - Source directory path.
295
- * @param {string} dest - Destination directory path.
296
- * @returns {Promise<void>} Resolves when move is complete.
297
- * @throws {Error} If copy or deletion fails.
298
- */
299
- export function moveDir(src, dest) {
300
- return __awaiter(this, void 0, void 0, function () {
301
- return __generator(this, function (_a) {
302
- switch (_a.label) {
303
- case 0: return [4 /*yield*/, copyDir(src, dest)];
304
- case 1:
305
- _a.sent();
306
- return [4 /*yield*/, deleteDirRecursive(src)];
307
- case 2:
308
- _a.sent();
309
- return [2 /*return*/];
310
- }
311
- });
312
- });
313
- }
314
- /**
315
- * Empties a directory by deleting all files and subdirectories inside it.
316
- *
317
- * @param {string} dirPath - Path to the directory to empty.
318
- * @returns {Promise<void>} Resolves when the directory has been emptied.
319
- * @throws {Error} If files or subdirectories cannot be removed.
320
- */
321
- export function emptyDir(dirPath) {
322
- return __awaiter(this, void 0, void 0, function () {
323
- var entries, entries_3, entries_3_1, entry, entryPath, e_3_1;
324
- var e_3, _a;
325
- return __generator(this, function (_b) {
326
- switch (_b.label) {
327
- case 0: return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
328
- case 1:
329
- entries = _b.sent();
330
- _b.label = 2;
331
- case 2:
332
- _b.trys.push([2, 9, 10, 11]);
333
- entries_3 = __values(entries), entries_3_1 = entries_3.next();
334
- _b.label = 3;
335
- case 3:
336
- if (!!entries_3_1.done) return [3 /*break*/, 8];
337
- entry = entries_3_1.value;
338
- entryPath = path.join(dirPath, entry.name);
339
- if (!entry.isDirectory()) return [3 /*break*/, 5];
340
- return [4 /*yield*/, fsp.rm(entryPath, { recursive: true, force: true })];
341
- case 4:
342
- _b.sent();
343
- return [3 /*break*/, 7];
344
- case 5: return [4 /*yield*/, fsp.unlink(entryPath)];
345
- case 6:
346
- _b.sent();
347
- _b.label = 7;
348
- case 7:
349
- entries_3_1 = entries_3.next();
350
- return [3 /*break*/, 3];
351
- case 8: return [3 /*break*/, 11];
352
- case 9:
353
- e_3_1 = _b.sent();
354
- e_3 = { error: e_3_1 };
355
- return [3 /*break*/, 11];
356
- case 10:
357
- try {
358
- if (entries_3_1 && !entries_3_1.done && (_a = entries_3.return)) _a.call(entries_3);
359
- }
360
- finally { if (e_3) throw e_3.error; }
361
- return [7 /*endfinally*/];
362
- case 11: return [2 /*return*/];
363
- }
364
- });
365
- });
366
- }
367
- /**
368
- * Calculates the total size (in bytes) of all files in a directory (recursive).
369
- *
370
- * @param {string} dirPath - Path to the directory.
371
- * @returns {Promise<number>} Total size in bytes.
372
- * @throws {Error} If any file stats cannot be read.
373
- */
374
- export function getDirSize(dirPath) {
375
- return __awaiter(this, void 0, void 0, function () {
376
- var total, entries, entries_4, entries_4_1, entry, entryPath, _a, stat, e_4_1;
377
- var e_4, _b;
378
- return __generator(this, function (_c) {
379
- switch (_c.label) {
380
- case 0:
381
- total = 0;
382
- return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
383
- case 1:
384
- entries = _c.sent();
385
- _c.label = 2;
386
- case 2:
387
- _c.trys.push([2, 9, 10, 11]);
388
- entries_4 = __values(entries), entries_4_1 = entries_4.next();
389
- _c.label = 3;
390
- case 3:
391
- if (!!entries_4_1.done) return [3 /*break*/, 8];
392
- entry = entries_4_1.value;
393
- entryPath = path.join(dirPath, entry.name);
394
- if (!entry.isDirectory()) return [3 /*break*/, 5];
395
- _a = total;
396
- return [4 /*yield*/, getDirSize(entryPath)];
397
- case 4:
398
- total = _a + _c.sent();
399
- return [3 /*break*/, 7];
400
- case 5: return [4 /*yield*/, fsp.stat(entryPath)];
401
- case 6:
402
- stat = _c.sent();
403
- total += stat.size;
404
- _c.label = 7;
405
- case 7:
406
- entries_4_1 = entries_4.next();
407
- return [3 /*break*/, 3];
408
- case 8: return [3 /*break*/, 11];
409
- case 9:
410
- e_4_1 = _c.sent();
411
- e_4 = { error: e_4_1 };
412
- return [3 /*break*/, 11];
413
- case 10:
414
- try {
415
- if (entries_4_1 && !entries_4_1.done && (_b = entries_4.return)) _b.call(entries_4);
416
- }
417
- finally { if (e_4) throw e_4.error; }
418
- return [7 /*endfinally*/];
419
- case 11: return [2 /*return*/, total];
420
- }
421
- });
422
- });
423
- }
424
- /**
425
- * Watches a directory for file changes and calls a callback on each event.
426
- *
427
- * @param {string} dirPath - Directory path to watch.
428
- * @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
429
- * @returns {() => void} A function to stop watching the directory.
430
- */
431
- export function watchDir(dirPath, callback) {
432
- var watcher = fs.watch(dirPath, function (eventType, filename) {
433
- callback(eventType, filename);
434
- });
435
- return function () { return watcher.close(); };
436
- }
437
- /**
438
- * Finds files matching a glob pattern.
439
- *
440
- * @param {string} pattern - Glob pattern to match files.
441
- * @param {object} [options] - Options for glob pattern matching.
442
- * @param {string} [options.cwd] - Current working directory for relative patterns.
443
- * @param {boolean} [options.dot=false] - Include dotfiles in matches.
444
- * @param {boolean} [options.nodir=true] - Only match files, not directories.
445
- * @returns {Promise<string[]>} Array of matched file paths.
446
- * @throws {Error} If pattern matching fails.
447
- */
448
- export function findFilesByPattern(pattern_1) {
449
- return __awaiter(this, arguments, void 0, function (pattern, options) {
450
- var defaultOptions;
451
- if (options === void 0) { options = {}; }
452
- return __generator(this, function (_a) {
453
- defaultOptions = __assign({ nodir: true }, options);
454
- return [2 /*return*/, glob(pattern, defaultOptions)];
455
- });
456
- });
457
- }
458
- /**
459
- * Gets all subdirectories in a directory.
460
- *
461
- * @param {string} dirPath - The directory to search in.
462
- * @param {boolean} [recursive=false] - Whether to include subdirectories recursively.
463
- * @returns {Promise<string[]>} Array of absolute subdirectory paths.
464
- * @throws {Error} If directory cannot be read.
465
- */
466
- export function getSubdirectories(dirPath_1) {
467
- return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
468
- var entries, dirs, nestedDirs;
469
- if (recursive === void 0) { recursive = false; }
470
- return __generator(this, function (_a) {
471
- switch (_a.label) {
472
- case 0: return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
473
- case 1:
474
- entries = _a.sent();
475
- dirs = entries.filter(function (entry) { return entry.isDirectory(); }).map(function (entry) { return path.join(dirPath, entry.name); });
476
- if (!(recursive && dirs.length > 0)) return [3 /*break*/, 3];
477
- return [4 /*yield*/, Promise.all(dirs.map(function (dir) { return getSubdirectories(dir, true); }))];
478
- case 2:
479
- nestedDirs = _a.sent();
480
- return [2 /*return*/, __spreadArray(__spreadArray([], __read(dirs), false), __read(nestedDirs.flat()), false)];
481
- case 3: return [2 /*return*/, dirs];
482
- }
483
- });
484
- });
485
- }
486
- /**
487
- * Ensures a directory exists and is empty.
488
- *
489
- * @param {string} dirPath - Path to the directory.
490
- * @returns {Promise<void>} Resolves when the directory exists and is empty.
491
- * @throws {Error} If directory cannot be created or emptied.
492
- */
493
- export function ensureEmptyDir(dirPath) {
494
- return __awaiter(this, void 0, void 0, function () {
495
- var error_1;
496
- return __generator(this, function (_a) {
497
- switch (_a.label) {
498
- case 0: return [4 /*yield*/, isDirectory(dirPath)];
499
- case 1:
500
- if (!_a.sent()) return [3 /*break*/, 3];
501
- return [4 /*yield*/, emptyDir(dirPath)];
502
- case 2:
503
- _a.sent();
504
- return [3 /*break*/, 8];
505
- case 3:
506
- _a.trys.push([3, 5, , 6]);
507
- return [4 /*yield*/, fsp.unlink(dirPath)];
508
- case 4:
509
- _a.sent();
510
- return [3 /*break*/, 6];
511
- case 5:
512
- error_1 = _a.sent();
513
- // Ignore if file doesn't exist
514
- if (error_1.code !== 'ENOENT') {
515
- throw error_1;
516
- }
517
- return [3 /*break*/, 6];
518
- case 6: return [4 /*yield*/, ensureDir(dirPath)];
519
- case 7:
520
- _a.sent();
521
- _a.label = 8;
522
- case 8: return [2 /*return*/];
523
- }
524
- });
525
- });
526
- }
527
- /**
528
- * Creates a temporary directory with optional auto-cleanup.
529
- *
530
- * @param {object} [options] - Options for the temporary directory.
531
- * @param {string} [options.prefix='tmp-'] - Prefix for the directory name.
532
- * @param {string} [options.parentDir=os.tmpdir()] - Parent directory.
533
- * @param {boolean} [options.cleanup=false] - Whether to register cleanup on process exit.
534
- * @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
535
- * @throws {Error} If directory cannot be created.
536
- */
537
- export function createTempDir() {
538
- return __awaiter(this, arguments, void 0, function (options) {
539
- var prefix, parentDir, dirName, tempDirPath, cleanup;
540
- var _this = this;
541
- if (options === void 0) { options = {}; }
542
- return __generator(this, function (_a) {
543
- switch (_a.label) {
544
- case 0:
545
- prefix = options.prefix || 'tmp-';
546
- parentDir = options.parentDir || os.tmpdir();
547
- dirName = "".concat(prefix).concat(randomBytes(6).toString('hex'));
548
- tempDirPath = path.join(parentDir, dirName);
549
- return [4 /*yield*/, ensureDir(tempDirPath)];
550
- case 1:
551
- _a.sent();
552
- cleanup = function () { return __awaiter(_this, void 0, void 0, function () {
553
- var _a;
554
- return __generator(this, function (_b) {
555
- switch (_b.label) {
556
- case 0:
557
- _b.trys.push([0, 2, , 3]);
558
- return [4 /*yield*/, deleteDirRecursive(tempDirPath)];
559
- case 1:
560
- _b.sent();
561
- return [3 /*break*/, 3];
562
- case 2:
563
- _a = _b.sent();
564
- return [3 /*break*/, 3];
565
- case 3: return [2 /*return*/];
566
- }
567
- });
568
- }); };
569
- if (options.cleanup) {
570
- process.once('exit', function () {
571
- try {
572
- fs.rmSync(tempDirPath, { recursive: true, force: true });
573
- }
574
- catch (_a) {
575
- // Ignore cleanup errors on exit
576
- }
577
- });
578
- // Handle signals for better cleanup
579
- ['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2'].forEach(function (signal) {
580
- process.once(signal, function () {
581
- cleanup().then(function () { return process.exit(); });
582
- });
583
- });
584
- }
585
- return [2 /*return*/, { path: tempDirPath, cleanup: cleanup }];
586
- }
587
- });
588
- });
589
- }
590
- /**
591
- * Finds the newest file in a directory.
592
- *
593
- * @param {string} dirPath - Directory to search.
594
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
595
- * @returns {Promise<string | null>} Path to the newest file or null if no files.
596
- * @throws {Error} If directory cannot be read.
597
- */
598
- export function findNewestFile(dirPath_1) {
599
- return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
600
- var files, stats;
601
- var _this = this;
602
- if (recursive === void 0) { recursive = false; }
603
- return __generator(this, function (_a) {
604
- switch (_a.label) {
605
- case 0: return [4 /*yield*/, listFiles(dirPath, recursive)];
606
- case 1:
607
- files = _a.sent();
608
- if (files.length === 0)
609
- return [2 /*return*/, null];
610
- return [4 /*yield*/, Promise.all(files.map(function (file) { return __awaiter(_this, void 0, void 0, function () {
611
- var _a;
612
- return __generator(this, function (_b) {
613
- switch (_b.label) {
614
- case 0:
615
- _a = {
616
- path: file
617
- };
618
- return [4 /*yield*/, fsp.stat(file)];
619
- case 1: return [2 /*return*/, (_a.mtime = (_b.sent()).mtime,
620
- _a)];
621
- }
622
- });
623
- }); }))];
624
- case 2:
625
- stats = _a.sent();
626
- return [2 /*return*/, stats.sort(function (a, b) { return b.mtime.getTime() - a.mtime.getTime(); })[0].path];
627
- }
628
- });
629
- });
630
- }
631
- /**
632
- * Finds the oldest file in a directory.
633
- *
634
- * @param {string} dirPath - Directory to search.
635
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
636
- * @returns {Promise<string | null>} Path to the oldest file or null if no files.
637
- * @throws {Error} If directory cannot be read.
638
- */
639
- export function findOldestFile(dirPath_1) {
640
- return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
641
- var files, stats;
642
- var _this = this;
643
- if (recursive === void 0) { recursive = false; }
644
- return __generator(this, function (_a) {
645
- switch (_a.label) {
646
- case 0: return [4 /*yield*/, listFiles(dirPath, recursive)];
647
- case 1:
648
- files = _a.sent();
649
- if (files.length === 0)
650
- return [2 /*return*/, null];
651
- return [4 /*yield*/, Promise.all(files.map(function (file) { return __awaiter(_this, void 0, void 0, function () {
652
- var _a;
653
- return __generator(this, function (_b) {
654
- switch (_b.label) {
655
- case 0:
656
- _a = {
657
- path: file
658
- };
659
- return [4 /*yield*/, fsp.stat(file)];
660
- case 1: return [2 /*return*/, (_a.mtime = (_b.sent()).mtime,
661
- _a)];
662
- }
663
- });
664
- }); }))];
665
- case 2:
666
- stats = _a.sent();
667
- return [2 /*return*/, stats.sort(function (a, b) { return a.mtime.getTime() - b.mtime.getTime(); })[0].path];
668
- }
669
- });
670
- });
671
- }
672
- /**
673
- * Finds files or directories in a directory matching a predicate function.
674
- *
675
- * @param {string} dirPath - Directory to search.
676
- * @param {(path: string, stat: fs.Stats) => boolean | Promise<boolean>} predicate - Function to test each path.
677
- * @param {boolean} [recursive=false] - Whether to search subdirectories.
678
- * @returns {Promise<string[]>} Array of matching paths.
679
- * @throws {Error} If directory cannot be read.
680
- */
681
- export function findInDir(dirPath_1, predicate_1) {
682
- return __awaiter(this, arguments, void 0, function (dirPath, predicate, recursive) {
683
- var entries, results, entries_5, entries_5_1, entry, entryPath, stat, nestedResults, e_5_1;
684
- var e_5, _a;
685
- if (recursive === void 0) { recursive = false; }
686
- return __generator(this, function (_b) {
687
- switch (_b.label) {
688
- case 0: return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
689
- case 1:
690
- entries = _b.sent();
691
- results = [];
692
- _b.label = 2;
693
- case 2:
694
- _b.trys.push([2, 9, 10, 11]);
695
- entries_5 = __values(entries), entries_5_1 = entries_5.next();
696
- _b.label = 3;
697
- case 3:
698
- if (!!entries_5_1.done) return [3 /*break*/, 8];
699
- entry = entries_5_1.value;
700
- entryPath = path.join(dirPath, entry.name);
701
- return [4 /*yield*/, fsp.stat(entryPath)];
702
- case 4:
703
- stat = _b.sent();
704
- return [4 /*yield*/, predicate(entryPath, stat)];
705
- case 5:
706
- if (_b.sent()) {
707
- results.push(entryPath);
708
- }
709
- if (!(recursive && entry.isDirectory())) return [3 /*break*/, 7];
710
- return [4 /*yield*/, findInDir(entryPath, predicate, true)];
711
- case 6:
712
- nestedResults = _b.sent();
713
- results.push.apply(results, __spreadArray([], __read(nestedResults), false));
714
- _b.label = 7;
715
- case 7:
716
- entries_5_1 = entries_5.next();
717
- return [3 /*break*/, 3];
718
- case 8: return [3 /*break*/, 11];
719
- case 9:
720
- e_5_1 = _b.sent();
721
- e_5 = { error: e_5_1 };
722
- return [3 /*break*/, 11];
723
- case 10:
724
- try {
725
- if (entries_5_1 && !entries_5_1.done && (_a = entries_5.return)) _a.call(entries_5);
726
- }
727
- finally { if (e_5) throw e_5.error; }
728
- return [7 /*endfinally*/];
729
- case 11: return [2 /*return*/, results];
730
- }
731
- });
732
- });
733
- }
734
- /**
735
- * Watches a directory recursively for file changes.
736
- *
737
- * @param {string} dirPath - Base directory path to watch.
738
- * @param {(eventType: "rename" | "change", filename: string) => void} callback - Callback for each change event.
739
- * @param {boolean} [includeSubdirs=true] - Whether to watch subdirectories.
740
- * @returns {Promise<() => void>} A function to stop watching the directory.
741
- * @throws {Error} If directory cannot be watched.
742
- */
743
- export function watchDirRecursive(dirPath_1, callback_1) {
744
- return __awaiter(this, arguments, void 0, function (dirPath, callback, includeSubdirs) {
745
- var normalizedBasePath, watchers, addWatcher, subdirs, subdirs_1, subdirs_1_1, subdir;
746
- var e_6, _a;
747
- var _this = this;
748
- if (includeSubdirs === void 0) { includeSubdirs = true; }
749
- return __generator(this, function (_b) {
750
- switch (_b.label) {
751
- case 0:
752
- normalizedBasePath = path.normalize(dirPath);
753
- watchers = [];
754
- addWatcher = function (dir) {
755
- try {
756
- var watcher = fs.watch(dir, function (eventType, filename) {
757
- if (!filename)
758
- return;
759
- var fullPath = path.join(dir, filename);
760
- // Make the path relative to the base directory
761
- var relativePath = path.relative(normalizedBasePath, fullPath);
762
- callback(eventType, relativePath);
763
- // If a new directory is created, we should watch it
764
- if (eventType === 'rename' && includeSubdirs) {
765
- setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
766
- var _a;
767
- return __generator(this, function (_b) {
768
- switch (_b.label) {
769
- case 0:
770
- _b.trys.push([0, 2, , 3]);
771
- return [4 /*yield*/, isDirectory(fullPath)];
772
- case 1:
773
- if (_b.sent()) {
774
- addWatcher(fullPath);
775
- }
776
- return [3 /*break*/, 3];
777
- case 2:
778
- _a = _b.sent();
779
- return [3 /*break*/, 3];
780
- case 3: return [2 /*return*/];
781
- }
782
- });
783
- }); }, 100);
784
- }
785
- });
786
- watchers.push(watcher);
787
- }
788
- catch (_a) {
789
- // Silently ignore directories we can't watch
790
- }
791
- };
792
- // Add watcher for the base directory
793
- addWatcher(normalizedBasePath);
794
- if (!includeSubdirs) return [3 /*break*/, 2];
795
- return [4 /*yield*/, getSubdirectories(normalizedBasePath, true)];
796
- case 1:
797
- subdirs = _b.sent();
798
- try {
799
- for (subdirs_1 = __values(subdirs), subdirs_1_1 = subdirs_1.next(); !subdirs_1_1.done; subdirs_1_1 = subdirs_1.next()) {
800
- subdir = subdirs_1_1.value;
801
- addWatcher(subdir);
802
- }
803
- }
804
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
805
- finally {
806
- try {
807
- if (subdirs_1_1 && !subdirs_1_1.done && (_a = subdirs_1.return)) _a.call(subdirs_1);
808
- }
809
- finally { if (e_6) throw e_6.error; }
810
- }
811
- _b.label = 2;
812
- case 2:
813
- // Return a function to close all watchers
814
- return [2 /*return*/, function () {
815
- watchers.forEach(function (watcher) { return watcher.close(); });
816
- }];
817
- }
818
- });
819
- });
820
- }
821
- /**
822
- * Gets detailed directory statistics including file count, directory count, and size.
823
- *
824
- * @param {string} dirPath - Path to the directory.
825
- * @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
826
- * @throws {Error} If directory cannot be read.
827
- */
828
- export function getDirStats(dirPath) {
829
- return __awaiter(this, void 0, void 0, function () {
830
- function processDir(currentPath) {
831
- return __awaiter(this, void 0, void 0, function () {
832
- var entries, entries_6, entries_6_1, entry, entryPath, stat, e_7_1;
833
- var e_7, _a;
834
- return __generator(this, function (_b) {
835
- switch (_b.label) {
836
- case 0: return [4 /*yield*/, fsp.readdir(currentPath, { withFileTypes: true })];
837
- case 1:
838
- entries = _b.sent();
839
- _b.label = 2;
840
- case 2:
841
- _b.trys.push([2, 9, 10, 11]);
842
- entries_6 = __values(entries), entries_6_1 = entries_6.next();
843
- _b.label = 3;
844
- case 3:
845
- if (!!entries_6_1.done) return [3 /*break*/, 8];
846
- entry = entries_6_1.value;
847
- entryPath = path.join(currentPath, entry.name);
848
- if (!entry.isDirectory()) return [3 /*break*/, 5];
849
- dirCount++;
850
- return [4 /*yield*/, processDir(entryPath)];
851
- case 4:
852
- _b.sent();
853
- return [3 /*break*/, 7];
854
- case 5:
855
- if (!entry.isFile()) return [3 /*break*/, 7];
856
- fileCount++;
857
- return [4 /*yield*/, fsp.stat(entryPath)];
858
- case 6:
859
- stat = _b.sent();
860
- totalSize += stat.size;
861
- _b.label = 7;
862
- case 7:
863
- entries_6_1 = entries_6.next();
864
- return [3 /*break*/, 3];
865
- case 8: return [3 /*break*/, 11];
866
- case 9:
867
- e_7_1 = _b.sent();
868
- e_7 = { error: e_7_1 };
869
- return [3 /*break*/, 11];
870
- case 10:
871
- try {
872
- if (entries_6_1 && !entries_6_1.done && (_a = entries_6.return)) _a.call(entries_6);
873
- }
874
- finally { if (e_7) throw e_7.error; }
875
- return [7 /*endfinally*/];
876
- case 11: return [2 /*return*/];
877
- }
878
- });
879
- });
880
- }
881
- var fileCount, dirCount, totalSize;
882
- return __generator(this, function (_a) {
883
- switch (_a.label) {
884
- case 0:
885
- fileCount = 0;
886
- dirCount = 0;
887
- totalSize = 0;
888
- return [4 /*yield*/, processDir(dirPath)];
889
- case 1:
890
- _a.sent();
891
- return [2 /*return*/, { fileCount: fileCount, dirCount: dirCount, totalSize: totalSize }];
892
- }
893
- });
894
- });
895
- }
896
- /**
897
- * Walks through a directory hierarchy, calling a visitor function for each entry.
898
- *
899
- * @param {string} dirPath - Starting directory path.
900
- * @param {object} options - Options for walking the directory.
901
- * @param {(entry: { path: string, name: string, isDirectory: boolean, stats: fs.Stats }) => boolean | void | Promise<boolean | void>} options.visitorFn -
902
- * Function called for each file/directory. Return false to skip a directory.
903
- * @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
904
- * @throws {Error} If directory cannot be read.
905
- */
906
- export function walkDir(dirPath, options) {
907
- return __awaiter(this, void 0, void 0, function () {
908
- var traversalOrder, entries, entries_7, entries_7_1, entry, entryPath, stats, entryInfo, shouldContinue, e_8_1, stats, entryInfo;
909
- var e_8, _a;
910
- return __generator(this, function (_b) {
911
- switch (_b.label) {
912
- case 0:
913
- traversalOrder = options.traversalOrder || 'pre';
914
- return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
915
- case 1:
916
- entries = _b.sent();
917
- _b.label = 2;
918
- case 2:
919
- _b.trys.push([2, 16, 17, 18]);
920
- entries_7 = __values(entries), entries_7_1 = entries_7.next();
921
- _b.label = 3;
922
- case 3:
923
- if (!!entries_7_1.done) return [3 /*break*/, 15];
924
- entry = entries_7_1.value;
925
- entryPath = path.join(dirPath, entry.name);
926
- return [4 /*yield*/, fsp.stat(entryPath)];
927
- case 4:
928
- stats = _b.sent();
929
- entryInfo = {
930
- path: entryPath,
931
- name: entry.name,
932
- isDirectory: entry.isDirectory(),
933
- stats: stats
934
- };
935
- if (!entry.isDirectory()) return [3 /*break*/, 12];
936
- if (!(traversalOrder === 'pre')) return [3 /*break*/, 8];
937
- return [4 /*yield*/, options.visitorFn(entryInfo)];
938
- case 5:
939
- shouldContinue = _b.sent();
940
- if (!(shouldContinue !== false)) return [3 /*break*/, 7];
941
- return [4 /*yield*/, walkDir(entryPath, options)];
942
- case 6:
943
- _b.sent();
944
- _b.label = 7;
945
- case 7: return [3 /*break*/, 11];
946
- case 8:
947
- // Post-order: visit directory after its contents
948
- return [4 /*yield*/, walkDir(entryPath, options)];
949
- case 9:
950
- // Post-order: visit directory after its contents
951
- _b.sent();
952
- return [4 /*yield*/, options.visitorFn(entryInfo)];
953
- case 10:
954
- _b.sent();
955
- _b.label = 11;
956
- case 11: return [3 /*break*/, 14];
957
- case 12:
958
- // Files are always visited when encountered
959
- return [4 /*yield*/, options.visitorFn(entryInfo)];
960
- case 13:
961
- // Files are always visited when encountered
962
- _b.sent();
963
- _b.label = 14;
964
- case 14:
965
- entries_7_1 = entries_7.next();
966
- return [3 /*break*/, 3];
967
- case 15: return [3 /*break*/, 18];
968
- case 16:
969
- e_8_1 = _b.sent();
970
- e_8 = { error: e_8_1 };
971
- return [3 /*break*/, 18];
972
- case 17:
973
- try {
974
- if (entries_7_1 && !entries_7_1.done && (_a = entries_7.return)) _a.call(entries_7);
975
- }
976
- finally { if (e_8) throw e_8.error; }
977
- return [7 /*endfinally*/];
978
- case 18:
979
- if (!(traversalOrder === 'post')) return [3 /*break*/, 21];
980
- return [4 /*yield*/, fsp.stat(dirPath)];
981
- case 19:
982
- stats = _b.sent();
983
- entryInfo = {
984
- path: dirPath,
985
- name: path.basename(dirPath),
986
- isDirectory: true,
987
- stats: stats
988
- };
989
- return [4 /*yield*/, options.visitorFn(entryInfo)];
990
- case 20:
991
- _b.sent();
992
- _b.label = 21;
993
- case 21: return [2 /*return*/];
994
- }
995
- });
996
- });
997
- }
998
- //# sourceMappingURL=dir.utils.js.map