@catbee/utils 0.0.8-next.0 → 0.0.8-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -26
- package/build/esm/config.d.ts +122 -0
- package/build/esm/config.js +133 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +27 -0
- package/build/esm/index.js +50 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/servers/server.builder.d.ts +508 -0
- package/build/esm/servers/server.builder.js +702 -0
- package/build/esm/servers/server.builder.js.map +1 -0
- package/build/esm/servers/server.d.ts +256 -0
- package/build/esm/servers/server.js +1248 -0
- package/build/esm/servers/server.js.map +1 -0
- package/build/esm/types/api-response.d.ts +152 -0
- package/build/esm/types/api-response.js +34 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/types/index.d.ts +125 -0
- package/build/esm/types/index.js +25 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/server.d.ts +268 -0
- package/build/esm/types/server.js +25 -0
- package/build/esm/types/server.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +168 -0
- package/build/esm/utils/array.utils.js +382 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +265 -0
- package/build/esm/utils/async.utils.js +896 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +153 -0
- package/build/esm/utils/cache.utils.js +499 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +189 -0
- package/build/esm/utils/context-store.utils.js +307 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +160 -0
- package/build/esm/utils/crypto.utils.js +375 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/decorators.utils.d.ts +315 -0
- package/build/esm/utils/decorators.utils.js +666 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +196 -0
- package/build/esm/utils/dir.utils.js +998 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +377 -0
- package/build/esm/utils/env.utils.js +873 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +230 -0
- package/build/esm/utils/exception.utils.js +559 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +164 -0
- package/build/esm/utils/fs.utils.js +580 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +266 -0
- package/build/esm/utils/http-status-codes.js +295 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +36 -0
- package/build/esm/utils/id.utils.js +87 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +160 -0
- package/build/esm/utils/logger.utils.js +341 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/middleware.utils.d.ts +100 -0
- package/build/esm/utils/middleware.utils.js +325 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +124 -0
- package/build/esm/utils/obj.utils.js +519 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +86 -0
- package/build/esm/utils/request.utils.js +223 -0
- package/build/esm/utils/request.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +163 -0
- package/build/esm/utils/response.utils.js +284 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +93 -0
- package/build/esm/utils/string.utils.js +169 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +141 -0
- package/build/esm/utils/url.utils.js +381 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +177 -0
- package/build/esm/utils/validate.utils.js +336 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +122 -0
- package/build/esnext/config.js +133 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +27 -0
- package/build/esnext/index.js +50 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/servers/server.builder.d.ts +508 -0
- package/build/esnext/servers/server.builder.js +657 -0
- package/build/esnext/servers/server.builder.js.map +1 -0
- package/build/esnext/servers/server.d.ts +256 -0
- package/build/esnext/servers/server.js +955 -0
- package/build/esnext/servers/server.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +152 -0
- package/build/esnext/types/api-response.js +34 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/types/index.d.ts +125 -0
- package/build/esnext/types/index.js +25 -0
- package/build/esnext/types/index.js.map +1 -0
- package/build/esnext/types/server.d.ts +268 -0
- package/build/esnext/types/server.js +25 -0
- package/build/esnext/types/server.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +168 -0
- package/build/esnext/utils/array.utils.js +345 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +265 -0
- package/build/esnext/utils/async.utils.js +620 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +153 -0
- package/build/esnext/utils/cache.utils.js +293 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +189 -0
- package/build/esnext/utils/context-store.utils.js +289 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +160 -0
- package/build/esnext/utils/crypto.utils.js +276 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/decorators.utils.d.ts +315 -0
- package/build/esnext/utils/decorators.utils.js +504 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +196 -0
- package/build/esnext/utils/dir.utils.js +477 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +377 -0
- package/build/esnext/utils/env.utils.js +785 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +230 -0
- package/build/esnext/utils/exception.utils.js +381 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +164 -0
- package/build/esnext/utils/fs.utils.js +349 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +266 -0
- package/build/esnext/utils/http-status-codes.js +295 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +36 -0
- package/build/esnext/utils/id.utils.js +84 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +160 -0
- package/build/esnext/utils/logger.utils.js +300 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/middleware.utils.d.ts +100 -0
- package/build/esnext/utils/middleware.utils.js +236 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +124 -0
- package/build/esnext/utils/obj.utils.js +413 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +86 -0
- package/build/esnext/utils/request.utils.js +186 -0
- package/build/esnext/utils/request.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +163 -0
- package/build/esnext/utils/response.utils.js +247 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +93 -0
- package/build/esnext/utils/string.utils.js +165 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +141 -0
- package/build/esnext/utils/url.utils.js +304 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +177 -0
- package/build/esnext/utils/validate.utils.js +320 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +122 -0
- package/build/src/config.js +138 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +27 -0
- package/build/src/index.js +69 -0
- package/build/src/index.js.map +1 -0
- package/build/src/servers/server.builder.d.ts +508 -0
- package/build/src/servers/server.builder.js +661 -0
- package/build/src/servers/server.builder.js.map +1 -0
- package/build/src/servers/server.d.ts +256 -0
- package/build/src/servers/server.js +962 -0
- package/build/src/servers/server.js.map +1 -0
- package/build/src/types/api-response.d.ts +152 -0
- package/build/src/types/api-response.js +37 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/types/index.d.ts +125 -0
- package/build/src/types/index.js +26 -0
- package/build/src/types/index.js.map +1 -0
- package/build/src/types/server.d.ts +268 -0
- package/build/src/types/server.js +26 -0
- package/build/src/types/server.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +168 -0
- package/build/src/utils/array.utils.js +364 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +265 -0
- package/build/src/utils/async.utils.js +641 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +153 -0
- package/build/src/utils/cache.utils.js +297 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +189 -0
- package/build/src/utils/context-store.utils.js +296 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +160 -0
- package/build/src/utils/crypto.utils.js +293 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/decorators.utils.d.ts +315 -0
- package/build/src/utils/decorators.utils.js +516 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +196 -0
- package/build/src/utils/dir.utils.js +501 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +377 -0
- package/build/src/utils/env.utils.js +789 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +230 -0
- package/build/src/utils/exception.utils.js +407 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +164 -0
- package/build/src/utils/fs.utils.js +372 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +266 -0
- package/build/src/utils/http-status-codes.js +298 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +36 -0
- package/build/src/utils/id.utils.js +91 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +160 -0
- package/build/src/utils/logger.utils.js +346 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/middleware.utils.d.ts +100 -0
- package/build/src/utils/middleware.utils.js +244 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +124 -0
- package/build/src/utils/obj.utils.js +429 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +86 -0
- package/build/src/utils/request.utils.js +195 -0
- package/build/src/utils/request.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +163 -0
- package/build/src/utils/response.utils.js +260 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +93 -0
- package/build/src/utils/string.utils.js +179 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +141 -0
- package/build/src/utils/url.utils.js +317 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +177 -0
- package/build/src/utils/validate.utils.js +345 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +66 -12
- package/build/index.cjs +0 -6888
- package/build/index.d.ts +0 -5242
- package/build/index.mjs +0 -6615
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
25
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
26
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
27
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
28
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
29
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
34
|
+
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);
|
|
35
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
36
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
37
|
+
function step(op) {
|
|
38
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
39
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
40
|
+
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;
|
|
41
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
42
|
+
switch (op[0]) {
|
|
43
|
+
case 0: case 1: t = op; break;
|
|
44
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
45
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
46
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
47
|
+
default:
|
|
48
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
49
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
50
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
51
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
52
|
+
if (t[2]) _.ops.pop();
|
|
53
|
+
_.trys.pop(); continue;
|
|
54
|
+
}
|
|
55
|
+
op = body.call(thisArg, _);
|
|
56
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
57
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
import fs from 'fs/promises';
|
|
61
|
+
import { createReadStream, createWriteStream } from 'fs';
|
|
62
|
+
import { tmpdir } from 'os';
|
|
63
|
+
import path from 'path';
|
|
64
|
+
import { pipeline } from 'stream/promises';
|
|
65
|
+
import { randomUUID } from 'crypto';
|
|
66
|
+
/**
|
|
67
|
+
* Checks whether a file or directory exists at the given path.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} path - The file or directory path to check.
|
|
70
|
+
* @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
|
|
71
|
+
*/
|
|
72
|
+
export function fileExists(path) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
74
|
+
var _a;
|
|
75
|
+
return __generator(this, function (_b) {
|
|
76
|
+
switch (_b.label) {
|
|
77
|
+
case 0:
|
|
78
|
+
_b.trys.push([0, 2, , 3]);
|
|
79
|
+
return [4 /*yield*/, fs.access(path)];
|
|
80
|
+
case 1:
|
|
81
|
+
_b.sent();
|
|
82
|
+
return [2 /*return*/, true];
|
|
83
|
+
case 2:
|
|
84
|
+
_a = _b.sent();
|
|
85
|
+
return [2 /*return*/, false];
|
|
86
|
+
case 3: return [2 /*return*/];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Reads and parses a JSON file from the specified path.
|
|
93
|
+
*
|
|
94
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
95
|
+
* @param {string} path - The path to the JSON file.
|
|
96
|
+
* @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
|
|
97
|
+
*/
|
|
98
|
+
export function readJsonFile(path) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
100
|
+
var data, _a;
|
|
101
|
+
return __generator(this, function (_b) {
|
|
102
|
+
switch (_b.label) {
|
|
103
|
+
case 0:
|
|
104
|
+
_b.trys.push([0, 2, , 3]);
|
|
105
|
+
return [4 /*yield*/, fs.readFile(path, 'utf-8')];
|
|
106
|
+
case 1:
|
|
107
|
+
data = _b.sent();
|
|
108
|
+
return [2 /*return*/, JSON.parse(data)];
|
|
109
|
+
case 2:
|
|
110
|
+
_a = _b.sent();
|
|
111
|
+
return [2 /*return*/, null];
|
|
112
|
+
case 3: return [2 /*return*/];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Writes a JavaScript object to a file as formatted JSON.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} path - The destination file path.
|
|
121
|
+
* @param {any} data - The object to serialize and write.
|
|
122
|
+
* @param {number} [space=2] - Number of spaces for JSON formatting.
|
|
123
|
+
* @returns {Promise<void>} Resolves when writing is complete.
|
|
124
|
+
*/
|
|
125
|
+
export function writeJsonFile(path_1, data_1) {
|
|
126
|
+
return __awaiter(this, arguments, void 0, function (path, data, space) {
|
|
127
|
+
var json;
|
|
128
|
+
if (space === void 0) { space = 2; }
|
|
129
|
+
return __generator(this, function (_a) {
|
|
130
|
+
switch (_a.label) {
|
|
131
|
+
case 0:
|
|
132
|
+
json = JSON.stringify(data, null, space);
|
|
133
|
+
return [4 /*yield*/, fs.writeFile(path, json, 'utf-8')];
|
|
134
|
+
case 1:
|
|
135
|
+
_a.sent();
|
|
136
|
+
return [2 /*return*/];
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Deletes a file if it exists.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} path - The file path to delete.
|
|
145
|
+
* @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
|
|
146
|
+
*/
|
|
147
|
+
export function deleteFileIfExists(path) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
149
|
+
var err_1;
|
|
150
|
+
return __generator(this, function (_a) {
|
|
151
|
+
switch (_a.label) {
|
|
152
|
+
case 0:
|
|
153
|
+
_a.trys.push([0, 2, , 3]);
|
|
154
|
+
return [4 /*yield*/, fs.unlink(path)];
|
|
155
|
+
case 1:
|
|
156
|
+
_a.sent();
|
|
157
|
+
return [2 /*return*/, true];
|
|
158
|
+
case 2:
|
|
159
|
+
err_1 = _a.sent();
|
|
160
|
+
if (err_1.code === 'ENOENT')
|
|
161
|
+
return [2 /*return*/, true];
|
|
162
|
+
return [2 /*return*/, false];
|
|
163
|
+
case 3: return [2 /*return*/];
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Reads a text file from the specified path.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} path - The path to the text file.
|
|
172
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
173
|
+
* @returns {Promise<string | null>} The file contents, or `null` if reading fails.
|
|
174
|
+
*/
|
|
175
|
+
export function readTextFile(path_1) {
|
|
176
|
+
return __awaiter(this, arguments, void 0, function (path, encoding) {
|
|
177
|
+
var _a;
|
|
178
|
+
if (encoding === void 0) { encoding = 'utf-8'; }
|
|
179
|
+
return __generator(this, function (_b) {
|
|
180
|
+
switch (_b.label) {
|
|
181
|
+
case 0:
|
|
182
|
+
_b.trys.push([0, 2, , 3]);
|
|
183
|
+
return [4 /*yield*/, fs.readFile(path, encoding)];
|
|
184
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
185
|
+
case 2:
|
|
186
|
+
_a = _b.sent();
|
|
187
|
+
return [2 /*return*/, null];
|
|
188
|
+
case 3: return [2 /*return*/];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Writes text content to a file.
|
|
195
|
+
*
|
|
196
|
+
* @param {string} path - The destination file path.
|
|
197
|
+
* @param {string} content - The text content to write.
|
|
198
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
199
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
200
|
+
*/
|
|
201
|
+
export function writeTextFile(path_1, content_1) {
|
|
202
|
+
return __awaiter(this, arguments, void 0, function (path, content, encoding) {
|
|
203
|
+
var _a;
|
|
204
|
+
if (encoding === void 0) { encoding = 'utf-8'; }
|
|
205
|
+
return __generator(this, function (_b) {
|
|
206
|
+
switch (_b.label) {
|
|
207
|
+
case 0:
|
|
208
|
+
_b.trys.push([0, 2, , 3]);
|
|
209
|
+
return [4 /*yield*/, fs.writeFile(path, content, encoding)];
|
|
210
|
+
case 1:
|
|
211
|
+
_b.sent();
|
|
212
|
+
return [2 /*return*/, true];
|
|
213
|
+
case 2:
|
|
214
|
+
_a = _b.sent();
|
|
215
|
+
return [2 /*return*/, false];
|
|
216
|
+
case 3: return [2 /*return*/];
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Appends text content to a file.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} path - The file path to append to.
|
|
225
|
+
* @param {string} content - The text content to append.
|
|
226
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
227
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
228
|
+
*/
|
|
229
|
+
export function appendTextFile(path_1, content_1) {
|
|
230
|
+
return __awaiter(this, arguments, void 0, function (path, content, encoding) {
|
|
231
|
+
var _a;
|
|
232
|
+
if (encoding === void 0) { encoding = 'utf-8'; }
|
|
233
|
+
return __generator(this, function (_b) {
|
|
234
|
+
switch (_b.label) {
|
|
235
|
+
case 0:
|
|
236
|
+
_b.trys.push([0, 2, , 3]);
|
|
237
|
+
return [4 /*yield*/, fs.appendFile(path, content, encoding)];
|
|
238
|
+
case 1:
|
|
239
|
+
_b.sent();
|
|
240
|
+
return [2 /*return*/, true];
|
|
241
|
+
case 2:
|
|
242
|
+
_a = _b.sent();
|
|
243
|
+
return [2 /*return*/, false];
|
|
244
|
+
case 3: return [2 /*return*/];
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Copies a file from source to destination.
|
|
251
|
+
*
|
|
252
|
+
* @param {string} source - Source file path.
|
|
253
|
+
* @param {string} destination - Destination file path.
|
|
254
|
+
* @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
|
|
255
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
256
|
+
*/
|
|
257
|
+
export function copyFile(source_1, destination_1) {
|
|
258
|
+
return __awaiter(this, arguments, void 0, function (source, destination, overwrite) {
|
|
259
|
+
var flags, _a;
|
|
260
|
+
if (overwrite === void 0) { overwrite = false; }
|
|
261
|
+
return __generator(this, function (_b) {
|
|
262
|
+
switch (_b.label) {
|
|
263
|
+
case 0:
|
|
264
|
+
_b.trys.push([0, 2, , 3]);
|
|
265
|
+
flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
|
|
266
|
+
return [4 /*yield*/, fs.copyFile(source, destination, flags)];
|
|
267
|
+
case 1:
|
|
268
|
+
_b.sent();
|
|
269
|
+
return [2 /*return*/, true];
|
|
270
|
+
case 2:
|
|
271
|
+
_a = _b.sent();
|
|
272
|
+
return [2 /*return*/, false];
|
|
273
|
+
case 3: return [2 /*return*/];
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Renames/moves a file.
|
|
280
|
+
*
|
|
281
|
+
* @param {string} oldPath - Current file path.
|
|
282
|
+
* @param {string} newPath - New file path.
|
|
283
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
284
|
+
*/
|
|
285
|
+
export function moveFile(oldPath, newPath) {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
287
|
+
var _a, _b;
|
|
288
|
+
return __generator(this, function (_c) {
|
|
289
|
+
switch (_c.label) {
|
|
290
|
+
case 0:
|
|
291
|
+
_c.trys.push([0, 2, , 8]);
|
|
292
|
+
return [4 /*yield*/, fs.rename(oldPath, newPath)];
|
|
293
|
+
case 1:
|
|
294
|
+
_c.sent();
|
|
295
|
+
return [2 /*return*/, true];
|
|
296
|
+
case 2:
|
|
297
|
+
_a = _c.sent();
|
|
298
|
+
_c.label = 3;
|
|
299
|
+
case 3:
|
|
300
|
+
_c.trys.push([3, 6, , 7]);
|
|
301
|
+
return [4 /*yield*/, fs.copyFile(oldPath, newPath)];
|
|
302
|
+
case 4:
|
|
303
|
+
_c.sent();
|
|
304
|
+
return [4 /*yield*/, fs.unlink(oldPath)];
|
|
305
|
+
case 5:
|
|
306
|
+
_c.sent();
|
|
307
|
+
return [2 /*return*/, true];
|
|
308
|
+
case 6:
|
|
309
|
+
_b = _c.sent();
|
|
310
|
+
return [2 /*return*/, false];
|
|
311
|
+
case 7: return [3 /*break*/, 8];
|
|
312
|
+
case 8: return [2 /*return*/];
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Gets file stats if the file exists.
|
|
319
|
+
*
|
|
320
|
+
* @param {string} path - Path to the file.
|
|
321
|
+
* @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
|
|
322
|
+
*/
|
|
323
|
+
export function getFileStats(path) {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
325
|
+
var _a;
|
|
326
|
+
return __generator(this, function (_b) {
|
|
327
|
+
switch (_b.label) {
|
|
328
|
+
case 0:
|
|
329
|
+
_b.trys.push([0, 2, , 3]);
|
|
330
|
+
return [4 /*yield*/, fs.stat(path)];
|
|
331
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
332
|
+
case 2:
|
|
333
|
+
_a = _b.sent();
|
|
334
|
+
return [2 /*return*/, null];
|
|
335
|
+
case 3: return [2 /*return*/];
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Creates a temporary file with optional content.
|
|
342
|
+
*
|
|
343
|
+
* @param {object} [options] - Options for creating the temp file.
|
|
344
|
+
* @param {string} [options.prefix="tmp-"] - Filename prefix.
|
|
345
|
+
* @param {string} [options.extension=""] - File extension.
|
|
346
|
+
* @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
|
|
347
|
+
* @param {string | Buffer} [options.content] - Optional content to write to the file.
|
|
348
|
+
* @returns {Promise<string>} Path to the created temporary file.
|
|
349
|
+
*/
|
|
350
|
+
export function createTempFile() {
|
|
351
|
+
return __awaiter(this, arguments, void 0, function (_a) {
|
|
352
|
+
var ext, filename, filepath;
|
|
353
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.prefix, prefix = _c === void 0 ? 'tmp-' : _c, _d = _b.extension, extension = _d === void 0 ? '' : _d, _e = _b.dir, dir = _e === void 0 ? tmpdir() : _e, content = _b.content;
|
|
354
|
+
return __generator(this, function (_f) {
|
|
355
|
+
switch (_f.label) {
|
|
356
|
+
case 0:
|
|
357
|
+
ext = extension ? (extension.startsWith('.') ? extension : ".".concat(extension)) : '';
|
|
358
|
+
filename = "".concat(prefix).concat(randomUUID()).concat(ext);
|
|
359
|
+
filepath = path.join(dir, filename);
|
|
360
|
+
if (!content) return [3 /*break*/, 2];
|
|
361
|
+
return [4 /*yield*/, fs.writeFile(filepath, content)];
|
|
362
|
+
case 1:
|
|
363
|
+
_f.sent();
|
|
364
|
+
return [3 /*break*/, 4];
|
|
365
|
+
case 2: return [4 /*yield*/, fs.writeFile(filepath, '')];
|
|
366
|
+
case 3:
|
|
367
|
+
_f.sent();
|
|
368
|
+
_f.label = 4;
|
|
369
|
+
case 4: return [2 /*return*/, filepath];
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Streams a file from source to destination.
|
|
376
|
+
* Useful for large files to avoid loading the entire file into memory.
|
|
377
|
+
*
|
|
378
|
+
* @param {string} source - Source file path.
|
|
379
|
+
* @param {string} destination - Destination file path.
|
|
380
|
+
* @returns {Promise<void>} Resolves when streaming completes.
|
|
381
|
+
* @throws {Error} If streaming fails.
|
|
382
|
+
*/
|
|
383
|
+
export function streamFile(source, destination) {
|
|
384
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
385
|
+
var readStream, writeStream, error_1;
|
|
386
|
+
return __generator(this, function (_a) {
|
|
387
|
+
switch (_a.label) {
|
|
388
|
+
case 0:
|
|
389
|
+
readStream = createReadStream(source);
|
|
390
|
+
writeStream = createWriteStream(destination);
|
|
391
|
+
_a.label = 1;
|
|
392
|
+
case 1:
|
|
393
|
+
_a.trys.push([1, 3, , 4]);
|
|
394
|
+
return [4 /*yield*/, pipeline(readStream, writeStream)];
|
|
395
|
+
case 2:
|
|
396
|
+
_a.sent();
|
|
397
|
+
return [3 /*break*/, 4];
|
|
398
|
+
case 3:
|
|
399
|
+
error_1 = _a.sent();
|
|
400
|
+
// Ensure both streams are closed in case of error
|
|
401
|
+
readStream.destroy();
|
|
402
|
+
writeStream.destroy();
|
|
403
|
+
throw error_1;
|
|
404
|
+
case 4: return [2 /*return*/];
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Reads a directory and returns file names.
|
|
411
|
+
*
|
|
412
|
+
* @param {string} dirPath - Path to the directory.
|
|
413
|
+
* @param {object} [options] - Options for reading the directory.
|
|
414
|
+
* @param {boolean} [options.fullPaths=false] - Whether to return full paths.
|
|
415
|
+
* @param {RegExp} [options.filter] - Optional regex to filter files.
|
|
416
|
+
* @returns {Promise<string[]>} Array of file names or paths.
|
|
417
|
+
* @throws {Error} If directory cannot be read.
|
|
418
|
+
*/
|
|
419
|
+
export function readDirectory(dirPath_1) {
|
|
420
|
+
return __awaiter(this, arguments, void 0, function (dirPath, options) {
|
|
421
|
+
var files, result;
|
|
422
|
+
if (options === void 0) { options = {}; }
|
|
423
|
+
return __generator(this, function (_a) {
|
|
424
|
+
switch (_a.label) {
|
|
425
|
+
case 0: return [4 /*yield*/, fs.readdir(dirPath)];
|
|
426
|
+
case 1:
|
|
427
|
+
files = _a.sent();
|
|
428
|
+
result = files;
|
|
429
|
+
if (options.filter) {
|
|
430
|
+
result = result.filter(function (file) { return options.filter.test(file); });
|
|
431
|
+
}
|
|
432
|
+
if (options.fullPaths) {
|
|
433
|
+
result = result.map(function (file) { return path.join(dirPath, file); });
|
|
434
|
+
}
|
|
435
|
+
return [2 /*return*/, result];
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Creates a directory if it doesn't exist.
|
|
442
|
+
*
|
|
443
|
+
* @param {string} dirPath - Path to the directory.
|
|
444
|
+
* @param {boolean} [recursive=true] - Whether to create parent directories.
|
|
445
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
446
|
+
*/
|
|
447
|
+
export function createDirectory(dirPath_1) {
|
|
448
|
+
return __awaiter(this, arguments, void 0, function (dirPath, recursive) {
|
|
449
|
+
var err_2;
|
|
450
|
+
if (recursive === void 0) { recursive = true; }
|
|
451
|
+
return __generator(this, function (_a) {
|
|
452
|
+
switch (_a.label) {
|
|
453
|
+
case 0:
|
|
454
|
+
_a.trys.push([0, 2, , 3]);
|
|
455
|
+
return [4 /*yield*/, fs.mkdir(dirPath, { recursive: recursive })];
|
|
456
|
+
case 1:
|
|
457
|
+
_a.sent();
|
|
458
|
+
return [2 /*return*/, true];
|
|
459
|
+
case 2:
|
|
460
|
+
err_2 = _a.sent();
|
|
461
|
+
// Consider it success if directory already exists
|
|
462
|
+
return [2 /*return*/, err_2.code === 'EEXIST'];
|
|
463
|
+
case 3: return [2 /*return*/];
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Safely reads and parses a JSON file with error details.
|
|
470
|
+
*
|
|
471
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
472
|
+
* @param {string} path - The path to the JSON file.
|
|
473
|
+
* @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
|
|
474
|
+
*/
|
|
475
|
+
export function safeReadJsonFile(path) {
|
|
476
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
477
|
+
var content, data, error_2;
|
|
478
|
+
return __generator(this, function (_a) {
|
|
479
|
+
switch (_a.label) {
|
|
480
|
+
case 0:
|
|
481
|
+
_a.trys.push([0, 2, , 3]);
|
|
482
|
+
return [4 /*yield*/, fs.readFile(path, 'utf-8')];
|
|
483
|
+
case 1:
|
|
484
|
+
content = _a.sent();
|
|
485
|
+
try {
|
|
486
|
+
data = JSON.parse(content);
|
|
487
|
+
return [2 /*return*/, { data: data, error: null }];
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
return [2 /*return*/, {
|
|
491
|
+
data: null,
|
|
492
|
+
error: new Error("Invalid JSON in file ".concat(path, ": ").concat(error.message))
|
|
493
|
+
}];
|
|
494
|
+
}
|
|
495
|
+
return [3 /*break*/, 3];
|
|
496
|
+
case 2:
|
|
497
|
+
error_2 = _a.sent();
|
|
498
|
+
return [2 /*return*/, {
|
|
499
|
+
data: null,
|
|
500
|
+
error: new Error("Failed to read file ".concat(path, ": ").concat(error_2.message))
|
|
501
|
+
}];
|
|
502
|
+
case 3: return [2 /*return*/];
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Checks if a path points to a file (not a directory).
|
|
509
|
+
*
|
|
510
|
+
* @param {string} path - Path to check.
|
|
511
|
+
* @returns {Promise<boolean>} True if the path is a file, false otherwise.
|
|
512
|
+
*/
|
|
513
|
+
export function isFile(path) {
|
|
514
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
515
|
+
var stat, _a;
|
|
516
|
+
return __generator(this, function (_b) {
|
|
517
|
+
switch (_b.label) {
|
|
518
|
+
case 0:
|
|
519
|
+
_b.trys.push([0, 2, , 3]);
|
|
520
|
+
return [4 /*yield*/, fs.stat(path)];
|
|
521
|
+
case 1:
|
|
522
|
+
stat = _b.sent();
|
|
523
|
+
return [2 /*return*/, stat.isFile()];
|
|
524
|
+
case 2:
|
|
525
|
+
_a = _b.sent();
|
|
526
|
+
return [2 /*return*/, false];
|
|
527
|
+
case 3: return [2 /*return*/];
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Gets the size of a file in bytes.
|
|
534
|
+
*
|
|
535
|
+
* @param {string} path - Path to the file.
|
|
536
|
+
* @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
|
|
537
|
+
*/
|
|
538
|
+
export function getFileSize(path) {
|
|
539
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
540
|
+
var stat, _a;
|
|
541
|
+
return __generator(this, function (_b) {
|
|
542
|
+
switch (_b.label) {
|
|
543
|
+
case 0:
|
|
544
|
+
_b.trys.push([0, 2, , 3]);
|
|
545
|
+
return [4 /*yield*/, fs.stat(path)];
|
|
546
|
+
case 1:
|
|
547
|
+
stat = _b.sent();
|
|
548
|
+
return [2 /*return*/, stat.size];
|
|
549
|
+
case 2:
|
|
550
|
+
_a = _b.sent();
|
|
551
|
+
return [2 /*return*/, -1];
|
|
552
|
+
case 3: return [2 /*return*/];
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Reads a file as a Buffer.
|
|
559
|
+
*
|
|
560
|
+
* @param {string} path - Path to the file.
|
|
561
|
+
* @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
|
|
562
|
+
*/
|
|
563
|
+
export function readFileBuffer(path) {
|
|
564
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
565
|
+
var _a;
|
|
566
|
+
return __generator(this, function (_b) {
|
|
567
|
+
switch (_b.label) {
|
|
568
|
+
case 0:
|
|
569
|
+
_b.trys.push([0, 2, , 3]);
|
|
570
|
+
return [4 /*yield*/, fs.readFile(path)];
|
|
571
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
572
|
+
case 2:
|
|
573
|
+
_a = _b.sent();
|
|
574
|
+
return [2 /*return*/, null];
|
|
575
|
+
case 3: return [2 /*return*/];
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
//# sourceMappingURL=fs.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGH,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAS,MAAM,IAAI,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,UAAgB,UAAU,CAAC,IAAY;;;;;;;oBAEzC,qBAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAA;;oBAArB,SAAqB,CAAC;oBACtB,sBAAO,IAAI,EAAC;;;oBAEZ,sBAAO,KAAK,EAAC;;;;;CAEhB;AAED;;;;;;GAMG;AACH,MAAM,UAAgB,YAAY,CAAU,IAAY;;;;;;;oBAEvC,qBAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,EAAA;;oBAAvC,IAAI,GAAG,SAAgC;oBAC7C,sBAAO,IAAI,CA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C,OAAO,CAAM,CAAC;wBACtC,sBAAO,EAAE,IAAI,MAAA,EAAE,KAAK,EAAE,IAAI,EAAE,EAAC;oBAC/B,CAAC;oBAAC,OAAO,KAAU,EAAE,CAAC;wBACpB,sBAAO;gCACL,IAAI,EAAE,IAAI;gCACV,KAAK,EAAE,IAAI,KAAK,CAAC,+BAAwB,IAAI,eAAK,KAAK,CAAC,OAAO,CAAE,CAAC;6BACnE,EAAC;oBACJ,CAAC;;;;oBAED,sBAAO;4BACL,IAAI,EAAE,IAAI;4BACV,KAAK,EAAE,IAAI,KAAK,CAAC,8BAAuB,IAAI,eAAK,OAAK,CAAC,OAAO,CAAE,CAAC;yBAClE,EAAC;;;;;CAEL;AAED;;;;;GAKG;AACH,MAAM,UAAgB,MAAM,CAAC,IAAY;;;;;;;oBAExB,qBAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAA;;oBAA1B,IAAI,GAAG,SAAmB;oBAChC,sBAAO,IAAI,CAAC,MAAM,EAAE,EAAC;;;oBAErB,sBAAO,KAAK,EAAC;;;;;CAEhB;AAED;;;;;GAKG;AACH,MAAM,UAAgB,WAAW,CAAC,IAAY;;;;;;;oBAE7B,qBAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAA;;oBAA1B,IAAI,GAAG,SAAmB;oBAChC,sBAAO,IAAI,CAAC,IAAI,EAAC;;;oBAEjB,sBAAO,CAAC,CAAC,EAAC;;;;;CAEb;AAED;;;;;GAKG;AACH,MAAM,UAAgB,cAAc,CAAC,IAAY;;;;;;;oBAEtC,qBAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAA;wBAA9B,sBAAO,SAAuB,EAAC;;;oBAE/B,sBAAO,IAAI,EAAC;;;;;CAEf","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { BufferEncoding } from './crypto.utils';\nimport fs from 'fs/promises';\nimport { createReadStream, createWriteStream, Stats } from 'fs';\nimport { tmpdir } from 'os';\nimport path from 'path';\nimport { pipeline } from 'stream/promises';\nimport { randomUUID } from 'crypto';\n\n/**\n * Checks whether a file or directory exists at the given path.\n *\n * @param {string} path - The file or directory path to check.\n * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fs.access(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Reads and parses a JSON file from the specified path.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.\n */\nexport async function readJsonFile<T = any>(path: string): Promise<T | null> {\n try {\n const data = await fs.readFile(path, 'utf-8');\n return JSON.parse(data) as T;\n } catch {\n return null;\n }\n}\n\n/**\n * Writes a JavaScript object to a file as formatted JSON.\n *\n * @param {string} path - The destination file path.\n * @param {any} data - The object to serialize and write.\n * @param {number} [space=2] - Number of spaces for JSON formatting.\n * @returns {Promise<void>} Resolves when writing is complete.\n */\nexport async function writeJsonFile(path: string, data: any, space = 2): Promise<void> {\n const json = JSON.stringify(data, null, space);\n await fs.writeFile(path, json, 'utf-8');\n}\n\n/**\n * Deletes a file if it exists.\n *\n * @param {string} path - The file path to delete.\n * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.\n */\nexport async function deleteFileIfExists(path: string): Promise<boolean> {\n try {\n await fs.unlink(path);\n return true;\n } catch (err: any) {\n if (err.code === 'ENOENT') return true;\n return false;\n }\n}\n\n/**\n * Reads a text file from the specified path.\n *\n * @param {string} path - The path to the text file.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<string | null>} The file contents, or `null` if reading fails.\n */\nexport async function readTextFile(path: string, encoding: BufferEncoding = 'utf-8'): Promise<string | null> {\n try {\n return await fs.readFile(path, encoding);\n } catch {\n return null;\n }\n}\n\n/**\n * Writes text content to a file.\n *\n * @param {string} path - The destination file path.\n * @param {string} content - The text content to write.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function writeTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = 'utf-8'\n): Promise<boolean> {\n try {\n await fs.writeFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Appends text content to a file.\n *\n * @param {string} path - The file path to append to.\n * @param {string} content - The text content to append.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function appendTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = 'utf-8'\n): Promise<boolean> {\n try {\n await fs.appendFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Copies a file from source to destination.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function copyFile(source: string, destination: string, overwrite = false): Promise<boolean> {\n try {\n const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;\n await fs.copyFile(source, destination, flags);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Renames/moves a file.\n *\n * @param {string} oldPath - Current file path.\n * @param {string} newPath - New file path.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function moveFile(oldPath: string, newPath: string): Promise<boolean> {\n try {\n await fs.rename(oldPath, newPath);\n return true;\n } catch {\n // If rename fails (e.g., across devices), try copy+delete\n try {\n await fs.copyFile(oldPath, newPath);\n await fs.unlink(oldPath);\n return true;\n } catch {\n return false;\n }\n }\n}\n\n/**\n * Gets file stats if the file exists.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.\n */\nexport async function getFileStats(path: string): Promise<Stats | null> {\n try {\n return await fs.stat(path);\n } catch {\n return null;\n }\n}\n\n/**\n * Creates a temporary file with optional content.\n *\n * @param {object} [options] - Options for creating the temp file.\n * @param {string} [options.prefix=\"tmp-\"] - Filename prefix.\n * @param {string} [options.extension=\"\"] - File extension.\n * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).\n * @param {string | Buffer} [options.content] - Optional content to write to the file.\n * @returns {Promise<string>} Path to the created temporary file.\n */\nexport async function createTempFile({\n prefix = 'tmp-',\n extension = '',\n dir = tmpdir(),\n content\n}: {\n prefix?: string;\n extension?: string;\n dir?: string;\n content?: string | Buffer;\n} = {}): Promise<string> {\n const ext = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : '';\n const filename = `${prefix}${randomUUID()}${ext}`;\n const filepath = path.join(dir, filename);\n\n if (content) {\n await fs.writeFile(filepath, content);\n } else {\n await fs.writeFile(filepath, '');\n }\n\n return filepath;\n}\n\n/**\n * Streams a file from source to destination.\n * Useful for large files to avoid loading the entire file into memory.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @returns {Promise<void>} Resolves when streaming completes.\n * @throws {Error} If streaming fails.\n */\nexport async function streamFile(source: string, destination: string): Promise<void> {\n const readStream = createReadStream(source);\n const writeStream = createWriteStream(destination);\n\n try {\n await pipeline(readStream, writeStream);\n } catch (error) {\n // Ensure both streams are closed in case of error\n readStream.destroy();\n writeStream.destroy();\n throw error;\n }\n}\n\n/**\n * Reads a directory and returns file names.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {object} [options] - Options for reading the directory.\n * @param {boolean} [options.fullPaths=false] - Whether to return full paths.\n * @param {RegExp} [options.filter] - Optional regex to filter files.\n * @returns {Promise<string[]>} Array of file names or paths.\n * @throws {Error} If directory cannot be read.\n */\nexport async function readDirectory(\n dirPath: string,\n options: { fullPaths?: boolean; filter?: RegExp } = {}\n): Promise<string[]> {\n const files = await fs.readdir(dirPath);\n\n let result = files;\n\n if (options.filter) {\n result = result.filter(file => options.filter!.test(file));\n }\n\n if (options.fullPaths) {\n result = result.map(file => path.join(dirPath, file));\n }\n\n return result;\n}\n\n/**\n * Creates a directory if it doesn't exist.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {boolean} [recursive=true] - Whether to create parent directories.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function createDirectory(dirPath: string, recursive = true): Promise<boolean> {\n try {\n await fs.mkdir(dirPath, { recursive });\n return true;\n } catch (err: any) {\n // Consider it success if directory already exists\n return err.code === 'EEXIST';\n }\n}\n\n/**\n * Safely reads and parses a JSON file with error details.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.\n */\nexport async function safeReadJsonFile<T = any>(path: string): Promise<{ data: T | null; error: Error | null }> {\n try {\n const content = await fs.readFile(path, 'utf-8');\n try {\n const data = JSON.parse(content) as T;\n return { data, error: null };\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Invalid JSON in file ${path}: ${error.message}`)\n };\n }\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Failed to read file ${path}: ${error.message}`)\n };\n }\n}\n\n/**\n * Checks if a path points to a file (not a directory).\n *\n * @param {string} path - Path to check.\n * @returns {Promise<boolean>} True if the path is a file, false otherwise.\n */\nexport async function isFile(path: string): Promise<boolean> {\n try {\n const stat = await fs.stat(path);\n return stat.isFile();\n } catch {\n return false;\n }\n}\n\n/**\n * Gets the size of a file in bytes.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.\n */\nexport async function getFileSize(path: string): Promise<number> {\n try {\n const stat = await fs.stat(path);\n return stat.size;\n } catch {\n return -1;\n }\n}\n\n/**\n * Reads a file as a Buffer.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.\n */\nexport async function readFileBuffer(path: string): Promise<Buffer | null> {\n try {\n return await fs.readFile(path);\n } catch {\n return null;\n }\n}\n"]}
|