@catbee/utils 0.0.1 → 0.0.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.
- package/README.md +303 -107
- package/build/esm/config.d.ts +12 -0
- package/build/esm/config.js +12 -0
- package/build/esm/config.js.map +1 -1
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +1 -0
- package/build/esm/index.js.map +1 -1
- package/build/esm/types/api-response.d.ts +82 -0
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/utils/array.utils.d.ts +64 -1
- package/build/esm/utils/array.utils.js +139 -2
- package/build/esm/utils/array.utils.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +60 -1
- package/build/esm/utils/async.utils.js +160 -2
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/cache.utils.d.ts +67 -4
- package/build/esm/utils/cache.utils.js +210 -28
- package/build/esm/utils/cache.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +101 -0
- package/build/esm/utils/context-store.utils.js +182 -0
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/crypto.utils.d.ts +110 -8
- package/build/esm/utils/crypto.utils.js +298 -15
- package/build/esm/utils/crypto.utils.js.map +1 -1
- package/build/esm/utils/decorators.utils.d.ts +40 -0
- package/build/esm/utils/decorators.utils.js +312 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +121 -0
- package/build/esm/utils/dir.utils.js +577 -0
- package/build/esm/utils/dir.utils.js.map +1 -1
- package/build/esm/utils/env.utils.d.ts +83 -0
- package/build/esm/utils/env.utils.js +233 -0
- package/build/esm/utils/env.utils.js.map +1 -1
- package/build/esm/utils/exception.utils.d.ts +116 -0
- package/build/esm/utils/exception.utils.js +322 -0
- package/build/esm/utils/exception.utils.js.map +1 -1
- package/build/esm/utils/fs.utils.d.ts +132 -0
- package/build/esm/utils/fs.utils.js +421 -0
- package/build/esm/utils/fs.utils.js.map +1 -1
- package/build/esm/utils/http-status-codes.d.ts +87 -178
- package/build/esm/utils/http-status-codes.js +92 -178
- package/build/esm/utils/http-status-codes.js.map +1 -1
- package/build/esm/utils/id.utils.d.ts +11 -10
- package/build/esm/utils/id.utils.js +29 -20
- package/build/esm/utils/id.utils.js.map +1 -1
- package/build/esm/utils/logger.utils.d.ts +24 -0
- package/build/esm/utils/logger.utils.js +54 -0
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.d.ts +72 -0
- package/build/esm/utils/middleware.utils.js +158 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +65 -3
- package/build/esm/utils/obj.utils.js +205 -6
- package/build/esm/utils/obj.utils.js.map +1 -1
- package/build/esm/utils/response.utils.d.ts +101 -0
- package/build/esm/utils/response.utils.js +153 -0
- package/build/esm/utils/response.utils.js.map +1 -1
- package/build/esm/utils/string.utils.d.ts +61 -5
- package/build/esm/utils/string.utils.js +101 -11
- package/build/esm/utils/string.utils.js.map +1 -1
- package/build/esm/utils/url.utils.d.ts +117 -0
- package/build/esm/utils/url.utils.js +287 -0
- package/build/esm/utils/url.utils.js.map +1 -1
- package/build/esm/utils/validate.utils.d.ts +110 -0
- package/build/esm/utils/validate.utils.js +186 -0
- package/build/esm/utils/validate.utils.js.map +1 -1
- package/build/esnext/config.d.ts +12 -0
- package/build/esnext/config.js +12 -0
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/index.d.ts +1 -0
- package/build/esnext/index.js +1 -0
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/types/api-response.d.ts +82 -0
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/utils/array.utils.d.ts +64 -1
- package/build/esnext/utils/array.utils.js +129 -2
- package/build/esnext/utils/array.utils.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +60 -1
- package/build/esnext/utils/async.utils.js +117 -1
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/cache.utils.d.ts +67 -4
- package/build/esnext/utils/cache.utils.js +137 -9
- package/build/esnext/utils/cache.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +101 -0
- package/build/esnext/utils/context-store.utils.js +168 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/crypto.utils.d.ts +110 -8
- package/build/esnext/utils/crypto.utils.js +205 -15
- package/build/esnext/utils/crypto.utils.js.map +1 -1
- package/build/esnext/utils/decorators.utils.d.ts +40 -0
- package/build/esnext/utils/decorators.utils.js +155 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +121 -0
- package/build/esnext/utils/dir.utils.js +306 -0
- package/build/esnext/utils/dir.utils.js.map +1 -1
- package/build/esnext/utils/env.utils.d.ts +83 -0
- package/build/esnext/utils/env.utils.js +185 -0
- package/build/esnext/utils/env.utils.js.map +1 -1
- package/build/esnext/utils/exception.utils.d.ts +116 -0
- package/build/esnext/utils/exception.utils.js +213 -0
- package/build/esnext/utils/exception.utils.js.map +1 -1
- package/build/esnext/utils/fs.utils.d.ts +132 -0
- package/build/esnext/utils/fs.utils.js +268 -0
- package/build/esnext/utils/fs.utils.js.map +1 -1
- package/build/esnext/utils/http-status-codes.d.ts +87 -178
- package/build/esnext/utils/http-status-codes.js +92 -178
- package/build/esnext/utils/http-status-codes.js.map +1 -1
- package/build/esnext/utils/id.utils.d.ts +11 -10
- package/build/esnext/utils/id.utils.js +27 -19
- package/build/esnext/utils/id.utils.js.map +1 -1
- package/build/esnext/utils/logger.utils.d.ts +24 -0
- package/build/esnext/utils/logger.utils.js +42 -0
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.d.ts +72 -0
- package/build/esnext/utils/middleware.utils.js +157 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +65 -3
- package/build/esnext/utils/obj.utils.js +167 -3
- package/build/esnext/utils/obj.utils.js.map +1 -1
- package/build/esnext/utils/response.utils.d.ts +101 -0
- package/build/esnext/utils/response.utils.js +138 -0
- package/build/esnext/utils/response.utils.js.map +1 -1
- package/build/esnext/utils/string.utils.d.ts +61 -5
- package/build/esnext/utils/string.utils.js +110 -14
- package/build/esnext/utils/string.utils.js.map +1 -1
- package/build/esnext/utils/url.utils.d.ts +117 -0
- package/build/esnext/utils/url.utils.js +248 -0
- package/build/esnext/utils/url.utils.js.map +1 -1
- package/build/esnext/utils/validate.utils.d.ts +110 -0
- package/build/esnext/utils/validate.utils.js +184 -0
- package/build/esnext/utils/validate.utils.js.map +1 -1
- package/build/src/config.d.ts +12 -0
- package/build/src/config.js +12 -0
- package/build/src/config.js.map +1 -1
- package/build/src/index.d.ts +1 -0
- package/build/src/index.js +1 -0
- package/build/src/index.js.map +1 -1
- package/build/src/types/api-response.d.ts +82 -0
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/utils/array.utils.d.ts +64 -1
- package/build/src/utils/array.utils.js +137 -4
- package/build/src/utils/array.utils.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +60 -1
- package/build/src/utils/async.utils.js +124 -4
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/cache.utils.d.ts +67 -4
- package/build/src/utils/cache.utils.js +137 -9
- package/build/src/utils/cache.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +101 -0
- package/build/src/utils/context-store.utils.js +171 -1
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/crypto.utils.d.ts +110 -8
- package/build/src/utils/crypto.utils.js +224 -27
- package/build/src/utils/crypto.utils.js.map +1 -1
- package/build/src/utils/decorators.utils.d.ts +40 -0
- package/build/src/utils/decorators.utils.js +165 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +121 -0
- package/build/src/utils/dir.utils.js +316 -0
- package/build/src/utils/dir.utils.js.map +1 -1
- package/build/src/utils/env.utils.d.ts +83 -0
- package/build/src/utils/env.utils.js +186 -1
- package/build/src/utils/env.utils.js.map +1 -1
- package/build/src/utils/exception.utils.d.ts +116 -0
- package/build/src/utils/exception.utils.js +226 -1
- package/build/src/utils/exception.utils.js.map +1 -1
- package/build/src/utils/fs.utils.d.ts +132 -0
- package/build/src/utils/fs.utils.js +282 -0
- package/build/src/utils/fs.utils.js.map +1 -1
- package/build/src/utils/http-status-codes.d.ts +87 -178
- package/build/src/utils/http-status-codes.js +92 -178
- package/build/src/utils/http-status-codes.js.map +1 -1
- package/build/src/utils/id.utils.d.ts +11 -10
- package/build/src/utils/id.utils.js +27 -19
- package/build/src/utils/id.utils.js.map +1 -1
- package/build/src/utils/logger.utils.d.ts +24 -0
- package/build/src/utils/logger.utils.js +45 -0
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.d.ts +72 -0
- package/build/src/utils/middleware.utils.js +163 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +65 -3
- package/build/src/utils/obj.utils.js +177 -7
- package/build/src/utils/obj.utils.js.map +1 -1
- package/build/src/utils/response.utils.d.ts +101 -0
- package/build/src/utils/response.utils.js +146 -1
- package/build/src/utils/response.utils.js.map +1 -1
- package/build/src/utils/string.utils.d.ts +61 -5
- package/build/src/utils/string.utils.js +122 -20
- package/build/src/utils/string.utils.js.map +1 -1
- package/build/src/utils/url.utils.d.ts +117 -0
- package/build/src/utils/url.utils.js +257 -0
- package/build/src/utils/url.utils.js.map +1 -1
- package/build/src/utils/validate.utils.d.ts +110 -0
- package/build/src/utils/validate.utils.js +198 -0
- package/build/src/utils/validate.utils.js.map +1 -1
- package/package.json +20 -12
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Res = exports.Req = exports.Body = exports.Param = exports.Query = exports.Connect = exports.Trace = exports.Head = exports.Options = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = void 0;
|
|
5
|
+
exports.Controller = Controller;
|
|
6
|
+
exports.Use = Use;
|
|
7
|
+
exports.HttpCode = HttpCode;
|
|
8
|
+
exports.Header = Header;
|
|
9
|
+
exports.Before = Before;
|
|
10
|
+
exports.After = After;
|
|
11
|
+
exports.registerControllers = registerControllers;
|
|
12
|
+
require("reflect-metadata");
|
|
13
|
+
// --- End sample Express interfaces ---
|
|
14
|
+
const ROUTES_KEY = Symbol("routes");
|
|
15
|
+
const MIDDLEWARE_KEY = Symbol("middlewares");
|
|
16
|
+
const PARAMS_KEY = Symbol("params");
|
|
17
|
+
// ---- Route Decorators ----
|
|
18
|
+
function createRouteDecorator(method) {
|
|
19
|
+
return (path) => {
|
|
20
|
+
return (target, propertyKey, descriptor) => {
|
|
21
|
+
const routes = Reflect.getMetadata(ROUTES_KEY, target.constructor) || [];
|
|
22
|
+
routes.push({
|
|
23
|
+
path,
|
|
24
|
+
method,
|
|
25
|
+
handlerName: propertyKey,
|
|
26
|
+
});
|
|
27
|
+
Reflect.defineMetadata(ROUTES_KEY, routes, target.constructor);
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.Get = createRouteDecorator("get");
|
|
32
|
+
exports.Post = createRouteDecorator("post");
|
|
33
|
+
exports.Put = createRouteDecorator("put");
|
|
34
|
+
exports.Patch = createRouteDecorator("patch");
|
|
35
|
+
exports.Delete = createRouteDecorator("delete");
|
|
36
|
+
exports.Options = createRouteDecorator("options");
|
|
37
|
+
exports.Head = createRouteDecorator("head");
|
|
38
|
+
exports.Trace = createRouteDecorator("trace");
|
|
39
|
+
exports.Connect = createRouteDecorator("connect");
|
|
40
|
+
// ---- Controller Decorator ----
|
|
41
|
+
function Controller(basePath) {
|
|
42
|
+
return (target) => {
|
|
43
|
+
Reflect.defineMetadata("basePath", basePath, target);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// ---- Middleware Decorator ----
|
|
47
|
+
function Use(...middlewares) {
|
|
48
|
+
return (target, propertyKey, descriptor) => {
|
|
49
|
+
const existing = Reflect.getMetadata(MIDDLEWARE_KEY, target, propertyKey) || [];
|
|
50
|
+
Reflect.defineMetadata(MIDDLEWARE_KEY, [...existing, ...middlewares], target, propertyKey);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// ---- Parameter Decorators ----
|
|
54
|
+
function createParamDecorator(type, key) {
|
|
55
|
+
return (paramKey) => {
|
|
56
|
+
return (target, propertyKey, parameterIndex) => {
|
|
57
|
+
const params = Reflect.getMetadata(PARAMS_KEY, target, propertyKey) || [];
|
|
58
|
+
// Ensure parameters are ordered by index
|
|
59
|
+
params.push({ index: parameterIndex, type, key: paramKey || key });
|
|
60
|
+
params.sort((a, b) => a.index - b.index);
|
|
61
|
+
Reflect.defineMetadata(PARAMS_KEY, params, target, propertyKey);
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
exports.Query = createParamDecorator("query");
|
|
66
|
+
exports.Param = createParamDecorator("param");
|
|
67
|
+
exports.Body = createParamDecorator("body");
|
|
68
|
+
exports.Req = createParamDecorator("req");
|
|
69
|
+
exports.Res = createParamDecorator("res");
|
|
70
|
+
// Custom HTTP status code decorator
|
|
71
|
+
const HTTP_CODE_KEY = Symbol("httpCode");
|
|
72
|
+
function HttpCode(status) {
|
|
73
|
+
return (target, propertyKey, descriptor) => {
|
|
74
|
+
Reflect.defineMetadata(HTTP_CODE_KEY, status, target, propertyKey);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
// Custom header decorator
|
|
78
|
+
const HEADER_KEY = Symbol("headers");
|
|
79
|
+
function Header(name, value) {
|
|
80
|
+
return (target, propertyKey, descriptor) => {
|
|
81
|
+
const headers = Reflect.getMetadata(HEADER_KEY, target, propertyKey) || {};
|
|
82
|
+
headers[name] = value;
|
|
83
|
+
Reflect.defineMetadata(HEADER_KEY, headers, target, propertyKey);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// Before/After hooks (not Express middleware, but can be used for logging, etc.)
|
|
87
|
+
const BEFORE_KEY = Symbol("before");
|
|
88
|
+
const AFTER_KEY = Symbol("after");
|
|
89
|
+
function Before(fn) {
|
|
90
|
+
return (target, propertyKey, descriptor) => {
|
|
91
|
+
const hooks = Reflect.getMetadata(BEFORE_KEY, target, propertyKey) || [];
|
|
92
|
+
hooks.push(fn);
|
|
93
|
+
Reflect.defineMetadata(BEFORE_KEY, hooks, target, propertyKey);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function After(fn) {
|
|
97
|
+
return (target, propertyKey, descriptor) => {
|
|
98
|
+
const hooks = Reflect.getMetadata(AFTER_KEY, target, propertyKey) || [];
|
|
99
|
+
hooks.push(fn);
|
|
100
|
+
Reflect.defineMetadata(AFTER_KEY, hooks, target, propertyKey);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// ---- Register Controllers ----
|
|
104
|
+
function registerControllers(router, controllers) {
|
|
105
|
+
controllers.forEach((ControllerClass) => {
|
|
106
|
+
const instance = new ControllerClass();
|
|
107
|
+
const basePath = Reflect.getMetadata("basePath", ControllerClass) || "";
|
|
108
|
+
const routes = Reflect.getMetadata(ROUTES_KEY, ControllerClass) || [];
|
|
109
|
+
routes.forEach(({ path, method, handlerName }) => {
|
|
110
|
+
const middlewares = Reflect.getMetadata(MIDDLEWARE_KEY, instance, handlerName) || [];
|
|
111
|
+
const params = Reflect.getMetadata(PARAMS_KEY, instance, handlerName) || [];
|
|
112
|
+
const httpCode = Reflect.getMetadata(HTTP_CODE_KEY, instance, handlerName);
|
|
113
|
+
const headers = Reflect.getMetadata(HEADER_KEY, instance, handlerName) || {};
|
|
114
|
+
const beforeHooks = Reflect.getMetadata(BEFORE_KEY, instance, handlerName) || [];
|
|
115
|
+
const afterHooks = Reflect.getMetadata(AFTER_KEY, instance, handlerName) || [];
|
|
116
|
+
const handler = async (req, res, next) => {
|
|
117
|
+
var _a, _b;
|
|
118
|
+
try {
|
|
119
|
+
for (const fn of beforeHooks)
|
|
120
|
+
await fn(req, res);
|
|
121
|
+
const args = [];
|
|
122
|
+
if (params.length) {
|
|
123
|
+
params.forEach(({ index, type, key }) => {
|
|
124
|
+
var _a;
|
|
125
|
+
switch (type) {
|
|
126
|
+
case "query":
|
|
127
|
+
args[index] = key ? req.query[key] : req.query;
|
|
128
|
+
break;
|
|
129
|
+
case "param":
|
|
130
|
+
args[index] = key ? req.params[key] : req.params;
|
|
131
|
+
break;
|
|
132
|
+
case "body":
|
|
133
|
+
args[index] = key ? (_a = req.body) === null || _a === void 0 ? void 0 : _a[key] : req.body;
|
|
134
|
+
break;
|
|
135
|
+
case "req":
|
|
136
|
+
args[index] = req;
|
|
137
|
+
break;
|
|
138
|
+
case "res":
|
|
139
|
+
args[index] = res;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const result = instance[handlerName](...args);
|
|
145
|
+
// Support both sync and async handlers
|
|
146
|
+
const awaited = result instanceof Promise ? await result : result;
|
|
147
|
+
if (!res.headersSent && typeof awaited !== "undefined") {
|
|
148
|
+
if (httpCode)
|
|
149
|
+
(_a = res.status) === null || _a === void 0 ? void 0 : _a.call(res, httpCode);
|
|
150
|
+
for (const [k, v] of Object.entries(headers))
|
|
151
|
+
(_b = res.set) === null || _b === void 0 ? void 0 : _b.call(res, k, v);
|
|
152
|
+
res.json(awaited);
|
|
153
|
+
}
|
|
154
|
+
for (const fn of afterHooks)
|
|
155
|
+
await fn(req, res, awaited);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
next(err);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
router[method](basePath + path, ...middlewares, handler);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=decorators.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|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AED,MAAc,CAAC,MAAM,CAAC,CAAC,QAAQ,GAAG,IAAI,EAAE,GAAG,WAAW,EAAE,OAAO,CAAC,CAAC;QACpE,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\n\nimport \"reflect-metadata\";\n\n// --- Sample Express interfaces for type safety ---\nexport interface Request {\n query: any;\n params: any;\n body?: any;\n [key: string]: any;\n}\nexport interface Response {\n json: (body: any) => void;\n headersSent: boolean;\n [key: string]: any;\n}\nexport type NextFunction = (err?: any) => void;\nexport type RequestHandler = (\n req: Request,\n res: Response,\n next: NextFunction,\n) => any;\nexport interface Router {\n [method: string]: (path: string, ...handlers: RequestHandler[]) => void;\n}\n// --- End sample Express interfaces ---\n\nconst ROUTES_KEY = Symbol(\"routes\");\nconst MIDDLEWARE_KEY = Symbol(\"middlewares\");\nconst PARAMS_KEY = Symbol(\"params\");\n\nexport type HttpMethod =\n | \"get\"\n | \"post\"\n | \"put\"\n | \"patch\"\n | \"delete\"\n | \"options\"\n | \"head\"\n | \"trace\"\n | \"connect\";\n\ninterface RouteDefinition {\n path: string;\n method: HttpMethod;\n handlerName: string;\n}\n\ninterface ParamDefinition {\n index: number;\n type: \"query\" | \"param\" | \"body\" | \"req\" | \"res\";\n key?: string;\n}\n\n// ---- Route Decorators ----\nfunction createRouteDecorator(method: HttpMethod) {\n return (path: string): MethodDecorator => {\n return (target, propertyKey, descriptor) => {\n const routes: RouteDefinition[] =\n Reflect.getMetadata(ROUTES_KEY, target.constructor) || [];\n routes.push({\n path,\n method,\n handlerName: propertyKey as string,\n });\n Reflect.defineMetadata(ROUTES_KEY, routes, target.constructor);\n };\n };\n}\n\nexport const Get = createRouteDecorator(\"get\");\nexport const Post = createRouteDecorator(\"post\");\nexport const Put = createRouteDecorator(\"put\");\nexport const Patch = createRouteDecorator(\"patch\");\nexport const Delete = createRouteDecorator(\"delete\");\nexport const Options = createRouteDecorator(\"options\");\nexport const Head = createRouteDecorator(\"head\");\nexport const Trace = createRouteDecorator(\"trace\");\nexport const Connect = createRouteDecorator(\"connect\");\n\n// ---- Controller Decorator ----\nexport function Controller(basePath: string): ClassDecorator {\n return (target) => {\n Reflect.defineMetadata(\"basePath\", basePath, target);\n };\n}\n\n// ---- Middleware Decorator ----\nexport function Use(...middlewares: RequestHandler[]): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n const existing: RequestHandler[] =\n Reflect.getMetadata(MIDDLEWARE_KEY, target, propertyKey as string) || [];\n Reflect.defineMetadata(\n MIDDLEWARE_KEY,\n [...existing, ...middlewares],\n target,\n propertyKey as string,\n );\n };\n}\n\n// ---- Parameter Decorators ----\nfunction createParamDecorator(type: ParamDefinition[\"type\"], key?: string) {\n return (paramKey?: string): ParameterDecorator => {\n return (target, propertyKey, parameterIndex) => {\n const params: ParamDefinition[] =\n Reflect.getMetadata(PARAMS_KEY, target, propertyKey as string) || [];\n // Ensure parameters are ordered by index\n params.push({ index: parameterIndex, type, key: paramKey || key });\n params.sort((a, b) => a.index - b.index);\n Reflect.defineMetadata(PARAMS_KEY, params, target, propertyKey as string);\n };\n };\n}\n\nexport const Query = createParamDecorator(\"query\");\nexport const Param = createParamDecorator(\"param\");\nexport const Body = createParamDecorator(\"body\");\nexport const Req = createParamDecorator(\"req\");\nexport const Res = createParamDecorator(\"res\");\n\n// Custom HTTP status code decorator\nconst HTTP_CODE_KEY = Symbol(\"httpCode\");\nexport function HttpCode(status: number): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n Reflect.defineMetadata(\n HTTP_CODE_KEY,\n status,\n target,\n propertyKey as string,\n );\n };\n}\n\n// Custom header decorator\nconst HEADER_KEY = Symbol(\"headers\");\nexport function Header(name: string, value: string): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n const headers: Record<string, string> =\n Reflect.getMetadata(HEADER_KEY, target, propertyKey as string) || {};\n headers[name] = value;\n Reflect.defineMetadata(HEADER_KEY, headers, target, propertyKey as string);\n };\n}\n\n// Before/After hooks (not Express middleware, but can be used for logging, etc.)\nconst BEFORE_KEY = Symbol(\"before\");\nconst AFTER_KEY = Symbol(\"after\");\nexport function Before(fn: Function): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n const hooks: Function[] =\n Reflect.getMetadata(BEFORE_KEY, target, propertyKey as string) || [];\n hooks.push(fn);\n Reflect.defineMetadata(BEFORE_KEY, hooks, target, propertyKey as string);\n };\n}\nexport function After(fn: Function): MethodDecorator {\n return (target, propertyKey, descriptor) => {\n const hooks: Function[] =\n Reflect.getMetadata(AFTER_KEY, target, propertyKey as string) || [];\n hooks.push(fn);\n Reflect.defineMetadata(AFTER_KEY, hooks, target, propertyKey as string);\n };\n}\n\n// ---- Register Controllers ----\nexport function registerControllers(router: Router, controllers: any[]) {\n controllers.forEach((ControllerClass) => {\n const instance = new ControllerClass();\n const basePath: string =\n Reflect.getMetadata(\"basePath\", ControllerClass) || \"\";\n const routes: RouteDefinition[] =\n Reflect.getMetadata(ROUTES_KEY, ControllerClass) || [];\n\n routes.forEach(({ path, method, handlerName }) => {\n const middlewares: RequestHandler[] =\n Reflect.getMetadata(MIDDLEWARE_KEY, instance, handlerName) || [];\n const params: ParamDefinition[] =\n Reflect.getMetadata(PARAMS_KEY, instance, handlerName) || [];\n\n const httpCode: number | undefined = Reflect.getMetadata(\n HTTP_CODE_KEY,\n instance,\n handlerName,\n );\n const headers: Record<string, string> =\n Reflect.getMetadata(HEADER_KEY, instance, handlerName) || {};\n const beforeHooks: Function[] =\n Reflect.getMetadata(BEFORE_KEY, instance, handlerName) || [];\n const afterHooks: Function[] =\n Reflect.getMetadata(AFTER_KEY, instance, handlerName) || [];\n\n const handler: RequestHandler = async (req, res, next) => {\n try {\n for (const fn of beforeHooks) await fn(req, res);\n const args: any[] = [];\n if (params.length) {\n params.forEach(({ index, type, key }) => {\n switch (type) {\n case \"query\":\n args[index] = key ? req.query[key] : req.query;\n break;\n case \"param\":\n args[index] = key ? req.params[key] : req.params;\n break;\n case \"body\":\n args[index] = key ? req.body?.[key] : req.body;\n break;\n case \"req\":\n args[index] = req;\n break;\n case \"res\":\n args[index] = res;\n break;\n }\n });\n }\n const result = (instance as any)[handlerName](...args);\n // Support both sync and async handlers\n const awaited = result instanceof Promise ? await result : result;\n if (!res.headersSent && typeof awaited !== \"undefined\") {\n if (httpCode) res.status?.(httpCode);\n for (const [k, v] of Object.entries(headers)) res.set?.(k, v);\n res.json(awaited);\n }\n for (const fn of afterHooks) await fn(req, res, awaited);\n } catch (err) {\n next(err);\n }\n };\n\n (router as any)[method](basePath + path, ...middlewares, handler);\n });\n });\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from "fs";
|
|
1
2
|
/**
|
|
2
3
|
* Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
|
|
3
4
|
*
|
|
@@ -72,4 +73,124 @@ export declare function getDirSize(dirPath: string): Promise<number>;
|
|
|
72
73
|
* @returns {() => void} A function to stop watching the directory.
|
|
73
74
|
*/
|
|
74
75
|
export declare function watchDir(dirPath: string, callback: (eventType: "rename" | "change", filename: string | null) => void): () => void;
|
|
76
|
+
/**
|
|
77
|
+
* Finds files matching a glob pattern.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} pattern - Glob pattern to match files.
|
|
80
|
+
* @param {object} [options] - Options for glob pattern matching.
|
|
81
|
+
* @param {string} [options.cwd] - Current working directory for relative patterns.
|
|
82
|
+
* @param {boolean} [options.dot=false] - Include dotfiles in matches.
|
|
83
|
+
* @param {boolean} [options.nodir=true] - Only match files, not directories.
|
|
84
|
+
* @returns {Promise<string[]>} Array of matched file paths.
|
|
85
|
+
* @throws {Error} If pattern matching fails.
|
|
86
|
+
*/
|
|
87
|
+
export declare function findFilesByPattern(pattern: string, options?: {
|
|
88
|
+
cwd?: string;
|
|
89
|
+
dot?: boolean;
|
|
90
|
+
nodir?: boolean;
|
|
91
|
+
}): Promise<string[]>;
|
|
92
|
+
/**
|
|
93
|
+
* Gets all subdirectories in a directory.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} dirPath - The directory to search in.
|
|
96
|
+
* @param {boolean} [recursive=false] - Whether to include subdirectories recursively.
|
|
97
|
+
* @returns {Promise<string[]>} Array of absolute subdirectory paths.
|
|
98
|
+
* @throws {Error} If directory cannot be read.
|
|
99
|
+
*/
|
|
100
|
+
export declare function getSubdirectories(dirPath: string, recursive?: boolean): Promise<string[]>;
|
|
101
|
+
/**
|
|
102
|
+
* Ensures a directory exists and is empty.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} dirPath - Path to the directory.
|
|
105
|
+
* @returns {Promise<void>} Resolves when the directory exists and is empty.
|
|
106
|
+
* @throws {Error} If directory cannot be created or emptied.
|
|
107
|
+
*/
|
|
108
|
+
export declare function ensureEmptyDir(dirPath: string): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Creates a temporary directory with optional auto-cleanup.
|
|
111
|
+
*
|
|
112
|
+
* @param {object} [options] - Options for the temporary directory.
|
|
113
|
+
* @param {string} [options.prefix='tmp-'] - Prefix for the directory name.
|
|
114
|
+
* @param {string} [options.parentDir=os.tmpdir()] - Parent directory.
|
|
115
|
+
* @param {boolean} [options.cleanup=false] - Whether to register cleanup on process exit.
|
|
116
|
+
* @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
|
|
117
|
+
* @throws {Error} If directory cannot be created.
|
|
118
|
+
*/
|
|
119
|
+
export declare function createTempDir(options?: {
|
|
120
|
+
prefix?: string;
|
|
121
|
+
parentDir?: string;
|
|
122
|
+
cleanup?: boolean;
|
|
123
|
+
}): Promise<{
|
|
124
|
+
path: string;
|
|
125
|
+
cleanup: () => Promise<void>;
|
|
126
|
+
}>;
|
|
127
|
+
/**
|
|
128
|
+
* Finds the newest file in a directory.
|
|
129
|
+
*
|
|
130
|
+
* @param {string} dirPath - Directory to search.
|
|
131
|
+
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
132
|
+
* @returns {Promise<string | null>} Path to the newest file or null if no files.
|
|
133
|
+
* @throws {Error} If directory cannot be read.
|
|
134
|
+
*/
|
|
135
|
+
export declare function findNewestFile(dirPath: string, recursive?: boolean): Promise<string | null>;
|
|
136
|
+
/**
|
|
137
|
+
* Finds the oldest file in a directory.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} dirPath - Directory to search.
|
|
140
|
+
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
141
|
+
* @returns {Promise<string | null>} Path to the oldest file or null if no files.
|
|
142
|
+
* @throws {Error} If directory cannot be read.
|
|
143
|
+
*/
|
|
144
|
+
export declare function findOldestFile(dirPath: string, recursive?: boolean): Promise<string | null>;
|
|
145
|
+
/**
|
|
146
|
+
* Finds files or directories in a directory matching a predicate function.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} dirPath - Directory to search.
|
|
149
|
+
* @param {(path: string, stat: fs.Stats) => boolean | Promise<boolean>} predicate - Function to test each path.
|
|
150
|
+
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
151
|
+
* @returns {Promise<string[]>} Array of matching paths.
|
|
152
|
+
* @throws {Error} If directory cannot be read.
|
|
153
|
+
*/
|
|
154
|
+
export declare function findInDir(dirPath: string, predicate: (path: string, stat: fs.Stats) => boolean | Promise<boolean>, recursive?: boolean): Promise<string[]>;
|
|
155
|
+
/**
|
|
156
|
+
* Watches a directory recursively for file changes.
|
|
157
|
+
*
|
|
158
|
+
* @param {string} dirPath - Base directory path to watch.
|
|
159
|
+
* @param {(eventType: "rename" | "change", filename: string) => void} callback - Callback for each change event.
|
|
160
|
+
* @param {boolean} [includeSubdirs=true] - Whether to watch subdirectories.
|
|
161
|
+
* @returns {Promise<() => void>} A function to stop watching the directory.
|
|
162
|
+
* @throws {Error} If directory cannot be watched.
|
|
163
|
+
*/
|
|
164
|
+
export declare function watchDirRecursive(dirPath: string, callback: (eventType: "rename" | "change", filename: string) => void, includeSubdirs?: boolean): Promise<() => void>;
|
|
165
|
+
/**
|
|
166
|
+
* Gets detailed directory statistics including file count, directory count, and size.
|
|
167
|
+
*
|
|
168
|
+
* @param {string} dirPath - Path to the directory.
|
|
169
|
+
* @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
|
|
170
|
+
* @throws {Error} If directory cannot be read.
|
|
171
|
+
*/
|
|
172
|
+
export declare function getDirStats(dirPath: string): Promise<{
|
|
173
|
+
fileCount: number;
|
|
174
|
+
dirCount: number;
|
|
175
|
+
totalSize: number;
|
|
176
|
+
}>;
|
|
177
|
+
/**
|
|
178
|
+
* Walks through a directory hierarchy, calling a visitor function for each entry.
|
|
179
|
+
*
|
|
180
|
+
* @param {string} dirPath - Starting directory path.
|
|
181
|
+
* @param {object} options - Options for walking the directory.
|
|
182
|
+
* @param {(entry: { path: string, name: string, isDirectory: boolean, stats: fs.Stats }) => boolean | void | Promise<boolean | void>} options.visitorFn -
|
|
183
|
+
* Function called for each file/directory. Return false to skip a directory.
|
|
184
|
+
* @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
|
|
185
|
+
* @throws {Error} If directory cannot be read.
|
|
186
|
+
*/
|
|
187
|
+
export declare function walkDir(dirPath: string, options: {
|
|
188
|
+
visitorFn: (entry: {
|
|
189
|
+
path: string;
|
|
190
|
+
name: string;
|
|
191
|
+
isDirectory: boolean;
|
|
192
|
+
stats: fs.Stats;
|
|
193
|
+
}) => boolean | void | Promise<boolean | void>;
|
|
194
|
+
traversalOrder?: "pre" | "post";
|
|
195
|
+
}): Promise<void>;
|
|
75
196
|
//# sourceMappingURL=dir.utils.d.ts.map
|
|
@@ -12,9 +12,22 @@ exports.moveDir = moveDir;
|
|
|
12
12
|
exports.emptyDir = emptyDir;
|
|
13
13
|
exports.getDirSize = getDirSize;
|
|
14
14
|
exports.watchDir = watchDir;
|
|
15
|
+
exports.findFilesByPattern = findFilesByPattern;
|
|
16
|
+
exports.getSubdirectories = getSubdirectories;
|
|
17
|
+
exports.ensureEmptyDir = ensureEmptyDir;
|
|
18
|
+
exports.createTempDir = createTempDir;
|
|
19
|
+
exports.findNewestFile = findNewestFile;
|
|
20
|
+
exports.findOldestFile = findOldestFile;
|
|
21
|
+
exports.findInDir = findInDir;
|
|
22
|
+
exports.watchDirRecursive = watchDirRecursive;
|
|
23
|
+
exports.getDirStats = getDirStats;
|
|
24
|
+
exports.walkDir = walkDir;
|
|
25
|
+
const os_1 = __importDefault(require("os"));
|
|
15
26
|
const fs_1 = __importDefault(require("fs"));
|
|
16
27
|
const promises_1 = __importDefault(require("fs/promises"));
|
|
17
28
|
const path_1 = __importDefault(require("path"));
|
|
29
|
+
const glob_1 = require("glob");
|
|
30
|
+
const crypto_1 = require("crypto");
|
|
18
31
|
/**
|
|
19
32
|
* Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
|
|
20
33
|
*
|
|
@@ -161,4 +174,307 @@ function watchDir(dirPath, callback) {
|
|
|
161
174
|
});
|
|
162
175
|
return () => watcher.close();
|
|
163
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Finds files matching a glob pattern.
|
|
179
|
+
*
|
|
180
|
+
* @param {string} pattern - Glob pattern to match files.
|
|
181
|
+
* @param {object} [options] - Options for glob pattern matching.
|
|
182
|
+
* @param {string} [options.cwd] - Current working directory for relative patterns.
|
|
183
|
+
* @param {boolean} [options.dot=false] - Include dotfiles in matches.
|
|
184
|
+
* @param {boolean} [options.nodir=true] - Only match files, not directories.
|
|
185
|
+
* @returns {Promise<string[]>} Array of matched file paths.
|
|
186
|
+
* @throws {Error} If pattern matching fails.
|
|
187
|
+
*/
|
|
188
|
+
async function findFilesByPattern(pattern, options = {}) {
|
|
189
|
+
const defaultOptions = Object.assign({ nodir: true }, options);
|
|
190
|
+
return (0, glob_1.glob)(pattern, defaultOptions);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Gets all subdirectories in a directory.
|
|
194
|
+
*
|
|
195
|
+
* @param {string} dirPath - The directory to search in.
|
|
196
|
+
* @param {boolean} [recursive=false] - Whether to include subdirectories recursively.
|
|
197
|
+
* @returns {Promise<string[]>} Array of absolute subdirectory paths.
|
|
198
|
+
* @throws {Error} If directory cannot be read.
|
|
199
|
+
*/
|
|
200
|
+
async function getSubdirectories(dirPath, recursive = false) {
|
|
201
|
+
const entries = await promises_1.default.readdir(dirPath, { withFileTypes: true });
|
|
202
|
+
const dirs = entries
|
|
203
|
+
.filter((entry) => entry.isDirectory())
|
|
204
|
+
.map((entry) => path_1.default.join(dirPath, entry.name));
|
|
205
|
+
if (recursive && dirs.length > 0) {
|
|
206
|
+
const nestedDirs = await Promise.all(dirs.map((dir) => getSubdirectories(dir, true)));
|
|
207
|
+
return [...dirs, ...nestedDirs.flat()];
|
|
208
|
+
}
|
|
209
|
+
return dirs;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Ensures a directory exists and is empty.
|
|
213
|
+
*
|
|
214
|
+
* @param {string} dirPath - Path to the directory.
|
|
215
|
+
* @returns {Promise<void>} Resolves when the directory exists and is empty.
|
|
216
|
+
* @throws {Error} If directory cannot be created or emptied.
|
|
217
|
+
*/
|
|
218
|
+
async function ensureEmptyDir(dirPath) {
|
|
219
|
+
if (await isDirectory(dirPath)) {
|
|
220
|
+
await emptyDir(dirPath);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
try {
|
|
224
|
+
await promises_1.default.unlink(dirPath);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
// Ignore if file doesn't exist
|
|
228
|
+
if (error.code !== "ENOENT") {
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
await ensureDir(dirPath);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Creates a temporary directory with optional auto-cleanup.
|
|
237
|
+
*
|
|
238
|
+
* @param {object} [options] - Options for the temporary directory.
|
|
239
|
+
* @param {string} [options.prefix='tmp-'] - Prefix for the directory name.
|
|
240
|
+
* @param {string} [options.parentDir=os.tmpdir()] - Parent directory.
|
|
241
|
+
* @param {boolean} [options.cleanup=false] - Whether to register cleanup on process exit.
|
|
242
|
+
* @returns {Promise<{ path: string, cleanup: () => Promise<void> }>} Object with directory path and cleanup function.
|
|
243
|
+
* @throws {Error} If directory cannot be created.
|
|
244
|
+
*/
|
|
245
|
+
async function createTempDir(options = {}) {
|
|
246
|
+
const prefix = options.prefix || "tmp-";
|
|
247
|
+
const parentDir = options.parentDir || os_1.default.tmpdir();
|
|
248
|
+
const dirName = `${prefix}${(0, crypto_1.randomBytes)(6).toString("hex")}`;
|
|
249
|
+
const tempDirPath = path_1.default.join(parentDir, dirName);
|
|
250
|
+
await ensureDir(tempDirPath);
|
|
251
|
+
const cleanup = async () => {
|
|
252
|
+
try {
|
|
253
|
+
await deleteDirRecursive(tempDirPath);
|
|
254
|
+
}
|
|
255
|
+
catch (_a) {
|
|
256
|
+
// Ignore cleanup errors
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
if (options.cleanup) {
|
|
260
|
+
process.once("exit", () => {
|
|
261
|
+
try {
|
|
262
|
+
fs_1.default.rmSync(tempDirPath, { recursive: true, force: true });
|
|
263
|
+
}
|
|
264
|
+
catch (_a) {
|
|
265
|
+
// Ignore cleanup errors on exit
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
// Handle signals for better cleanup
|
|
269
|
+
["SIGINT", "SIGTERM", "SIGUSR1", "SIGUSR2"].forEach((signal) => {
|
|
270
|
+
process.once(signal, () => {
|
|
271
|
+
cleanup().then(() => process.exit());
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return { path: tempDirPath, cleanup };
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Finds the newest file in a directory.
|
|
279
|
+
*
|
|
280
|
+
* @param {string} dirPath - Directory to search.
|
|
281
|
+
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
282
|
+
* @returns {Promise<string | null>} Path to the newest file or null if no files.
|
|
283
|
+
* @throws {Error} If directory cannot be read.
|
|
284
|
+
*/
|
|
285
|
+
async function findNewestFile(dirPath, recursive = false) {
|
|
286
|
+
const files = await listFiles(dirPath, recursive);
|
|
287
|
+
if (files.length === 0)
|
|
288
|
+
return null;
|
|
289
|
+
const stats = await Promise.all(files.map(async (file) => ({
|
|
290
|
+
path: file,
|
|
291
|
+
mtime: (await promises_1.default.stat(file)).mtime,
|
|
292
|
+
})));
|
|
293
|
+
return stats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime())[0].path;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Finds the oldest file in a directory.
|
|
297
|
+
*
|
|
298
|
+
* @param {string} dirPath - Directory to search.
|
|
299
|
+
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
300
|
+
* @returns {Promise<string | null>} Path to the oldest file or null if no files.
|
|
301
|
+
* @throws {Error} If directory cannot be read.
|
|
302
|
+
*/
|
|
303
|
+
async function findOldestFile(dirPath, recursive = false) {
|
|
304
|
+
const files = await listFiles(dirPath, recursive);
|
|
305
|
+
if (files.length === 0)
|
|
306
|
+
return null;
|
|
307
|
+
const stats = await Promise.all(files.map(async (file) => ({
|
|
308
|
+
path: file,
|
|
309
|
+
mtime: (await promises_1.default.stat(file)).mtime,
|
|
310
|
+
})));
|
|
311
|
+
return stats.sort((a, b) => a.mtime.getTime() - b.mtime.getTime())[0].path;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Finds files or directories in a directory matching a predicate function.
|
|
315
|
+
*
|
|
316
|
+
* @param {string} dirPath - Directory to search.
|
|
317
|
+
* @param {(path: string, stat: fs.Stats) => boolean | Promise<boolean>} predicate - Function to test each path.
|
|
318
|
+
* @param {boolean} [recursive=false] - Whether to search subdirectories.
|
|
319
|
+
* @returns {Promise<string[]>} Array of matching paths.
|
|
320
|
+
* @throws {Error} If directory cannot be read.
|
|
321
|
+
*/
|
|
322
|
+
async function findInDir(dirPath, predicate, recursive = false) {
|
|
323
|
+
const entries = await promises_1.default.readdir(dirPath, { withFileTypes: true });
|
|
324
|
+
const results = [];
|
|
325
|
+
for (const entry of entries) {
|
|
326
|
+
const entryPath = path_1.default.join(dirPath, entry.name);
|
|
327
|
+
const stat = await promises_1.default.stat(entryPath);
|
|
328
|
+
if (await predicate(entryPath, stat)) {
|
|
329
|
+
results.push(entryPath);
|
|
330
|
+
}
|
|
331
|
+
if (recursive && entry.isDirectory()) {
|
|
332
|
+
const nestedResults = await findInDir(entryPath, predicate, true);
|
|
333
|
+
results.push(...nestedResults);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return results;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Watches a directory recursively for file changes.
|
|
340
|
+
*
|
|
341
|
+
* @param {string} dirPath - Base directory path to watch.
|
|
342
|
+
* @param {(eventType: "rename" | "change", filename: string) => void} callback - Callback for each change event.
|
|
343
|
+
* @param {boolean} [includeSubdirs=true] - Whether to watch subdirectories.
|
|
344
|
+
* @returns {Promise<() => void>} A function to stop watching the directory.
|
|
345
|
+
* @throws {Error} If directory cannot be watched.
|
|
346
|
+
*/
|
|
347
|
+
async function watchDirRecursive(dirPath, callback, includeSubdirs = true) {
|
|
348
|
+
// Normalize the path to ensure consistent path separators
|
|
349
|
+
const normalizedBasePath = path_1.default.normalize(dirPath);
|
|
350
|
+
// Create watchers for the base dir and all subdirectories
|
|
351
|
+
const watchers = [];
|
|
352
|
+
// Helper function to add a watcher for a directory
|
|
353
|
+
const addWatcher = (dir) => {
|
|
354
|
+
try {
|
|
355
|
+
const watcher = fs_1.default.watch(dir, (eventType, filename) => {
|
|
356
|
+
if (!filename)
|
|
357
|
+
return;
|
|
358
|
+
const fullPath = path_1.default.join(dir, filename);
|
|
359
|
+
// Make the path relative to the base directory
|
|
360
|
+
const relativePath = path_1.default.relative(normalizedBasePath, fullPath);
|
|
361
|
+
callback(eventType, relativePath);
|
|
362
|
+
// If a new directory is created, we should watch it
|
|
363
|
+
if (eventType === "rename" && includeSubdirs) {
|
|
364
|
+
setTimeout(async () => {
|
|
365
|
+
try {
|
|
366
|
+
if (await isDirectory(fullPath)) {
|
|
367
|
+
addWatcher(fullPath);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
catch (_a) {
|
|
371
|
+
// Ignore errors checking newly created items
|
|
372
|
+
}
|
|
373
|
+
}, 100);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
watchers.push(watcher);
|
|
377
|
+
}
|
|
378
|
+
catch (_a) {
|
|
379
|
+
// Silently ignore directories we can't watch
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
// Add watcher for the base directory
|
|
383
|
+
addWatcher(normalizedBasePath);
|
|
384
|
+
// If watching subdirectories, add watchers for all existing subdirectories
|
|
385
|
+
if (includeSubdirs) {
|
|
386
|
+
const subdirs = await getSubdirectories(normalizedBasePath, true);
|
|
387
|
+
for (const subdir of subdirs) {
|
|
388
|
+
addWatcher(subdir);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
// Return a function to close all watchers
|
|
392
|
+
return () => {
|
|
393
|
+
watchers.forEach((watcher) => watcher.close());
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Gets detailed directory statistics including file count, directory count, and size.
|
|
398
|
+
*
|
|
399
|
+
* @param {string} dirPath - Path to the directory.
|
|
400
|
+
* @returns {Promise<{ fileCount: number, dirCount: number, totalSize: number }>} Directory statistics.
|
|
401
|
+
* @throws {Error} If directory cannot be read.
|
|
402
|
+
*/
|
|
403
|
+
async function getDirStats(dirPath) {
|
|
404
|
+
let fileCount = 0;
|
|
405
|
+
let dirCount = 0;
|
|
406
|
+
let totalSize = 0;
|
|
407
|
+
async function processDir(currentPath) {
|
|
408
|
+
const entries = await promises_1.default.readdir(currentPath, { withFileTypes: true });
|
|
409
|
+
for (const entry of entries) {
|
|
410
|
+
const entryPath = path_1.default.join(currentPath, entry.name);
|
|
411
|
+
if (entry.isDirectory()) {
|
|
412
|
+
dirCount++;
|
|
413
|
+
await processDir(entryPath);
|
|
414
|
+
}
|
|
415
|
+
else if (entry.isFile()) {
|
|
416
|
+
fileCount++;
|
|
417
|
+
const stat = await promises_1.default.stat(entryPath);
|
|
418
|
+
totalSize += stat.size;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
await processDir(dirPath);
|
|
423
|
+
return { fileCount, dirCount, totalSize };
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Walks through a directory hierarchy, calling a visitor function for each entry.
|
|
427
|
+
*
|
|
428
|
+
* @param {string} dirPath - Starting directory path.
|
|
429
|
+
* @param {object} options - Options for walking the directory.
|
|
430
|
+
* @param {(entry: { path: string, name: string, isDirectory: boolean, stats: fs.Stats }) => boolean | void | Promise<boolean | void>} options.visitorFn -
|
|
431
|
+
* Function called for each file/directory. Return false to skip a directory.
|
|
432
|
+
* @param {'pre' | 'post'} [options.traversalOrder='pre'] - Whether to visit directories before or after their contents.
|
|
433
|
+
* @throws {Error} If directory cannot be read.
|
|
434
|
+
*/
|
|
435
|
+
async function walkDir(dirPath, options) {
|
|
436
|
+
const traversalOrder = options.traversalOrder || "pre";
|
|
437
|
+
const entries = await promises_1.default.readdir(dirPath, { withFileTypes: true });
|
|
438
|
+
for (const entry of entries) {
|
|
439
|
+
const entryPath = path_1.default.join(dirPath, entry.name);
|
|
440
|
+
const stats = await promises_1.default.stat(entryPath);
|
|
441
|
+
const entryInfo = {
|
|
442
|
+
path: entryPath,
|
|
443
|
+
name: entry.name,
|
|
444
|
+
isDirectory: entry.isDirectory(),
|
|
445
|
+
stats,
|
|
446
|
+
};
|
|
447
|
+
if (entry.isDirectory()) {
|
|
448
|
+
// Pre-order: visit directory before its contents
|
|
449
|
+
if (traversalOrder === "pre") {
|
|
450
|
+
const shouldContinue = await options.visitorFn(entryInfo);
|
|
451
|
+
// If the visitor returns false, don't recurse into this directory
|
|
452
|
+
if (shouldContinue !== false) {
|
|
453
|
+
await walkDir(entryPath, options);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
// Post-order: visit directory after its contents
|
|
458
|
+
await walkDir(entryPath, options);
|
|
459
|
+
await options.visitorFn(entryInfo);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
// Files are always visited when encountered
|
|
464
|
+
await options.visitorFn(entryInfo);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (traversalOrder === "post") {
|
|
468
|
+
// Only visit the root if this is the top-level call (not for subdirectories)
|
|
469
|
+
// But since this function is always called recursively, always visit after children
|
|
470
|
+
const stats = await promises_1.default.stat(dirPath);
|
|
471
|
+
const entryInfo = {
|
|
472
|
+
path: dirPath,
|
|
473
|
+
name: path_1.default.basename(dirPath),
|
|
474
|
+
isDirectory: true,
|
|
475
|
+
stats,
|
|
476
|
+
};
|
|
477
|
+
await options.visitorFn(entryInfo);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
164
480
|
//# sourceMappingURL=dir.utils.js.map
|