@catbee/utils 0.0.8-rc.0 → 0.0.8-rc.2

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 (281) hide show
  1. package/README.md +25 -1
  2. package/build/esm/config.d.ts +0 -1
  3. package/build/esm/config.js +6 -7
  4. package/build/esm/index.d.ts +0 -1
  5. package/build/esm/index.js +0 -1
  6. package/build/esm/servers/server.builder.d.ts +0 -1
  7. package/build/esm/servers/server.builder.js +99 -147
  8. package/build/esm/servers/server.d.ts +0 -1
  9. package/build/esm/servers/server.js +591 -876
  10. package/build/esm/types/api-response.d.ts +0 -1
  11. package/build/esm/types/api-response.js +0 -1
  12. package/build/esm/types/index.d.ts +0 -1
  13. package/build/esm/types/index.js +0 -1
  14. package/build/esm/types/server.d.ts +0 -1
  15. package/build/esm/types/server.js +0 -1
  16. package/build/esm/utils/array.utils.d.ts +0 -1
  17. package/build/esm/utils/array.utils.js +48 -86
  18. package/build/esm/utils/async.utils.d.ts +0 -1
  19. package/build/esm/utils/async.utils.js +247 -524
  20. package/build/esm/utils/cache.utils.d.ts +0 -1
  21. package/build/esm/utils/cache.utils.js +96 -301
  22. package/build/esm/utils/context-store.utils.d.ts +0 -1
  23. package/build/esm/utils/context-store.utils.js +62 -79
  24. package/build/esm/utils/crypto.utils.d.ts +0 -1
  25. package/build/esm/utils/crypto.utils.js +78 -175
  26. package/build/esm/utils/date.utils.d.ts +0 -1
  27. package/build/esm/utils/date.utils.js +47 -53
  28. package/build/esm/utils/decorators.utils.d.ts +201 -5
  29. package/build/esm/utils/decorators.utils.js +632 -285
  30. package/build/esm/utils/dir.utils.d.ts +0 -1
  31. package/build/esm/utils/dir.utils.js +264 -786
  32. package/build/esm/utils/env.utils.d.ts +0 -1
  33. package/build/esm/utils/env.utils.js +180 -271
  34. package/build/esm/utils/exception.utils.d.ts +0 -1
  35. package/build/esm/utils/exception.utils.js +112 -289
  36. package/build/esm/utils/fs.utils.d.ts +0 -1
  37. package/build/esm/utils/fs.utils.js +169 -401
  38. package/build/esm/utils/http-status-codes.d.ts +0 -1
  39. package/build/esm/utils/http-status-codes.js +0 -1
  40. package/build/esm/utils/id.utils.d.ts +0 -1
  41. package/build/esm/utils/id.utils.js +5 -9
  42. package/build/esm/utils/logger.utils.d.ts +0 -1
  43. package/build/esm/utils/logger.utils.js +34 -71
  44. package/build/esm/utils/middleware.utils.d.ts +0 -1
  45. package/build/esm/utils/middleware.utils.js +65 -155
  46. package/build/esm/utils/obj.utils.d.ts +0 -1
  47. package/build/esm/utils/obj.utils.js +69 -176
  48. package/build/esm/utils/performance.utils.d.ts +0 -1
  49. package/build/esm/utils/performance.utils.js +92 -195
  50. package/build/esm/utils/request.utils.d.ts +0 -1
  51. package/build/esm/utils/request.utils.js +41 -74
  52. package/build/esm/utils/response.utils.d.ts +0 -1
  53. package/build/esm/utils/response.utils.js +84 -107
  54. package/build/esm/utils/stream.utils.d.ts +0 -1
  55. package/build/esm/utils/stream.utils.js +38 -113
  56. package/build/esm/utils/string.utils.d.ts +0 -1
  57. package/build/esm/utils/string.utils.js +6 -11
  58. package/build/esm/utils/type.utils.d.ts +0 -1
  59. package/build/esm/utils/type.utils.js +8 -12
  60. package/build/esm/utils/url.utils.d.ts +0 -1
  61. package/build/esm/utils/url.utils.js +49 -127
  62. package/build/esm/utils/validate.utils.d.ts +0 -1
  63. package/build/esm/utils/validate.utils.js +23 -40
  64. package/package.json +3 -33
  65. package/build/esm/config.js.map +0 -1
  66. package/build/esm/index.js.map +0 -1
  67. package/build/esm/servers/server.builder.js.map +0 -1
  68. package/build/esm/servers/server.js.map +0 -1
  69. package/build/esm/types/api-response.js.map +0 -1
  70. package/build/esm/types/index.js.map +0 -1
  71. package/build/esm/types/server.js.map +0 -1
  72. package/build/esm/utils/array.utils.js.map +0 -1
  73. package/build/esm/utils/async.utils.js.map +0 -1
  74. package/build/esm/utils/cache.utils.js.map +0 -1
  75. package/build/esm/utils/context-store.utils.js.map +0 -1
  76. package/build/esm/utils/crypto.utils.js.map +0 -1
  77. package/build/esm/utils/date.utils.js.map +0 -1
  78. package/build/esm/utils/decorators.utils.js.map +0 -1
  79. package/build/esm/utils/dir.utils.js.map +0 -1
  80. package/build/esm/utils/env.utils.js.map +0 -1
  81. package/build/esm/utils/exception.utils.js.map +0 -1
  82. package/build/esm/utils/fs.utils.js.map +0 -1
  83. package/build/esm/utils/http-status-codes.js.map +0 -1
  84. package/build/esm/utils/id.utils.js.map +0 -1
  85. package/build/esm/utils/logger.utils.js.map +0 -1
  86. package/build/esm/utils/middleware.utils.js.map +0 -1
  87. package/build/esm/utils/obj.utils.js.map +0 -1
  88. package/build/esm/utils/performance.utils.js.map +0 -1
  89. package/build/esm/utils/request.utils.js.map +0 -1
  90. package/build/esm/utils/response.utils.js.map +0 -1
  91. package/build/esm/utils/stream.utils.js.map +0 -1
  92. package/build/esm/utils/string.utils.js.map +0 -1
  93. package/build/esm/utils/type.utils.js.map +0 -1
  94. package/build/esm/utils/url.utils.js.map +0 -1
  95. package/build/esm/utils/validate.utils.js.map +0 -1
  96. package/build/esnext/config.d.ts +0 -122
  97. package/build/esnext/config.js +0 -133
  98. package/build/esnext/config.js.map +0 -1
  99. package/build/esnext/index.d.ts +0 -27
  100. package/build/esnext/index.js +0 -50
  101. package/build/esnext/index.js.map +0 -1
  102. package/build/esnext/servers/server.builder.d.ts +0 -508
  103. package/build/esnext/servers/server.builder.js +0 -657
  104. package/build/esnext/servers/server.builder.js.map +0 -1
  105. package/build/esnext/servers/server.d.ts +0 -256
  106. package/build/esnext/servers/server.js +0 -955
  107. package/build/esnext/servers/server.js.map +0 -1
  108. package/build/esnext/types/api-response.d.ts +0 -152
  109. package/build/esnext/types/api-response.js +0 -34
  110. package/build/esnext/types/api-response.js.map +0 -1
  111. package/build/esnext/types/index.d.ts +0 -125
  112. package/build/esnext/types/index.js +0 -25
  113. package/build/esnext/types/index.js.map +0 -1
  114. package/build/esnext/types/server.d.ts +0 -268
  115. package/build/esnext/types/server.js +0 -25
  116. package/build/esnext/types/server.js.map +0 -1
  117. package/build/esnext/utils/array.utils.d.ts +0 -168
  118. package/build/esnext/utils/array.utils.js +0 -345
  119. package/build/esnext/utils/array.utils.js.map +0 -1
  120. package/build/esnext/utils/async.utils.d.ts +0 -265
  121. package/build/esnext/utils/async.utils.js +0 -620
  122. package/build/esnext/utils/async.utils.js.map +0 -1
  123. package/build/esnext/utils/cache.utils.d.ts +0 -153
  124. package/build/esnext/utils/cache.utils.js +0 -293
  125. package/build/esnext/utils/cache.utils.js.map +0 -1
  126. package/build/esnext/utils/context-store.utils.d.ts +0 -189
  127. package/build/esnext/utils/context-store.utils.js +0 -289
  128. package/build/esnext/utils/context-store.utils.js.map +0 -1
  129. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  130. package/build/esnext/utils/crypto.utils.js +0 -276
  131. package/build/esnext/utils/crypto.utils.js.map +0 -1
  132. package/build/esnext/utils/date.utils.d.ts +0 -159
  133. package/build/esnext/utils/date.utils.js +0 -384
  134. package/build/esnext/utils/date.utils.js.map +0 -1
  135. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  136. package/build/esnext/utils/decorators.utils.js +0 -504
  137. package/build/esnext/utils/decorators.utils.js.map +0 -1
  138. package/build/esnext/utils/dir.utils.d.ts +0 -196
  139. package/build/esnext/utils/dir.utils.js +0 -477
  140. package/build/esnext/utils/dir.utils.js.map +0 -1
  141. package/build/esnext/utils/env.utils.d.ts +0 -377
  142. package/build/esnext/utils/env.utils.js +0 -785
  143. package/build/esnext/utils/env.utils.js.map +0 -1
  144. package/build/esnext/utils/exception.utils.d.ts +0 -230
  145. package/build/esnext/utils/exception.utils.js +0 -381
  146. package/build/esnext/utils/exception.utils.js.map +0 -1
  147. package/build/esnext/utils/fs.utils.d.ts +0 -164
  148. package/build/esnext/utils/fs.utils.js +0 -349
  149. package/build/esnext/utils/fs.utils.js.map +0 -1
  150. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  151. package/build/esnext/utils/http-status-codes.js +0 -295
  152. package/build/esnext/utils/http-status-codes.js.map +0 -1
  153. package/build/esnext/utils/id.utils.d.ts +0 -36
  154. package/build/esnext/utils/id.utils.js +0 -84
  155. package/build/esnext/utils/id.utils.js.map +0 -1
  156. package/build/esnext/utils/logger.utils.d.ts +0 -160
  157. package/build/esnext/utils/logger.utils.js +0 -300
  158. package/build/esnext/utils/logger.utils.js.map +0 -1
  159. package/build/esnext/utils/middleware.utils.d.ts +0 -100
  160. package/build/esnext/utils/middleware.utils.js +0 -236
  161. package/build/esnext/utils/middleware.utils.js.map +0 -1
  162. package/build/esnext/utils/obj.utils.d.ts +0 -124
  163. package/build/esnext/utils/obj.utils.js +0 -413
  164. package/build/esnext/utils/obj.utils.js.map +0 -1
  165. package/build/esnext/utils/performance.utils.d.ts +0 -136
  166. package/build/esnext/utils/performance.utils.js +0 -271
  167. package/build/esnext/utils/performance.utils.js.map +0 -1
  168. package/build/esnext/utils/request.utils.d.ts +0 -86
  169. package/build/esnext/utils/request.utils.js +0 -186
  170. package/build/esnext/utils/request.utils.js.map +0 -1
  171. package/build/esnext/utils/response.utils.d.ts +0 -163
  172. package/build/esnext/utils/response.utils.js +0 -247
  173. package/build/esnext/utils/response.utils.js.map +0 -1
  174. package/build/esnext/utils/stream.utils.d.ts +0 -88
  175. package/build/esnext/utils/stream.utils.js +0 -210
  176. package/build/esnext/utils/stream.utils.js.map +0 -1
  177. package/build/esnext/utils/string.utils.d.ts +0 -93
  178. package/build/esnext/utils/string.utils.js +0 -165
  179. package/build/esnext/utils/string.utils.js.map +0 -1
  180. package/build/esnext/utils/type.utils.d.ts +0 -90
  181. package/build/esnext/utils/type.utils.js +0 -187
  182. package/build/esnext/utils/type.utils.js.map +0 -1
  183. package/build/esnext/utils/url.utils.d.ts +0 -141
  184. package/build/esnext/utils/url.utils.js +0 -304
  185. package/build/esnext/utils/url.utils.js.map +0 -1
  186. package/build/esnext/utils/validate.utils.d.ts +0 -177
  187. package/build/esnext/utils/validate.utils.js +0 -320
  188. package/build/esnext/utils/validate.utils.js.map +0 -1
  189. package/build/src/config.d.ts +0 -122
  190. package/build/src/config.js +0 -138
  191. package/build/src/config.js.map +0 -1
  192. package/build/src/index.d.ts +0 -27
  193. package/build/src/index.js +0 -69
  194. package/build/src/index.js.map +0 -1
  195. package/build/src/servers/server.builder.d.ts +0 -508
  196. package/build/src/servers/server.builder.js +0 -661
  197. package/build/src/servers/server.builder.js.map +0 -1
  198. package/build/src/servers/server.d.ts +0 -256
  199. package/build/src/servers/server.js +0 -962
  200. package/build/src/servers/server.js.map +0 -1
  201. package/build/src/types/api-response.d.ts +0 -152
  202. package/build/src/types/api-response.js +0 -37
  203. package/build/src/types/api-response.js.map +0 -1
  204. package/build/src/types/index.d.ts +0 -125
  205. package/build/src/types/index.js +0 -26
  206. package/build/src/types/index.js.map +0 -1
  207. package/build/src/types/server.d.ts +0 -268
  208. package/build/src/types/server.js +0 -26
  209. package/build/src/types/server.js.map +0 -1
  210. package/build/src/utils/array.utils.d.ts +0 -168
  211. package/build/src/utils/array.utils.js +0 -364
  212. package/build/src/utils/array.utils.js.map +0 -1
  213. package/build/src/utils/async.utils.d.ts +0 -265
  214. package/build/src/utils/async.utils.js +0 -641
  215. package/build/src/utils/async.utils.js.map +0 -1
  216. package/build/src/utils/cache.utils.d.ts +0 -153
  217. package/build/src/utils/cache.utils.js +0 -297
  218. package/build/src/utils/cache.utils.js.map +0 -1
  219. package/build/src/utils/context-store.utils.d.ts +0 -189
  220. package/build/src/utils/context-store.utils.js +0 -296
  221. package/build/src/utils/context-store.utils.js.map +0 -1
  222. package/build/src/utils/crypto.utils.d.ts +0 -160
  223. package/build/src/utils/crypto.utils.js +0 -293
  224. package/build/src/utils/crypto.utils.js.map +0 -1
  225. package/build/src/utils/date.utils.d.ts +0 -159
  226. package/build/src/utils/date.utils.js +0 -396
  227. package/build/src/utils/date.utils.js.map +0 -1
  228. package/build/src/utils/decorators.utils.d.ts +0 -315
  229. package/build/src/utils/decorators.utils.js +0 -516
  230. package/build/src/utils/decorators.utils.js.map +0 -1
  231. package/build/src/utils/dir.utils.d.ts +0 -196
  232. package/build/src/utils/dir.utils.js +0 -501
  233. package/build/src/utils/dir.utils.js.map +0 -1
  234. package/build/src/utils/env.utils.d.ts +0 -377
  235. package/build/src/utils/env.utils.js +0 -789
  236. package/build/src/utils/env.utils.js.map +0 -1
  237. package/build/src/utils/exception.utils.d.ts +0 -230
  238. package/build/src/utils/exception.utils.js +0 -407
  239. package/build/src/utils/exception.utils.js.map +0 -1
  240. package/build/src/utils/fs.utils.d.ts +0 -164
  241. package/build/src/utils/fs.utils.js +0 -372
  242. package/build/src/utils/fs.utils.js.map +0 -1
  243. package/build/src/utils/http-status-codes.d.ts +0 -266
  244. package/build/src/utils/http-status-codes.js +0 -298
  245. package/build/src/utils/http-status-codes.js.map +0 -1
  246. package/build/src/utils/id.utils.d.ts +0 -36
  247. package/build/src/utils/id.utils.js +0 -91
  248. package/build/src/utils/id.utils.js.map +0 -1
  249. package/build/src/utils/logger.utils.d.ts +0 -160
  250. package/build/src/utils/logger.utils.js +0 -346
  251. package/build/src/utils/logger.utils.js.map +0 -1
  252. package/build/src/utils/middleware.utils.d.ts +0 -100
  253. package/build/src/utils/middleware.utils.js +0 -244
  254. package/build/src/utils/middleware.utils.js.map +0 -1
  255. package/build/src/utils/obj.utils.d.ts +0 -124
  256. package/build/src/utils/obj.utils.js +0 -429
  257. package/build/src/utils/obj.utils.js.map +0 -1
  258. package/build/src/utils/performance.utils.d.ts +0 -136
  259. package/build/src/utils/performance.utils.js +0 -278
  260. package/build/src/utils/performance.utils.js.map +0 -1
  261. package/build/src/utils/request.utils.d.ts +0 -86
  262. package/build/src/utils/request.utils.js +0 -195
  263. package/build/src/utils/request.utils.js.map +0 -1
  264. package/build/src/utils/response.utils.d.ts +0 -163
  265. package/build/src/utils/response.utils.js +0 -260
  266. package/build/src/utils/response.utils.js.map +0 -1
  267. package/build/src/utils/stream.utils.d.ts +0 -88
  268. package/build/src/utils/stream.utils.js +0 -218
  269. package/build/src/utils/stream.utils.js.map +0 -1
  270. package/build/src/utils/string.utils.d.ts +0 -93
  271. package/build/src/utils/string.utils.js +0 -179
  272. package/build/src/utils/string.utils.js.map +0 -1
  273. package/build/src/utils/type.utils.d.ts +0 -90
  274. package/build/src/utils/type.utils.js +0 -196
  275. package/build/src/utils/type.utils.js.map +0 -1
  276. package/build/src/utils/url.utils.d.ts +0 -141
  277. package/build/src/utils/url.utils.js +0 -317
  278. package/build/src/utils/url.utils.js.map +0 -1
  279. package/build/src/utils/validate.utils.d.ts +0 -177
  280. package/build/src/utils/validate.utils.js +0 -345
  281. package/build/src/utils/validate.utils.js.map +0 -1
