@catbee/utils 1.0.5 → 2.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7579
- package/build/index.d.ts +0 -5774
- package/build/index.mjs +0 -7274
package/fs/index.cjs
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var fs$1 = require('fs');
|
|
28
|
+
var fs = require('fs/promises');
|
|
29
|
+
var os = require('os');
|
|
30
|
+
var path = require('path');
|
|
31
|
+
var promises = require('stream/promises');
|
|
32
|
+
var crypto = require('crypto');
|
|
33
|
+
|
|
34
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
35
|
+
|
|
36
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
37
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
38
|
+
|
|
39
|
+
var __defProp = Object.defineProperty;
|
|
40
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
41
|
+
async function fileExists(path2) {
|
|
42
|
+
try {
|
|
43
|
+
await fs__default.default.access(path2);
|
|
44
|
+
return true;
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
__name(fileExists, "fileExists");
|
|
50
|
+
async function readJsonFile(path2) {
|
|
51
|
+
try {
|
|
52
|
+
const data = await fs__default.default.readFile(path2, "utf-8");
|
|
53
|
+
return JSON.parse(data);
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
__name(readJsonFile, "readJsonFile");
|
|
59
|
+
async function readJsonOrDefault(path2, defaultValue) {
|
|
60
|
+
const res = await safeReadJsonFile(path2);
|
|
61
|
+
return res.data === null ? defaultValue : res.data;
|
|
62
|
+
}
|
|
63
|
+
__name(readJsonOrDefault, "readJsonOrDefault");
|
|
64
|
+
async function writeJsonFile(path2, data, space = 2) {
|
|
65
|
+
const json = JSON.stringify(data, null, space);
|
|
66
|
+
await fs__default.default.writeFile(path2, json, "utf-8");
|
|
67
|
+
}
|
|
68
|
+
__name(writeJsonFile, "writeJsonFile");
|
|
69
|
+
async function deleteFileIfExists(path2) {
|
|
70
|
+
try {
|
|
71
|
+
await fs__default.default.unlink(path2);
|
|
72
|
+
return true;
|
|
73
|
+
} catch (err) {
|
|
74
|
+
if (err.code === "ENOENT") return true;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
__name(deleteFileIfExists, "deleteFileIfExists");
|
|
79
|
+
async function readTextFile(path2, encoding = "utf-8") {
|
|
80
|
+
try {
|
|
81
|
+
return await fs__default.default.readFile(path2, encoding);
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
__name(readTextFile, "readTextFile");
|
|
87
|
+
async function writeTextFile(path2, content, encoding = "utf-8") {
|
|
88
|
+
try {
|
|
89
|
+
await fs__default.default.writeFile(path2, content, encoding);
|
|
90
|
+
return true;
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
__name(writeTextFile, "writeTextFile");
|
|
96
|
+
async function appendTextFile(path2, content, encoding = "utf-8") {
|
|
97
|
+
try {
|
|
98
|
+
await fs__default.default.appendFile(path2, content, encoding);
|
|
99
|
+
return true;
|
|
100
|
+
} catch {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
__name(appendTextFile, "appendTextFile");
|
|
105
|
+
async function copyFile(source, destination, overwrite = false) {
|
|
106
|
+
try {
|
|
107
|
+
const flags = overwrite ? 0 : fs__default.default.constants.COPYFILE_EXCL;
|
|
108
|
+
await fs__default.default.copyFile(source, destination, flags);
|
|
109
|
+
return true;
|
|
110
|
+
} catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
__name(copyFile, "copyFile");
|
|
115
|
+
async function moveFile(oldPath, newPath) {
|
|
116
|
+
try {
|
|
117
|
+
await fs__default.default.rename(oldPath, newPath);
|
|
118
|
+
return true;
|
|
119
|
+
} catch {
|
|
120
|
+
try {
|
|
121
|
+
await fs__default.default.copyFile(oldPath, newPath);
|
|
122
|
+
await fs__default.default.unlink(oldPath);
|
|
123
|
+
return true;
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
__name(moveFile, "moveFile");
|
|
130
|
+
async function getFileStats(path2) {
|
|
131
|
+
try {
|
|
132
|
+
return await fs__default.default.stat(path2);
|
|
133
|
+
} catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
__name(getFileStats, "getFileStats");
|
|
138
|
+
async function createTempFile({ prefix = "tmp-", extension = "", dir = os.tmpdir(), content } = {}) {
|
|
139
|
+
const ext = extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
|
|
140
|
+
const filename = `${prefix}${crypto.randomUUID()}${ext}`;
|
|
141
|
+
const filepath = path__default.default.join(dir, filename);
|
|
142
|
+
if (content) {
|
|
143
|
+
await fs__default.default.writeFile(filepath, content);
|
|
144
|
+
} else {
|
|
145
|
+
await fs__default.default.writeFile(filepath, "");
|
|
146
|
+
}
|
|
147
|
+
return filepath;
|
|
148
|
+
}
|
|
149
|
+
__name(createTempFile, "createTempFile");
|
|
150
|
+
async function streamFile(source, destination) {
|
|
151
|
+
const readStream = fs$1.createReadStream(source);
|
|
152
|
+
const writeStream = fs$1.createWriteStream(destination);
|
|
153
|
+
try {
|
|
154
|
+
await promises.pipeline(readStream, writeStream);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
readStream.destroy();
|
|
157
|
+
writeStream.destroy();
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
__name(streamFile, "streamFile");
|
|
162
|
+
async function readDirectory(dirPath, options = {}) {
|
|
163
|
+
const files = await fs__default.default.readdir(dirPath);
|
|
164
|
+
let result = files;
|
|
165
|
+
if (options.filter) {
|
|
166
|
+
result = result.filter((file) => options.filter.test(file));
|
|
167
|
+
}
|
|
168
|
+
if (options.fullPaths) {
|
|
169
|
+
result = result.map((file) => path__default.default.join(dirPath, file));
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
__name(readDirectory, "readDirectory");
|
|
174
|
+
async function createDirectory(dirPath, recursive = true) {
|
|
175
|
+
try {
|
|
176
|
+
await fs__default.default.mkdir(dirPath, {
|
|
177
|
+
recursive
|
|
178
|
+
});
|
|
179
|
+
return true;
|
|
180
|
+
} catch (err) {
|
|
181
|
+
return err.code === "EEXIST";
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
__name(createDirectory, "createDirectory");
|
|
185
|
+
async function safeReadJsonFile(path2) {
|
|
186
|
+
try {
|
|
187
|
+
const content = await fs__default.default.readFile(path2, "utf-8");
|
|
188
|
+
try {
|
|
189
|
+
const data = JSON.parse(content);
|
|
190
|
+
return {
|
|
191
|
+
data,
|
|
192
|
+
error: null
|
|
193
|
+
};
|
|
194
|
+
} catch (error) {
|
|
195
|
+
return {
|
|
196
|
+
data: null,
|
|
197
|
+
error: new Error(`Invalid JSON in file ${path2}: ${error.message}`)
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
} catch (error) {
|
|
201
|
+
return {
|
|
202
|
+
data: null,
|
|
203
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
__name(safeReadJsonFile, "safeReadJsonFile");
|
|
208
|
+
async function isFile(path2) {
|
|
209
|
+
try {
|
|
210
|
+
const stat = await fs__default.default.stat(path2);
|
|
211
|
+
return stat.isFile();
|
|
212
|
+
} catch {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
__name(isFile, "isFile");
|
|
217
|
+
async function getFileSize(path2) {
|
|
218
|
+
try {
|
|
219
|
+
const stat = await fs__default.default.stat(path2);
|
|
220
|
+
return stat.size;
|
|
221
|
+
} catch {
|
|
222
|
+
return -1;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
__name(getFileSize, "getFileSize");
|
|
226
|
+
async function readFileBuffer(path2) {
|
|
227
|
+
try {
|
|
228
|
+
return await fs__default.default.readFile(path2);
|
|
229
|
+
} catch {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
__name(readFileBuffer, "readFileBuffer");
|
|
234
|
+
|
|
235
|
+
exports.appendTextFile = appendTextFile;
|
|
236
|
+
exports.copyFile = copyFile;
|
|
237
|
+
exports.createDirectory = createDirectory;
|
|
238
|
+
exports.createTempFile = createTempFile;
|
|
239
|
+
exports.deleteFileIfExists = deleteFileIfExists;
|
|
240
|
+
exports.fileExists = fileExists;
|
|
241
|
+
exports.getFileSize = getFileSize;
|
|
242
|
+
exports.getFileStats = getFileStats;
|
|
243
|
+
exports.isFile = isFile;
|
|
244
|
+
exports.moveFile = moveFile;
|
|
245
|
+
exports.readDirectory = readDirectory;
|
|
246
|
+
exports.readFileBuffer = readFileBuffer;
|
|
247
|
+
exports.readJsonFile = readJsonFile;
|
|
248
|
+
exports.readJsonOrDefault = readJsonOrDefault;
|
|
249
|
+
exports.readTextFile = readTextFile;
|
|
250
|
+
exports.safeReadJsonFile = safeReadJsonFile;
|
|
251
|
+
exports.streamFile = streamFile;
|
|
252
|
+
exports.writeJsonFile = writeJsonFile;
|
|
253
|
+
exports.writeTextFile = writeTextFile;
|
package/fs/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
export * from './fs.utils';
|
package/fs/index.mjs
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
import { createReadStream, createWriteStream } from 'fs';
|
|
26
|
+
import fs from 'fs/promises';
|
|
27
|
+
import { tmpdir } from 'os';
|
|
28
|
+
import path from 'path';
|
|
29
|
+
import { pipeline } from 'stream/promises';
|
|
30
|
+
import { randomUUID } from 'crypto';
|
|
31
|
+
|
|
32
|
+
var __defProp = Object.defineProperty;
|
|
33
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
34
|
+
async function fileExists(path2) {
|
|
35
|
+
try {
|
|
36
|
+
await fs.access(path2);
|
|
37
|
+
return true;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
__name(fileExists, "fileExists");
|
|
43
|
+
async function readJsonFile(path2) {
|
|
44
|
+
try {
|
|
45
|
+
const data = await fs.readFile(path2, "utf-8");
|
|
46
|
+
return JSON.parse(data);
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
__name(readJsonFile, "readJsonFile");
|
|
52
|
+
async function readJsonOrDefault(path2, defaultValue) {
|
|
53
|
+
const res = await safeReadJsonFile(path2);
|
|
54
|
+
return res.data === null ? defaultValue : res.data;
|
|
55
|
+
}
|
|
56
|
+
__name(readJsonOrDefault, "readJsonOrDefault");
|
|
57
|
+
async function writeJsonFile(path2, data, space = 2) {
|
|
58
|
+
const json = JSON.stringify(data, null, space);
|
|
59
|
+
await fs.writeFile(path2, json, "utf-8");
|
|
60
|
+
}
|
|
61
|
+
__name(writeJsonFile, "writeJsonFile");
|
|
62
|
+
async function deleteFileIfExists(path2) {
|
|
63
|
+
try {
|
|
64
|
+
await fs.unlink(path2);
|
|
65
|
+
return true;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (err.code === "ENOENT") return true;
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
__name(deleteFileIfExists, "deleteFileIfExists");
|
|
72
|
+
async function readTextFile(path2, encoding = "utf-8") {
|
|
73
|
+
try {
|
|
74
|
+
return await fs.readFile(path2, encoding);
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
__name(readTextFile, "readTextFile");
|
|
80
|
+
async function writeTextFile(path2, content, encoding = "utf-8") {
|
|
81
|
+
try {
|
|
82
|
+
await fs.writeFile(path2, content, encoding);
|
|
83
|
+
return true;
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
__name(writeTextFile, "writeTextFile");
|
|
89
|
+
async function appendTextFile(path2, content, encoding = "utf-8") {
|
|
90
|
+
try {
|
|
91
|
+
await fs.appendFile(path2, content, encoding);
|
|
92
|
+
return true;
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
__name(appendTextFile, "appendTextFile");
|
|
98
|
+
async function copyFile(source, destination, overwrite = false) {
|
|
99
|
+
try {
|
|
100
|
+
const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
|
|
101
|
+
await fs.copyFile(source, destination, flags);
|
|
102
|
+
return true;
|
|
103
|
+
} catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
__name(copyFile, "copyFile");
|
|
108
|
+
async function moveFile(oldPath, newPath) {
|
|
109
|
+
try {
|
|
110
|
+
await fs.rename(oldPath, newPath);
|
|
111
|
+
return true;
|
|
112
|
+
} catch {
|
|
113
|
+
try {
|
|
114
|
+
await fs.copyFile(oldPath, newPath);
|
|
115
|
+
await fs.unlink(oldPath);
|
|
116
|
+
return true;
|
|
117
|
+
} catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
__name(moveFile, "moveFile");
|
|
123
|
+
async function getFileStats(path2) {
|
|
124
|
+
try {
|
|
125
|
+
return await fs.stat(path2);
|
|
126
|
+
} catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
__name(getFileStats, "getFileStats");
|
|
131
|
+
async function createTempFile({ prefix = "tmp-", extension = "", dir = tmpdir(), content } = {}) {
|
|
132
|
+
const ext = extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
|
|
133
|
+
const filename = `${prefix}${randomUUID()}${ext}`;
|
|
134
|
+
const filepath = path.join(dir, filename);
|
|
135
|
+
if (content) {
|
|
136
|
+
await fs.writeFile(filepath, content);
|
|
137
|
+
} else {
|
|
138
|
+
await fs.writeFile(filepath, "");
|
|
139
|
+
}
|
|
140
|
+
return filepath;
|
|
141
|
+
}
|
|
142
|
+
__name(createTempFile, "createTempFile");
|
|
143
|
+
async function streamFile(source, destination) {
|
|
144
|
+
const readStream = createReadStream(source);
|
|
145
|
+
const writeStream = createWriteStream(destination);
|
|
146
|
+
try {
|
|
147
|
+
await pipeline(readStream, writeStream);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
readStream.destroy();
|
|
150
|
+
writeStream.destroy();
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
__name(streamFile, "streamFile");
|
|
155
|
+
async function readDirectory(dirPath, options = {}) {
|
|
156
|
+
const files = await fs.readdir(dirPath);
|
|
157
|
+
let result = files;
|
|
158
|
+
if (options.filter) {
|
|
159
|
+
result = result.filter((file) => options.filter.test(file));
|
|
160
|
+
}
|
|
161
|
+
if (options.fullPaths) {
|
|
162
|
+
result = result.map((file) => path.join(dirPath, file));
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
__name(readDirectory, "readDirectory");
|
|
167
|
+
async function createDirectory(dirPath, recursive = true) {
|
|
168
|
+
try {
|
|
169
|
+
await fs.mkdir(dirPath, {
|
|
170
|
+
recursive
|
|
171
|
+
});
|
|
172
|
+
return true;
|
|
173
|
+
} catch (err) {
|
|
174
|
+
return err.code === "EEXIST";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
__name(createDirectory, "createDirectory");
|
|
178
|
+
async function safeReadJsonFile(path2) {
|
|
179
|
+
try {
|
|
180
|
+
const content = await fs.readFile(path2, "utf-8");
|
|
181
|
+
try {
|
|
182
|
+
const data = JSON.parse(content);
|
|
183
|
+
return {
|
|
184
|
+
data,
|
|
185
|
+
error: null
|
|
186
|
+
};
|
|
187
|
+
} catch (error) {
|
|
188
|
+
return {
|
|
189
|
+
data: null,
|
|
190
|
+
error: new Error(`Invalid JSON in file ${path2}: ${error.message}`)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
} catch (error) {
|
|
194
|
+
return {
|
|
195
|
+
data: null,
|
|
196
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
__name(safeReadJsonFile, "safeReadJsonFile");
|
|
201
|
+
async function isFile(path2) {
|
|
202
|
+
try {
|
|
203
|
+
const stat = await fs.stat(path2);
|
|
204
|
+
return stat.isFile();
|
|
205
|
+
} catch {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
__name(isFile, "isFile");
|
|
210
|
+
async function getFileSize(path2) {
|
|
211
|
+
try {
|
|
212
|
+
const stat = await fs.stat(path2);
|
|
213
|
+
return stat.size;
|
|
214
|
+
} catch {
|
|
215
|
+
return -1;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
__name(getFileSize, "getFileSize");
|
|
219
|
+
async function readFileBuffer(path2) {
|
|
220
|
+
try {
|
|
221
|
+
return await fs.readFile(path2);
|
|
222
|
+
} catch {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
__name(readFileBuffer, "readFileBuffer");
|
|
227
|
+
|
|
228
|
+
export { appendTextFile, copyFile, createDirectory, createTempFile, deleteFileIfExists, fileExists, getFileSize, getFileStats, isFile, moveFile, readDirectory, readFileBuffer, readJsonFile, readJsonOrDefault, readTextFile, safeReadJsonFile, streamFile, writeJsonFile, writeTextFile };
|