@catbee/utils 0.0.1
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/LICENSE +201 -0
- package/README.md +295 -0
- package/build/esm/config.d.ts +22 -0
- package/build/esm/config.js +21 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +19 -0
- package/build/esm/index.js +19 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/types/api-response.d.ts +44 -0
- package/build/esm/types/api-response.js +2 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +105 -0
- package/build/esm/utils/array.utils.js +232 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +119 -0
- package/build/esm/utils/async.utils.js +474 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +90 -0
- package/build/esm/utils/cache.utils.js +294 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +85 -0
- package/build/esm/utils/context-store.utils.js +99 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +58 -0
- package/build/esm/utils/crypto.utils.js +76 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +75 -0
- package/build/esm/utils/dir.utils.js +400 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +131 -0
- package/build/esm/utils/env.utils.js +218 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +114 -0
- package/build/esm/utils/exception.utils.js +217 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +32 -0
- package/build/esm/utils/fs.utils.js +140 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +357 -0
- package/build/esm/utils/http-status-codes.js +358 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +35 -0
- package/build/esm/utils/id.utils.js +55 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +15 -0
- package/build/esm/utils/logger.utils.js +59 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +55 -0
- package/build/esm/utils/obj.utils.js +121 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +48 -0
- package/build/esm/utils/response.utils.js +80 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +37 -0
- package/build/esm/utils/string.utils.js +56 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +24 -0
- package/build/esm/utils/url.utils.js +73 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +60 -0
- package/build/esm/utils/validate.utils.js +121 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +22 -0
- package/build/esnext/config.js +21 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +19 -0
- package/build/esnext/index.js +19 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +44 -0
- package/build/esnext/types/api-response.js +2 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +105 -0
- package/build/esnext/utils/array.utils.js +203 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +119 -0
- package/build/esnext/utils/async.utils.js +291 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +90 -0
- package/build/esnext/utils/cache.utils.js +142 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +85 -0
- package/build/esnext/utils/context-store.utils.js +95 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +58 -0
- package/build/esnext/utils/crypto.utils.js +72 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +75 -0
- package/build/esnext/utils/dir.utils.js +150 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +131 -0
- package/build/esnext/utils/env.utils.js +211 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +114 -0
- package/build/esnext/utils/exception.utils.js +148 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +32 -0
- package/build/esnext/utils/fs.utils.js +62 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +357 -0
- package/build/esnext/utils/http-status-codes.js +358 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +35 -0
- package/build/esnext/utils/id.utils.js +53 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +15 -0
- package/build/esnext/utils/logger.utils.js +59 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +55 -0
- package/build/esnext/utils/obj.utils.js +94 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +48 -0
- package/build/esnext/utils/response.utils.js +58 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +37 -0
- package/build/esnext/utils/string.utils.js +46 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +24 -0
- package/build/esnext/utils/url.utils.js +35 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +60 -0
- package/build/esnext/utils/validate.utils.js +105 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +22 -0
- package/build/src/config.js +24 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +19 -0
- package/build/src/index.js +35 -0
- package/build/src/index.js.map +1 -0
- package/build/src/types/api-response.d.ts +44 -0
- package/build/src/types/api-response.js +3 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +105 -0
- package/build/src/utils/array.utils.js +216 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +119 -0
- package/build/src/utils/async.utils.js +304 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +90 -0
- package/build/src/utils/cache.utils.js +146 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +85 -0
- package/build/src/utils/context-store.utils.js +100 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +58 -0
- package/build/src/utils/crypto.utils.js +82 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +75 -0
- package/build/src/utils/dir.utils.js +164 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +131 -0
- package/build/src/utils/env.utils.js +215 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +114 -0
- package/build/src/utils/exception.utils.js +162 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +32 -0
- package/build/src/utils/fs.utils.js +71 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +357 -0
- package/build/src/utils/http-status-codes.js +361 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +35 -0
- package/build/src/utils/id.utils.js +60 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +15 -0
- package/build/src/utils/logger.utils.js +96 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +55 -0
- package/build/src/utils/obj.utils.js +103 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +48 -0
- package/build/src/utils/response.utils.js +63 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +37 -0
- package/build/src/utils/string.utils.js +54 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +24 -0
- package/build/src/utils/url.utils.js +39 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +60 -0
- package/build/src/utils/validate.utils.js +115 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +80 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
|
|
3
|
+
*
|
|
4
|
+
* @param {string} dirPath - The directory path to ensure.
|
|
5
|
+
* @returns {Promise<void>} Resolves when the directory exists.
|
|
6
|
+
* @throws {Error} If directory cannot be created.
|
|
7
|
+
*/
|
|
8
|
+
export declare function ensureDir(dirPath: string): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Recursively lists all files in a directory.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} dirPath - The base directory.
|
|
13
|
+
* @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
|
|
14
|
+
* @returns {Promise<string[]>} Array of absolute file paths.
|
|
15
|
+
* @throws {Error} If the directory cannot be read.
|
|
16
|
+
*/
|
|
17
|
+
export declare function listFiles(dirPath: string, recursive?: boolean): Promise<string[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Deletes a directory and all its contents recursively (like `rm -rf`).
|
|
20
|
+
*
|
|
21
|
+
* @param {string} dirPath - Directory to delete.
|
|
22
|
+
* @returns {Promise<void>} Resolves when deletion is complete.
|
|
23
|
+
* @throws {Error} If deletion fails.
|
|
24
|
+
*/
|
|
25
|
+
export declare function deleteDirRecursive(dirPath: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether a given path is a directory.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} pathStr - Path to check.
|
|
30
|
+
* @returns {Promise<boolean>} True if the path is a directory, else false.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isDirectory(pathStr: string): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Recursively copies a directory and all its contents to a destination.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} src - Source directory path.
|
|
37
|
+
* @param {string} dest - Destination directory path.
|
|
38
|
+
* @returns {Promise<void>} Resolves when copy is complete.
|
|
39
|
+
* @throws {Error} If source does not exist or copy fails.
|
|
40
|
+
*/
|
|
41
|
+
export declare function copyDir(src: string, dest: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Moves a directory to a new location by copying and deleting the original.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} src - Source directory path.
|
|
46
|
+
* @param {string} dest - Destination directory path.
|
|
47
|
+
* @returns {Promise<void>} Resolves when move is complete.
|
|
48
|
+
* @throws {Error} If copy or deletion fails.
|
|
49
|
+
*/
|
|
50
|
+
export declare function moveDir(src: string, dest: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Empties a directory by deleting all files and subdirectories inside it.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} dirPath - Path to the directory to empty.
|
|
55
|
+
* @returns {Promise<void>} Resolves when the directory has been emptied.
|
|
56
|
+
* @throws {Error} If files or subdirectories cannot be removed.
|
|
57
|
+
*/
|
|
58
|
+
export declare function emptyDir(dirPath: string): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Calculates the total size (in bytes) of all files in a directory (recursive).
|
|
61
|
+
*
|
|
62
|
+
* @param {string} dirPath - Path to the directory.
|
|
63
|
+
* @returns {Promise<number>} Total size in bytes.
|
|
64
|
+
* @throws {Error} If any file stats cannot be read.
|
|
65
|
+
*/
|
|
66
|
+
export declare function getDirSize(dirPath: string): Promise<number>;
|
|
67
|
+
/**
|
|
68
|
+
* Watches a directory for file changes and calls a callback on each event.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} dirPath - Directory path to watch.
|
|
71
|
+
* @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
|
|
72
|
+
* @returns {() => void} A function to stop watching the directory.
|
|
73
|
+
*/
|
|
74
|
+
export declare function watchDir(dirPath: string, callback: (eventType: "rename" | "change", filename: string | null) => void): () => void;
|
|
75
|
+
//# sourceMappingURL=dir.utils.d.ts.map
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
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);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
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;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var __values = (this && this.__values) || function(o) {
|
|
38
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
39
|
+
if (m) return m.call(o);
|
|
40
|
+
if (o && typeof o.length === "number") return {
|
|
41
|
+
next: function () {
|
|
42
|
+
if (o && i >= o.length) o = void 0;
|
|
43
|
+
return { value: o && o[i++], done: !o };
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
47
|
+
};
|
|
48
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
49
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
50
|
+
if (!m) return o;
|
|
51
|
+
var i = m.call(o), r, ar = [], e;
|
|
52
|
+
try {
|
|
53
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
54
|
+
}
|
|
55
|
+
catch (error) { e = { error: error }; }
|
|
56
|
+
finally {
|
|
57
|
+
try {
|
|
58
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
59
|
+
}
|
|
60
|
+
finally { if (e) throw e.error; }
|
|
61
|
+
}
|
|
62
|
+
return ar;
|
|
63
|
+
};
|
|
64
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
65
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
66
|
+
if (ar || !(i in from)) {
|
|
67
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
68
|
+
ar[i] = from[i];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
72
|
+
};
|
|
73
|
+
import fs from "fs";
|
|
74
|
+
import fsp from "fs/promises";
|
|
75
|
+
import path from "path";
|
|
76
|
+
/**
|
|
77
|
+
* Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
|
|
78
|
+
*
|
|
79
|
+
* @param {string} dirPath - The directory path to ensure.
|
|
80
|
+
* @returns {Promise<void>} Resolves when the directory exists.
|
|
81
|
+
* @throws {Error} If directory cannot be created.
|
|
82
|
+
*/
|
|
83
|
+
export function ensureDir(dirPath) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
+
return __generator(this, function (_a) {
|
|
86
|
+
switch (_a.label) {
|
|
87
|
+
case 0: return [4 /*yield*/, fsp.mkdir(dirPath, { recursive: true })];
|
|
88
|
+
case 1:
|
|
89
|
+
_a.sent();
|
|
90
|
+
return [2 /*return*/];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Recursively lists all files in a directory.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} dirPath - The base directory.
|
|
99
|
+
* @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
|
|
100
|
+
* @returns {Promise<string[]>} Array of absolute file paths.
|
|
101
|
+
* @throws {Error} If the directory cannot be read.
|
|
102
|
+
*/
|
|
103
|
+
export function listFiles(dirPath_1) {
|
|
104
|
+
return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
|
|
105
|
+
var entries, files, entries_1, entries_1_1, entry, fullPath, nestedFiles, e_1_1;
|
|
106
|
+
var e_1, _a;
|
|
107
|
+
if (recursive === void 0) { recursive = false; }
|
|
108
|
+
return __generator(this, function (_b) {
|
|
109
|
+
switch (_b.label) {
|
|
110
|
+
case 0: return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
|
|
111
|
+
case 1:
|
|
112
|
+
entries = _b.sent();
|
|
113
|
+
files = [];
|
|
114
|
+
_b.label = 2;
|
|
115
|
+
case 2:
|
|
116
|
+
_b.trys.push([2, 8, 9, 10]);
|
|
117
|
+
entries_1 = __values(entries), entries_1_1 = entries_1.next();
|
|
118
|
+
_b.label = 3;
|
|
119
|
+
case 3:
|
|
120
|
+
if (!!entries_1_1.done) return [3 /*break*/, 7];
|
|
121
|
+
entry = entries_1_1.value;
|
|
122
|
+
fullPath = path.join(dirPath, entry.name);
|
|
123
|
+
if (!entry.isFile()) return [3 /*break*/, 4];
|
|
124
|
+
files.push(fullPath);
|
|
125
|
+
return [3 /*break*/, 6];
|
|
126
|
+
case 4:
|
|
127
|
+
if (!(recursive && entry.isDirectory())) return [3 /*break*/, 6];
|
|
128
|
+
return [4 /*yield*/, listFiles(fullPath, true)];
|
|
129
|
+
case 5:
|
|
130
|
+
nestedFiles = _b.sent();
|
|
131
|
+
files.push.apply(files, __spreadArray([], __read(nestedFiles), false));
|
|
132
|
+
_b.label = 6;
|
|
133
|
+
case 6:
|
|
134
|
+
entries_1_1 = entries_1.next();
|
|
135
|
+
return [3 /*break*/, 3];
|
|
136
|
+
case 7: return [3 /*break*/, 10];
|
|
137
|
+
case 8:
|
|
138
|
+
e_1_1 = _b.sent();
|
|
139
|
+
e_1 = { error: e_1_1 };
|
|
140
|
+
return [3 /*break*/, 10];
|
|
141
|
+
case 9:
|
|
142
|
+
try {
|
|
143
|
+
if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
|
|
144
|
+
}
|
|
145
|
+
finally { if (e_1) throw e_1.error; }
|
|
146
|
+
return [7 /*endfinally*/];
|
|
147
|
+
case 10: return [2 /*return*/, files];
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Deletes a directory and all its contents recursively (like `rm -rf`).
|
|
154
|
+
*
|
|
155
|
+
* @param {string} dirPath - Directory to delete.
|
|
156
|
+
* @returns {Promise<void>} Resolves when deletion is complete.
|
|
157
|
+
* @throws {Error} If deletion fails.
|
|
158
|
+
*/
|
|
159
|
+
export function deleteDirRecursive(dirPath) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
+
return __generator(this, function (_a) {
|
|
162
|
+
switch (_a.label) {
|
|
163
|
+
case 0: return [4 /*yield*/, fsp.rm(dirPath, { recursive: true, force: true })];
|
|
164
|
+
case 1:
|
|
165
|
+
_a.sent();
|
|
166
|
+
return [2 /*return*/];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Checks whether a given path is a directory.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} pathStr - Path to check.
|
|
175
|
+
* @returns {Promise<boolean>} True if the path is a directory, else false.
|
|
176
|
+
*/
|
|
177
|
+
export function isDirectory(pathStr) {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
179
|
+
var stat, _a;
|
|
180
|
+
return __generator(this, function (_b) {
|
|
181
|
+
switch (_b.label) {
|
|
182
|
+
case 0:
|
|
183
|
+
_b.trys.push([0, 2, , 3]);
|
|
184
|
+
return [4 /*yield*/, fsp.stat(pathStr)];
|
|
185
|
+
case 1:
|
|
186
|
+
stat = _b.sent();
|
|
187
|
+
return [2 /*return*/, stat.isDirectory()];
|
|
188
|
+
case 2:
|
|
189
|
+
_a = _b.sent();
|
|
190
|
+
return [2 /*return*/, false];
|
|
191
|
+
case 3: return [2 /*return*/];
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Recursively copies a directory and all its contents to a destination.
|
|
198
|
+
*
|
|
199
|
+
* @param {string} src - Source directory path.
|
|
200
|
+
* @param {string} dest - Destination directory path.
|
|
201
|
+
* @returns {Promise<void>} Resolves when copy is complete.
|
|
202
|
+
* @throws {Error} If source does not exist or copy fails.
|
|
203
|
+
*/
|
|
204
|
+
export function copyDir(src, dest) {
|
|
205
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
206
|
+
var entries, entries_2, entries_2_1, entry, srcPath, destPath, e_2_1;
|
|
207
|
+
var e_2, _a;
|
|
208
|
+
return __generator(this, function (_b) {
|
|
209
|
+
switch (_b.label) {
|
|
210
|
+
case 0: return [4 /*yield*/, ensureDir(dest)];
|
|
211
|
+
case 1:
|
|
212
|
+
_b.sent();
|
|
213
|
+
return [4 /*yield*/, fsp.readdir(src, { withFileTypes: true })];
|
|
214
|
+
case 2:
|
|
215
|
+
entries = _b.sent();
|
|
216
|
+
_b.label = 3;
|
|
217
|
+
case 3:
|
|
218
|
+
_b.trys.push([3, 10, 11, 12]);
|
|
219
|
+
entries_2 = __values(entries), entries_2_1 = entries_2.next();
|
|
220
|
+
_b.label = 4;
|
|
221
|
+
case 4:
|
|
222
|
+
if (!!entries_2_1.done) return [3 /*break*/, 9];
|
|
223
|
+
entry = entries_2_1.value;
|
|
224
|
+
srcPath = path.join(src, entry.name);
|
|
225
|
+
destPath = path.join(dest, entry.name);
|
|
226
|
+
if (!entry.isDirectory()) return [3 /*break*/, 6];
|
|
227
|
+
return [4 /*yield*/, copyDir(srcPath, destPath)];
|
|
228
|
+
case 5:
|
|
229
|
+
_b.sent();
|
|
230
|
+
return [3 /*break*/, 8];
|
|
231
|
+
case 6: return [4 /*yield*/, fsp.copyFile(srcPath, destPath)];
|
|
232
|
+
case 7:
|
|
233
|
+
_b.sent();
|
|
234
|
+
_b.label = 8;
|
|
235
|
+
case 8:
|
|
236
|
+
entries_2_1 = entries_2.next();
|
|
237
|
+
return [3 /*break*/, 4];
|
|
238
|
+
case 9: return [3 /*break*/, 12];
|
|
239
|
+
case 10:
|
|
240
|
+
e_2_1 = _b.sent();
|
|
241
|
+
e_2 = { error: e_2_1 };
|
|
242
|
+
return [3 /*break*/, 12];
|
|
243
|
+
case 11:
|
|
244
|
+
try {
|
|
245
|
+
if (entries_2_1 && !entries_2_1.done && (_a = entries_2.return)) _a.call(entries_2);
|
|
246
|
+
}
|
|
247
|
+
finally { if (e_2) throw e_2.error; }
|
|
248
|
+
return [7 /*endfinally*/];
|
|
249
|
+
case 12: return [2 /*return*/];
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Moves a directory to a new location by copying and deleting the original.
|
|
256
|
+
*
|
|
257
|
+
* @param {string} src - Source directory path.
|
|
258
|
+
* @param {string} dest - Destination directory path.
|
|
259
|
+
* @returns {Promise<void>} Resolves when move is complete.
|
|
260
|
+
* @throws {Error} If copy or deletion fails.
|
|
261
|
+
*/
|
|
262
|
+
export function moveDir(src, dest) {
|
|
263
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
264
|
+
return __generator(this, function (_a) {
|
|
265
|
+
switch (_a.label) {
|
|
266
|
+
case 0: return [4 /*yield*/, copyDir(src, dest)];
|
|
267
|
+
case 1:
|
|
268
|
+
_a.sent();
|
|
269
|
+
return [4 /*yield*/, deleteDirRecursive(src)];
|
|
270
|
+
case 2:
|
|
271
|
+
_a.sent();
|
|
272
|
+
return [2 /*return*/];
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Empties a directory by deleting all files and subdirectories inside it.
|
|
279
|
+
*
|
|
280
|
+
* @param {string} dirPath - Path to the directory to empty.
|
|
281
|
+
* @returns {Promise<void>} Resolves when the directory has been emptied.
|
|
282
|
+
* @throws {Error} If files or subdirectories cannot be removed.
|
|
283
|
+
*/
|
|
284
|
+
export function emptyDir(dirPath) {
|
|
285
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
286
|
+
var entries, entries_3, entries_3_1, entry, entryPath, e_3_1;
|
|
287
|
+
var e_3, _a;
|
|
288
|
+
return __generator(this, function (_b) {
|
|
289
|
+
switch (_b.label) {
|
|
290
|
+
case 0: return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
|
|
291
|
+
case 1:
|
|
292
|
+
entries = _b.sent();
|
|
293
|
+
_b.label = 2;
|
|
294
|
+
case 2:
|
|
295
|
+
_b.trys.push([2, 9, 10, 11]);
|
|
296
|
+
entries_3 = __values(entries), entries_3_1 = entries_3.next();
|
|
297
|
+
_b.label = 3;
|
|
298
|
+
case 3:
|
|
299
|
+
if (!!entries_3_1.done) return [3 /*break*/, 8];
|
|
300
|
+
entry = entries_3_1.value;
|
|
301
|
+
entryPath = path.join(dirPath, entry.name);
|
|
302
|
+
if (!entry.isDirectory()) return [3 /*break*/, 5];
|
|
303
|
+
return [4 /*yield*/, fsp.rm(entryPath, { recursive: true, force: true })];
|
|
304
|
+
case 4:
|
|
305
|
+
_b.sent();
|
|
306
|
+
return [3 /*break*/, 7];
|
|
307
|
+
case 5: return [4 /*yield*/, fsp.unlink(entryPath)];
|
|
308
|
+
case 6:
|
|
309
|
+
_b.sent();
|
|
310
|
+
_b.label = 7;
|
|
311
|
+
case 7:
|
|
312
|
+
entries_3_1 = entries_3.next();
|
|
313
|
+
return [3 /*break*/, 3];
|
|
314
|
+
case 8: return [3 /*break*/, 11];
|
|
315
|
+
case 9:
|
|
316
|
+
e_3_1 = _b.sent();
|
|
317
|
+
e_3 = { error: e_3_1 };
|
|
318
|
+
return [3 /*break*/, 11];
|
|
319
|
+
case 10:
|
|
320
|
+
try {
|
|
321
|
+
if (entries_3_1 && !entries_3_1.done && (_a = entries_3.return)) _a.call(entries_3);
|
|
322
|
+
}
|
|
323
|
+
finally { if (e_3) throw e_3.error; }
|
|
324
|
+
return [7 /*endfinally*/];
|
|
325
|
+
case 11: return [2 /*return*/];
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Calculates the total size (in bytes) of all files in a directory (recursive).
|
|
332
|
+
*
|
|
333
|
+
* @param {string} dirPath - Path to the directory.
|
|
334
|
+
* @returns {Promise<number>} Total size in bytes.
|
|
335
|
+
* @throws {Error} If any file stats cannot be read.
|
|
336
|
+
*/
|
|
337
|
+
export function getDirSize(dirPath) {
|
|
338
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
339
|
+
var total, entries, entries_4, entries_4_1, entry, entryPath, _a, stat, e_4_1;
|
|
340
|
+
var e_4, _b;
|
|
341
|
+
return __generator(this, function (_c) {
|
|
342
|
+
switch (_c.label) {
|
|
343
|
+
case 0:
|
|
344
|
+
total = 0;
|
|
345
|
+
return [4 /*yield*/, fsp.readdir(dirPath, { withFileTypes: true })];
|
|
346
|
+
case 1:
|
|
347
|
+
entries = _c.sent();
|
|
348
|
+
_c.label = 2;
|
|
349
|
+
case 2:
|
|
350
|
+
_c.trys.push([2, 9, 10, 11]);
|
|
351
|
+
entries_4 = __values(entries), entries_4_1 = entries_4.next();
|
|
352
|
+
_c.label = 3;
|
|
353
|
+
case 3:
|
|
354
|
+
if (!!entries_4_1.done) return [3 /*break*/, 8];
|
|
355
|
+
entry = entries_4_1.value;
|
|
356
|
+
entryPath = path.join(dirPath, entry.name);
|
|
357
|
+
if (!entry.isDirectory()) return [3 /*break*/, 5];
|
|
358
|
+
_a = total;
|
|
359
|
+
return [4 /*yield*/, getDirSize(entryPath)];
|
|
360
|
+
case 4:
|
|
361
|
+
total = _a + _c.sent();
|
|
362
|
+
return [3 /*break*/, 7];
|
|
363
|
+
case 5: return [4 /*yield*/, fsp.stat(entryPath)];
|
|
364
|
+
case 6:
|
|
365
|
+
stat = _c.sent();
|
|
366
|
+
total += stat.size;
|
|
367
|
+
_c.label = 7;
|
|
368
|
+
case 7:
|
|
369
|
+
entries_4_1 = entries_4.next();
|
|
370
|
+
return [3 /*break*/, 3];
|
|
371
|
+
case 8: return [3 /*break*/, 11];
|
|
372
|
+
case 9:
|
|
373
|
+
e_4_1 = _c.sent();
|
|
374
|
+
e_4 = { error: e_4_1 };
|
|
375
|
+
return [3 /*break*/, 11];
|
|
376
|
+
case 10:
|
|
377
|
+
try {
|
|
378
|
+
if (entries_4_1 && !entries_4_1.done && (_b = entries_4.return)) _b.call(entries_4);
|
|
379
|
+
}
|
|
380
|
+
finally { if (e_4) throw e_4.error; }
|
|
381
|
+
return [7 /*endfinally*/];
|
|
382
|
+
case 11: return [2 /*return*/, total];
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Watches a directory for file changes and calls a callback on each event.
|
|
389
|
+
*
|
|
390
|
+
* @param {string} dirPath - Directory path to watch.
|
|
391
|
+
* @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
|
|
392
|
+
* @returns {() => void} A function to stop watching the directory.
|
|
393
|
+
*/
|
|
394
|
+
export function watchDir(dirPath, callback) {
|
|
395
|
+
var watcher = fs.watch(dirPath, function (eventType, filename) {
|
|
396
|
+
callback(eventType, filename);
|
|
397
|
+
});
|
|
398
|
+
return function () { return watcher.close(); };
|
|
399
|
+
}
|
|
400
|
+
//# sourceMappingURL=dir.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dir.utils.js","sourceRoot":"","sources":["../../../src/utils/dir.utils.ts"],"names":[],"mappings":"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","sourcesContent":["import fs from \"fs\";\nimport fsp from \"fs/promises\";\nimport path from \"path\";\n\n/**\n * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).\n *\n * @param {string} dirPath - The directory path to ensure.\n * @returns {Promise<void>} Resolves when the directory exists.\n * @throws {Error} If directory cannot be created.\n */\nexport async function ensureDir(dirPath: string): Promise<void> {\n await fsp.mkdir(dirPath, { recursive: true });\n}\n\n/**\n * Recursively lists all files in a directory.\n *\n * @param {string} dirPath - The base directory.\n * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.\n * @returns {Promise<string[]>} Array of absolute file paths.\n * @throws {Error} If the directory cannot be read.\n */\nexport async function listFiles(\n dirPath: string,\n recursive = false,\n): Promise<string[]> {\n const entries = await fsp.readdir(dirPath, { withFileTypes: true });\n const files: string[] = [];\n\n for (const entry of entries) {\n const fullPath = path.join(dirPath, entry.name);\n\n if (entry.isFile()) {\n files.push(fullPath);\n } else if (recursive && entry.isDirectory()) {\n const nestedFiles = await listFiles(fullPath, true);\n files.push(...nestedFiles);\n }\n }\n\n return files;\n}\n\n/**\n * Deletes a directory and all its contents recursively (like `rm -rf`).\n *\n * @param {string} dirPath - Directory to delete.\n * @returns {Promise<void>} Resolves when deletion is complete.\n * @throws {Error} If deletion fails.\n */\nexport async function deleteDirRecursive(dirPath: string): Promise<void> {\n await fsp.rm(dirPath, { recursive: true, force: true });\n}\n\n/**\n * Checks whether a given path is a directory.\n *\n * @param {string} pathStr - Path to check.\n * @returns {Promise<boolean>} True if the path is a directory, else false.\n */\nexport async function isDirectory(pathStr: string): Promise<boolean> {\n try {\n const stat = await fsp.stat(pathStr);\n return stat.isDirectory();\n } catch {\n return false;\n }\n}\n\n/**\n * Recursively copies a directory and all its contents to a destination.\n *\n * @param {string} src - Source directory path.\n * @param {string} dest - Destination directory path.\n * @returns {Promise<void>} Resolves when copy is complete.\n * @throws {Error} If source does not exist or copy fails.\n */\nexport async function copyDir(src: string, dest: string): Promise<void> {\n await ensureDir(dest);\n const entries = await fsp.readdir(src, { withFileTypes: true });\n\n for (const entry of entries) {\n const srcPath = path.join(src, entry.name);\n const destPath = path.join(dest, entry.name);\n\n if (entry.isDirectory()) {\n await copyDir(srcPath, destPath);\n } else {\n await fsp.copyFile(srcPath, destPath);\n }\n }\n}\n\n/**\n * Moves a directory to a new location by copying and deleting the original.\n *\n * @param {string} src - Source directory path.\n * @param {string} dest - Destination directory path.\n * @returns {Promise<void>} Resolves when move is complete.\n * @throws {Error} If copy or deletion fails.\n */\nexport async function moveDir(src: string, dest: string): Promise<void> {\n await copyDir(src, dest);\n await deleteDirRecursive(src);\n}\n\n/**\n * Empties a directory by deleting all files and subdirectories inside it.\n *\n * @param {string} dirPath - Path to the directory to empty.\n * @returns {Promise<void>} Resolves when the directory has been emptied.\n * @throws {Error} If files or subdirectories cannot be removed.\n */\nexport async function emptyDir(dirPath: string): Promise<void> {\n const entries = await fsp.readdir(dirPath, { withFileTypes: true });\n\n for (const entry of entries) {\n const entryPath = path.join(dirPath, entry.name);\n\n if (entry.isDirectory()) {\n await fsp.rm(entryPath, { recursive: true, force: true });\n } else {\n await fsp.unlink(entryPath);\n }\n }\n}\n\n/**\n * Calculates the total size (in bytes) of all files in a directory (recursive).\n *\n * @param {string} dirPath - Path to the directory.\n * @returns {Promise<number>} Total size in bytes.\n * @throws {Error} If any file stats cannot be read.\n */\nexport async function getDirSize(dirPath: string): Promise<number> {\n let total = 0;\n const entries = await fsp.readdir(dirPath, { withFileTypes: true });\n\n for (const entry of entries) {\n const entryPath = path.join(dirPath, entry.name);\n if (entry.isDirectory()) {\n total += await getDirSize(entryPath);\n } else {\n const stat = await fsp.stat(entryPath);\n total += stat.size;\n }\n }\n\n return total;\n}\n\n/**\n * Watches a directory for file changes and calls a callback on each event.\n *\n * @param {string} dirPath - Directory path to watch.\n * @param {(eventType: \"rename\" | \"change\", filename: string | null) => void} callback - Callback for each change event.\n * @returns {() => void} A function to stop watching the directory.\n */\nexport function watchDir(\n dirPath: string,\n callback: (eventType: \"rename\" | \"change\", filename: string | null) => void,\n): () => void {\n const watcher = fs.watch(dirPath, (eventType, filename) => {\n callback(eventType, filename);\n });\n\n return () => watcher.close();\n}\n"]}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enum representing valid application environments.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum Environment {
|
|
5
|
+
PRODUCTION = "production",
|
|
6
|
+
DEVELOPMENT = "development",
|
|
7
|
+
STAGING = "staging",
|
|
8
|
+
TESTING = "testing"
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Utility class for accessing and managing environment variables.
|
|
12
|
+
* Provides typed getters with fallback defaults and validation.
|
|
13
|
+
*/
|
|
14
|
+
export declare class Env {
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the current NODE_ENV is 'development'.
|
|
17
|
+
*
|
|
18
|
+
* @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
|
|
19
|
+
*/
|
|
20
|
+
static isDev(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Sets an environment variable (only affects runtime memory).
|
|
23
|
+
*
|
|
24
|
+
* @param {string} key - The environment variable key.
|
|
25
|
+
* @param {string} value - The value to set.
|
|
26
|
+
*/
|
|
27
|
+
static set(key: string, value: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns all environment variables as an object.
|
|
30
|
+
*
|
|
31
|
+
* @returns {object} The current `process.env` object.
|
|
32
|
+
*/
|
|
33
|
+
static getAll(): object;
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves a string environment variable with a fallback default.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} key - The environment variable key.
|
|
38
|
+
* @param {string} [defaultValue] - Value to return if the key is missing.
|
|
39
|
+
* @returns {string | undefined} The env value or the fallback.
|
|
40
|
+
*/
|
|
41
|
+
static get(key: string, defaultValue?: string): string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves a string environment variable and throws if it's missing.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} key - The environment variable key.
|
|
46
|
+
* @returns {string} The environment variable's value.
|
|
47
|
+
* @throws {Error} If the variable is not defined.
|
|
48
|
+
*/
|
|
49
|
+
static getRequired(key: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves an environment variable as a number, or returns a default.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} key - The environment variable key.
|
|
54
|
+
* @param {number} defaultValue - Fallback number if key is not present.
|
|
55
|
+
* @returns {number} Parsed numeric value or default.
|
|
56
|
+
* @throws {Error} If the value is not a valid number.
|
|
57
|
+
*/
|
|
58
|
+
static getNumber(key: string, defaultValue: number): number;
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves a required environment variable as a number.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} key - The environment variable key.
|
|
63
|
+
* @returns {number} Parsed number.
|
|
64
|
+
* @throws {Error} If the value is missing or not a number.
|
|
65
|
+
*/
|
|
66
|
+
static getNumberRequired(key: string): number;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves an environment variable as a boolean.
|
|
69
|
+
* Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} key - The environment variable key.
|
|
72
|
+
* @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
|
|
73
|
+
* @returns {boolean} Parsed boolean.
|
|
74
|
+
* @throws {Error} If the value is not a recognized boolean string.
|
|
75
|
+
*/
|
|
76
|
+
static getBoolean(key: string, defaultValue?: boolean): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Retrieves a required environment variable as a boolean.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} key - The environment variable key.
|
|
81
|
+
* @returns {boolean} Parsed boolean value.
|
|
82
|
+
* @throws {Error} If missing or invalid.
|
|
83
|
+
*/
|
|
84
|
+
static getBooleanRequired(key: string): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Parses a stringified JSON object from an environment variable.
|
|
87
|
+
*
|
|
88
|
+
* @typeParam T - The type to parse as (defaults to `object`).
|
|
89
|
+
* @param {string} key - The environment variable key.
|
|
90
|
+
* @param {T} defaultValue - Value to return if key is missing.
|
|
91
|
+
* @returns {T} Parsed object or default.
|
|
92
|
+
* @throws {Error} If the value is not valid JSON.
|
|
93
|
+
*/
|
|
94
|
+
static getJSON<T extends object = object>(key: string, defaultValue: T): T;
|
|
95
|
+
/**
|
|
96
|
+
* Parses a comma-separated string as an array.
|
|
97
|
+
*
|
|
98
|
+
* @typeParam T - The item type (optional, defaults to string).
|
|
99
|
+
* @param {string} key - The environment variable key.
|
|
100
|
+
* @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
|
|
101
|
+
* @param {string} [splitter=','] - Delimiter to split on.
|
|
102
|
+
* @returns {string[] | T[]} An array of strings.
|
|
103
|
+
*/
|
|
104
|
+
static getArray<T = string>(key: string, defaultValue?: T[], splitter?: string): string[] | T[];
|
|
105
|
+
/**
|
|
106
|
+
* Retrieves an enum-like environment variable value, validating against allowed values.
|
|
107
|
+
*
|
|
108
|
+
* @typeParam T - The allowed value type (string literal types).
|
|
109
|
+
* @param {string} key - The environment variable key.
|
|
110
|
+
* @param {T[]} allowedValues - Array of accepted string values.
|
|
111
|
+
* @param {T} [defaultValue] - Optional fallback value.
|
|
112
|
+
* @returns {T} The validated environment value.
|
|
113
|
+
* @throws {Error} If missing or invalid.
|
|
114
|
+
*/
|
|
115
|
+
static getEnum<T extends string>(key: string, allowedValues: T[], defaultValue?: T): T;
|
|
116
|
+
/**
|
|
117
|
+
* Checks whether the specified environment variable exists.
|
|
118
|
+
*
|
|
119
|
+
* @param {string} key - The environment variable key.
|
|
120
|
+
* @returns {boolean} `true` if the variable is defined, otherwise `false`.
|
|
121
|
+
*/
|
|
122
|
+
static has(key: string): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Deletes the given environment variable (useful in tests).
|
|
125
|
+
*
|
|
126
|
+
* @param {string} key - The environment variable key to delete.
|
|
127
|
+
* @returns {void}
|
|
128
|
+
*/
|
|
129
|
+
static delete(key: string): void;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=env.utils.d.ts.map
|