@@ -21,89 +21,6 @@
21
21
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  * SOFTWARE.
23
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
24
  import os from 'os';
108
25
  import fs from 'fs';
109
26
  import fsp from 'fs/promises';
@@ -117,17 +34,8 @@ import { randomBytes } from 'crypto';
117
34
  * @returns {Promise<void>} Resolves when the directory exists.
118
35
  * @throws {Error} If directory cannot be created.
119
36
  */
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
- });
37
+ export async function ensureDir(dirPath) {
38
+ await fsp.mkdir(dirPath, { recursive: true });
131
39
  }
132
40
  /**
133
41
  * Recursively lists all files in a directory.
@@ -137,54 +45,20 @@ export function ensureDir(dirPath) {
137
45
  * @returns {Promise<string[]>} Array of absolute file paths.
138
46
  * @throws {Error} If the directory cannot be read.
139
47
  */
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
- });
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;
188
62
  }
189
63
  /**
190
64
  * Deletes a directory and all its contents recursively (like `rm -rf`).
@@ -193,17 +67,8 @@ export function listFiles(dirPath_1) {
193
67
  * @returns {Promise<void>} Resolves when deletion is complete.
194
68
  * @throws {Error} If deletion fails.
195
69
  */
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
- });
70
+ export async function deleteDirRecursive(dirPath) {
71
+ await fsp.rm(dirPath, { recursive: true, force: true });
207
72
  }
208
73
  /**
209
74
  * Checks whether a given path is a directory.
@@ -211,24 +76,14 @@ export function deleteDirRecursive(dirPath) {
211
76
  * @param {string} pathStr - Path to check.
212
77
  * @returns {Promise<boolean>} True if the path is a directory, else false.
213
78
  */
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
- });
79
+ export async function isDirectory(pathStr) {
80
+ try {
81
+ const stat = await fsp.stat(pathStr);
82
+ return stat.isDirectory();
83
+ }
84
+ catch {
85
+ return false;
86
+ }
232
87
  }
233
88
  /**
234
89
  * Recursively copies a directory and all its contents to a destination.
@@ -238,55 +93,19 @@ export function isDirectory(pathStr) {
238
93
  * @returns {Promise<void>} Resolves when copy is complete.
239
94
  * @throws {Error} If source does not exist or copy fails.
240
95
  */
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
- });
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
+ }
290
109
  }
291
110
  /**
292
111
  * Moves a directory to a new location by copying and deleting the original.
@@ -296,20 +115,9 @@ export function copyDir(src, dest) {
296
115
  * @returns {Promise<void>} Resolves when move is complete.
297
116
  * @throws {Error} If copy or deletion fails.
298
117
  */
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
- });
118
+ export async function moveDir(src, dest) {
119
+ await copyDir(src, dest);
120
+ await deleteDirRecursive(src);
313
121
  }
314
122
  /**
315
123
  * Empties a directory by deleting all files and subdirectories inside it.
@@ -318,51 +126,17 @@ export function moveDir(src, dest) {
318
126
  * @returns {Promise<void>} Resolves when the directory has been emptied.
319
127
  * @throws {Error} If files or subdirectories cannot be removed.
320
128
  */
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
- });
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
+ }
366
140
  }
367
141
  /**
368
142
  * Calculates the total size (in bytes) of all files in a directory (recursive).
@@ -371,55 +145,20 @@ export function emptyDir(dirPath) {
371
145
  * @returns {Promise<number>} Total size in bytes.
372
146
  * @throws {Error} If any file stats cannot be read.
373
147
  */
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
- });
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;
423
162
  }
424
163
  /**
425
164
  * Watches a directory for file changes and calls a callback on each event.
@@ -429,10 +168,10 @@ export function getDirSize(dirPath) {
429
168
  * @returns {() => void} A function to stop watching the directory.
430
169
  */
431
170
  export function watchDir(dirPath, callback) {
432
- var watcher = fs.watch(dirPath, function (eventType, filename) {
171
+ const watcher = fs.watch(dirPath, (eventType, filename) => {
433
172
  callback(eventType, filename);
434
173
  });
435
- return function () { return watcher.close(); };
174
+ return () => watcher.close();
436
175
  }
437
176
  /**
438
177
  * Finds files matching a glob pattern.
@@ -445,15 +184,9 @@ export function watchDir(dirPath, callback) {
445
184
  * @returns {Promise<string[]>} Array of matched file paths.
446
185
  * @throws {Error} If pattern matching fails.
447
186
  */
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
- });
187
+ export async function findFilesByPattern(pattern, options = {}) {
188
+ const defaultOptions = { nodir: true, ...options };
189
+ return glob(pattern, defaultOptions);
457
190
  }
458
191
  /**
459
192
  * Gets all subdirectories in a directory.
@@ -463,25 +196,14 @@ export function findFilesByPattern(pattern_1) {
463
196
  * @returns {Promise<string[]>} Array of absolute subdirectory paths.
464
197
  * @throws {Error} If directory cannot be read.
465
198
  */
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
- });
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;
485
207
  }
486
208
  /**
487
209
  * Ensures a directory exists and is empty.
@@ -490,39 +212,22 @@ export function getSubdirectories(dirPath_1) {
490
212
  * @returns {Promise<void>} Resolves when the directory exists and is empty.
491
213
  * @throws {Error} If directory cannot be created or emptied.
492
214
  */
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*/];
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;
523
227
  }
524
- });
525
- });
228
+ }
229
+ await ensureDir(dirPath);
230
+ }
526
231
  }
527
232
  /**
528
233
  * Creates a temporary directory with optional auto-cleanup.
@@ -534,58 +239,37 @@ export function ensureEmptyDir(dirPath) {
534
239
  * @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
535
240
  * @throws {Error} If directory cannot be created.
536
241
  */
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 }];
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 {
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 {
262
+ // Ignore cleanup errors on exit
586
263
  }
587
264
  });
588
- });
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 };
589
273
  }
590
274
  /**
591
275
  * Finds the newest file in a directory.
@@ -595,38 +279,15 @@ export function createTempDir() {
595
279
  * @returns {Promise<string | null>} Path to the newest file or null if no files.
596
280
  * @throws {Error} If directory cannot be read.
597
281
  */
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
- });
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;
630
291
  }
631
292
  /**
632
293
  * Finds the oldest file in a directory.
@@ -636,38 +297,15 @@ export function findNewestFile(dirPath_1) {
636
297
  * @returns {Promise<string | null>} Path to the oldest file or null if no files.
637
298
  * @throws {Error} If directory cannot be read.
638
299
  */
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
- });
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;
671
309
  }
672
310
  /**
673
311
  * Finds files or directories in a directory matching a predicate function.
@@ -678,58 +316,21 @@ export function findOldestFile(dirPath_1) {
678
316
  * @returns {Promise<string[]>} Array of matching paths.
679
317
  * @throws {Error} If directory cannot be read.
680
318
  */
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
- });
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;
733
334
  }
734
335
  /**
735
336
  * Watches a directory recursively for file changes.
@@ -740,83 +341,54 @@ export function findInDir(dirPath_1, predicate_1) {
740
341
  * @returns {Promise<() => void>} A function to stop watching the directory.
741
342
  * @throws {Error} If directory cannot be watched.
742
343
  */
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) {
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 () => {
755
362
  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);
363
+ if (await isDirectory(fullPath)) {
364
+ addWatcher(fullPath);
365
+ }
802
366
  }
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);
367
+ catch {
368
+ // Ignore errors checking newly created items
808
369
  }
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
- });
370
+ }, 100);
371
+ }
372
+ });
373
+ watchers.push(watcher);
374
+ }
375
+ catch {
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
+ };
820
392
  }
821
393
  /**
822
394
  * Gets detailed directory statistics including file count, directory count, and size.
@@ -825,73 +397,27 @@ export function watchDirRecursive(dirPath_1, callback_1) {
825
397
  * @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
826
398
  * @throws {Error} If directory cannot be read.
827
399
  */
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 }];
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);
892
411
  }
893
- });
894
- });
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 };
895
421
  }
896
422
  /**
897
423
  * Walks through a directory hierarchy, calling a visitor function for each entry.
@@ -903,96 +429,48 @@ export function getDirStats(dirPath) {
903
429
  * @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
904
430
  * @throws {Error} If directory cannot be read.
905
431
  */
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:
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 {
947
454
  // 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*/];
455
+ await walkDir(entryPath, options);
456
+ await options.visitorFn(entryInfo);
994
457
  }
995
- });
996
- });
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
+ }
997
476
  }
998
- //# sourceMappingURL=dir.utils.js.map