@catbee/utils 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +303 -107
- package/build/esm/config.d.ts +12 -0
- package/build/esm/config.js +12 -0
- package/build/esm/config.js.map +1 -1
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +1 -0
- package/build/esm/index.js.map +1 -1
- package/build/esm/types/api-response.d.ts +82 -0
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/utils/array.utils.d.ts +64 -1
- package/build/esm/utils/array.utils.js +139 -2
- package/build/esm/utils/array.utils.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +60 -1
- package/build/esm/utils/async.utils.js +160 -2
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/cache.utils.d.ts +67 -4
- package/build/esm/utils/cache.utils.js +210 -28
- package/build/esm/utils/cache.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +101 -0
- package/build/esm/utils/context-store.utils.js +182 -0
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/crypto.utils.d.ts +110 -8
- package/build/esm/utils/crypto.utils.js +298 -15
- package/build/esm/utils/crypto.utils.js.map +1 -1
- package/build/esm/utils/decorators.utils.d.ts +40 -0
- package/build/esm/utils/decorators.utils.js +312 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +121 -0
- package/build/esm/utils/dir.utils.js +577 -0
- package/build/esm/utils/dir.utils.js.map +1 -1
- package/build/esm/utils/env.utils.d.ts +83 -0
- package/build/esm/utils/env.utils.js +233 -0
- package/build/esm/utils/env.utils.js.map +1 -1
- package/build/esm/utils/exception.utils.d.ts +116 -0
- package/build/esm/utils/exception.utils.js +322 -0
- package/build/esm/utils/exception.utils.js.map +1 -1
- package/build/esm/utils/fs.utils.d.ts +132 -0
- package/build/esm/utils/fs.utils.js +421 -0
- package/build/esm/utils/fs.utils.js.map +1 -1
- package/build/esm/utils/http-status-codes.d.ts +87 -178
- package/build/esm/utils/http-status-codes.js +92 -178
- package/build/esm/utils/http-status-codes.js.map +1 -1
- package/build/esm/utils/id.utils.d.ts +11 -10
- package/build/esm/utils/id.utils.js +29 -20
- package/build/esm/utils/id.utils.js.map +1 -1
- package/build/esm/utils/logger.utils.d.ts +24 -0
- package/build/esm/utils/logger.utils.js +54 -0
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.d.ts +72 -0
- package/build/esm/utils/middleware.utils.js +158 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +65 -3
- package/build/esm/utils/obj.utils.js +205 -6
- package/build/esm/utils/obj.utils.js.map +1 -1
- package/build/esm/utils/response.utils.d.ts +101 -0
- package/build/esm/utils/response.utils.js +153 -0
- package/build/esm/utils/response.utils.js.map +1 -1
- package/build/esm/utils/string.utils.d.ts +61 -5
- package/build/esm/utils/string.utils.js +101 -11
- package/build/esm/utils/string.utils.js.map +1 -1
- package/build/esm/utils/url.utils.d.ts +117 -0
- package/build/esm/utils/url.utils.js +287 -0
- package/build/esm/utils/url.utils.js.map +1 -1
- package/build/esm/utils/validate.utils.d.ts +110 -0
- package/build/esm/utils/validate.utils.js +186 -0
- package/build/esm/utils/validate.utils.js.map +1 -1
- package/build/esnext/config.d.ts +12 -0
- package/build/esnext/config.js +12 -0
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/index.d.ts +1 -0
- package/build/esnext/index.js +1 -0
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/types/api-response.d.ts +82 -0
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/utils/array.utils.d.ts +64 -1
- package/build/esnext/utils/array.utils.js +129 -2
- package/build/esnext/utils/array.utils.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +60 -1
- package/build/esnext/utils/async.utils.js +117 -1
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/cache.utils.d.ts +67 -4
- package/build/esnext/utils/cache.utils.js +137 -9
- package/build/esnext/utils/cache.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +101 -0
- package/build/esnext/utils/context-store.utils.js +168 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/crypto.utils.d.ts +110 -8
- package/build/esnext/utils/crypto.utils.js +205 -15
- package/build/esnext/utils/crypto.utils.js.map +1 -1
- package/build/esnext/utils/decorators.utils.d.ts +40 -0
- package/build/esnext/utils/decorators.utils.js +155 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +121 -0
- package/build/esnext/utils/dir.utils.js +306 -0
- package/build/esnext/utils/dir.utils.js.map +1 -1
- package/build/esnext/utils/env.utils.d.ts +83 -0
- package/build/esnext/utils/env.utils.js +185 -0
- package/build/esnext/utils/env.utils.js.map +1 -1
- package/build/esnext/utils/exception.utils.d.ts +116 -0
- package/build/esnext/utils/exception.utils.js +213 -0
- package/build/esnext/utils/exception.utils.js.map +1 -1
- package/build/esnext/utils/fs.utils.d.ts +132 -0
- package/build/esnext/utils/fs.utils.js +268 -0
- package/build/esnext/utils/fs.utils.js.map +1 -1
- package/build/esnext/utils/http-status-codes.d.ts +87 -178
- package/build/esnext/utils/http-status-codes.js +92 -178
- package/build/esnext/utils/http-status-codes.js.map +1 -1
- package/build/esnext/utils/id.utils.d.ts +11 -10
- package/build/esnext/utils/id.utils.js +27 -19
- package/build/esnext/utils/id.utils.js.map +1 -1
- package/build/esnext/utils/logger.utils.d.ts +24 -0
- package/build/esnext/utils/logger.utils.js +42 -0
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.d.ts +72 -0
- package/build/esnext/utils/middleware.utils.js +157 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +65 -3
- package/build/esnext/utils/obj.utils.js +167 -3
- package/build/esnext/utils/obj.utils.js.map +1 -1
- package/build/esnext/utils/response.utils.d.ts +101 -0
- package/build/esnext/utils/response.utils.js +138 -0
- package/build/esnext/utils/response.utils.js.map +1 -1
- package/build/esnext/utils/string.utils.d.ts +61 -5
- package/build/esnext/utils/string.utils.js +110 -14
- package/build/esnext/utils/string.utils.js.map +1 -1
- package/build/esnext/utils/url.utils.d.ts +117 -0
- package/build/esnext/utils/url.utils.js +248 -0
- package/build/esnext/utils/url.utils.js.map +1 -1
- package/build/esnext/utils/validate.utils.d.ts +110 -0
- package/build/esnext/utils/validate.utils.js +184 -0
- package/build/esnext/utils/validate.utils.js.map +1 -1
- package/build/src/config.d.ts +12 -0
- package/build/src/config.js +12 -0
- package/build/src/config.js.map +1 -1
- package/build/src/index.d.ts +1 -0
- package/build/src/index.js +1 -0
- package/build/src/index.js.map +1 -1
- package/build/src/types/api-response.d.ts +82 -0
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/utils/array.utils.d.ts +64 -1
- package/build/src/utils/array.utils.js +137 -4
- package/build/src/utils/array.utils.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +60 -1
- package/build/src/utils/async.utils.js +124 -4
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/cache.utils.d.ts +67 -4
- package/build/src/utils/cache.utils.js +137 -9
- package/build/src/utils/cache.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +101 -0
- package/build/src/utils/context-store.utils.js +171 -1
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/crypto.utils.d.ts +110 -8
- package/build/src/utils/crypto.utils.js +224 -27
- package/build/src/utils/crypto.utils.js.map +1 -1
- package/build/src/utils/decorators.utils.d.ts +40 -0
- package/build/src/utils/decorators.utils.js +165 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +121 -0
- package/build/src/utils/dir.utils.js +316 -0
- package/build/src/utils/dir.utils.js.map +1 -1
- package/build/src/utils/env.utils.d.ts +83 -0
- package/build/src/utils/env.utils.js +186 -1
- package/build/src/utils/env.utils.js.map +1 -1
- package/build/src/utils/exception.utils.d.ts +116 -0
- package/build/src/utils/exception.utils.js +226 -1
- package/build/src/utils/exception.utils.js.map +1 -1
- package/build/src/utils/fs.utils.d.ts +132 -0
- package/build/src/utils/fs.utils.js +282 -0
- package/build/src/utils/fs.utils.js.map +1 -1
- package/build/src/utils/http-status-codes.d.ts +87 -178
- package/build/src/utils/http-status-codes.js +92 -178
- package/build/src/utils/http-status-codes.js.map +1 -1
- package/build/src/utils/id.utils.d.ts +11 -10
- package/build/src/utils/id.utils.js +27 -19
- package/build/src/utils/id.utils.js.map +1 -1
- package/build/src/utils/logger.utils.d.ts +24 -0
- package/build/src/utils/logger.utils.js +45 -0
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.d.ts +72 -0
- package/build/src/utils/middleware.utils.js +163 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +65 -3
- package/build/src/utils/obj.utils.js +177 -7
- package/build/src/utils/obj.utils.js.map +1 -1
- package/build/src/utils/response.utils.d.ts +101 -0
- package/build/src/utils/response.utils.js +146 -1
- package/build/src/utils/response.utils.js.map +1 -1
- package/build/src/utils/string.utils.d.ts +61 -5
- package/build/src/utils/string.utils.js +122 -20
- package/build/src/utils/string.utils.js.map +1 -1
- package/build/src/utils/url.utils.d.ts +117 -0
- package/build/src/utils/url.utils.js +257 -0
- package/build/src/utils/url.utils.js.map +1 -1
- package/build/src/utils/validate.utils.d.ts +110 -0
- package/build/src/utils/validate.utils.js +198 -0
- package/build/src/utils/validate.utils.js.map +1 -1
- package/package.json +20 -12
|
@@ -7,7 +7,26 @@ exports.fileExists = fileExists;
|
|
|
7
7
|
exports.readJsonFile = readJsonFile;
|
|
8
8
|
exports.writeJsonFile = writeJsonFile;
|
|
9
9
|
exports.deleteFileIfExists = deleteFileIfExists;
|
|
10
|
+
exports.readTextFile = readTextFile;
|
|
11
|
+
exports.writeTextFile = writeTextFile;
|
|
12
|
+
exports.appendTextFile = appendTextFile;
|
|
13
|
+
exports.copyFile = copyFile;
|
|
14
|
+
exports.moveFile = moveFile;
|
|
15
|
+
exports.getFileStats = getFileStats;
|
|
16
|
+
exports.createTempFile = createTempFile;
|
|
17
|
+
exports.streamFile = streamFile;
|
|
18
|
+
exports.readDirectory = readDirectory;
|
|
19
|
+
exports.createDirectory = createDirectory;
|
|
20
|
+
exports.safeReadJsonFile = safeReadJsonFile;
|
|
21
|
+
exports.isFile = isFile;
|
|
22
|
+
exports.getFileSize = getFileSize;
|
|
23
|
+
exports.readFileBuffer = readFileBuffer;
|
|
10
24
|
const promises_1 = __importDefault(require("fs/promises"));
|
|
25
|
+
const fs_1 = require("fs");
|
|
26
|
+
const os_1 = require("os");
|
|
27
|
+
const path_1 = __importDefault(require("path"));
|
|
28
|
+
const promises_2 = require("stream/promises");
|
|
29
|
+
const crypto_1 = require("crypto");
|
|
11
30
|
/**
|
|
12
31
|
* Checks whether a file or directory exists at the given path.
|
|
13
32
|
*
|
|
@@ -68,4 +87,267 @@ async function deleteFileIfExists(path) {
|
|
|
68
87
|
return false;
|
|
69
88
|
}
|
|
70
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Reads a text file from the specified path.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} path - The path to the text file.
|
|
94
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
95
|
+
* @returns {Promise<string | null>} The file contents, or `null` if reading fails.
|
|
96
|
+
*/
|
|
97
|
+
async function readTextFile(path, encoding = "utf-8") {
|
|
98
|
+
try {
|
|
99
|
+
return await promises_1.default.readFile(path, encoding);
|
|
100
|
+
}
|
|
101
|
+
catch (_a) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Writes text content to a file.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} path - The destination file path.
|
|
109
|
+
* @param {string} content - The text content to write.
|
|
110
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
111
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
112
|
+
*/
|
|
113
|
+
async function writeTextFile(path, content, encoding = "utf-8") {
|
|
114
|
+
try {
|
|
115
|
+
await promises_1.default.writeFile(path, content, encoding);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
catch (_a) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Appends text content to a file.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} path - The file path to append to.
|
|
126
|
+
* @param {string} content - The text content to append.
|
|
127
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
128
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
129
|
+
*/
|
|
130
|
+
async function appendTextFile(path, content, encoding = "utf-8") {
|
|
131
|
+
try {
|
|
132
|
+
await promises_1.default.appendFile(path, content, encoding);
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
catch (_a) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Copies a file from source to destination.
|
|
141
|
+
*
|
|
142
|
+
* @param {string} source - Source file path.
|
|
143
|
+
* @param {string} destination - Destination file path.
|
|
144
|
+
* @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
|
|
145
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
146
|
+
*/
|
|
147
|
+
async function copyFile(source, destination, overwrite = false) {
|
|
148
|
+
try {
|
|
149
|
+
const flags = overwrite ? 0 : promises_1.default.constants.COPYFILE_EXCL;
|
|
150
|
+
await promises_1.default.copyFile(source, destination, flags);
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
catch (_a) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Renames/moves a file.
|
|
159
|
+
*
|
|
160
|
+
* @param {string} oldPath - Current file path.
|
|
161
|
+
* @param {string} newPath - New file path.
|
|
162
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
163
|
+
*/
|
|
164
|
+
async function moveFile(oldPath, newPath) {
|
|
165
|
+
try {
|
|
166
|
+
await promises_1.default.rename(oldPath, newPath);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
catch (_a) {
|
|
170
|
+
// If rename fails (e.g., across devices), try copy+delete
|
|
171
|
+
try {
|
|
172
|
+
await promises_1.default.copyFile(oldPath, newPath);
|
|
173
|
+
await promises_1.default.unlink(oldPath);
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
catch (_b) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Gets file stats if the file exists.
|
|
183
|
+
*
|
|
184
|
+
* @param {string} path - Path to the file.
|
|
185
|
+
* @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
|
|
186
|
+
*/
|
|
187
|
+
async function getFileStats(path) {
|
|
188
|
+
try {
|
|
189
|
+
return await promises_1.default.stat(path);
|
|
190
|
+
}
|
|
191
|
+
catch (_a) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Creates a temporary file with optional content.
|
|
197
|
+
*
|
|
198
|
+
* @param {object} [options] - Options for creating the temp file.
|
|
199
|
+
* @param {string} [options.prefix="tmp-"] - Filename prefix.
|
|
200
|
+
* @param {string} [options.extension=""] - File extension.
|
|
201
|
+
* @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
|
|
202
|
+
* @param {string | Buffer} [options.content] - Optional content to write to the file.
|
|
203
|
+
* @returns {Promise<string>} Path to the created temporary file.
|
|
204
|
+
*/
|
|
205
|
+
async function createTempFile({ prefix = "tmp-", extension = "", dir = (0, os_1.tmpdir)(), content, } = {}) {
|
|
206
|
+
const ext = extension
|
|
207
|
+
? extension.startsWith(".")
|
|
208
|
+
? extension
|
|
209
|
+
: `.${extension}`
|
|
210
|
+
: "";
|
|
211
|
+
const filename = `${prefix}${(0, crypto_1.randomUUID)()}${ext}`;
|
|
212
|
+
const filepath = path_1.default.join(dir, filename);
|
|
213
|
+
if (content) {
|
|
214
|
+
await promises_1.default.writeFile(filepath, content);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
await promises_1.default.writeFile(filepath, "");
|
|
218
|
+
}
|
|
219
|
+
return filepath;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Streams a file from source to destination.
|
|
223
|
+
* Useful for large files to avoid loading the entire file into memory.
|
|
224
|
+
*
|
|
225
|
+
* @param {string} source - Source file path.
|
|
226
|
+
* @param {string} destination - Destination file path.
|
|
227
|
+
* @returns {Promise<void>} Resolves when streaming completes.
|
|
228
|
+
* @throws {Error} If streaming fails.
|
|
229
|
+
*/
|
|
230
|
+
async function streamFile(source, destination) {
|
|
231
|
+
const readStream = (0, fs_1.createReadStream)(source);
|
|
232
|
+
const writeStream = (0, fs_1.createWriteStream)(destination);
|
|
233
|
+
try {
|
|
234
|
+
await (0, promises_2.pipeline)(readStream, writeStream);
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
// Ensure both streams are closed in case of error
|
|
238
|
+
readStream.destroy();
|
|
239
|
+
writeStream.destroy();
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Reads a directory and returns file names.
|
|
245
|
+
*
|
|
246
|
+
* @param {string} dirPath - Path to the directory.
|
|
247
|
+
* @param {object} [options] - Options for reading the directory.
|
|
248
|
+
* @param {boolean} [options.fullPaths=false] - Whether to return full paths.
|
|
249
|
+
* @param {RegExp} [options.filter] - Optional regex to filter files.
|
|
250
|
+
* @returns {Promise<string[]>} Array of file names or paths.
|
|
251
|
+
* @throws {Error} If directory cannot be read.
|
|
252
|
+
*/
|
|
253
|
+
async function readDirectory(dirPath, options = {}) {
|
|
254
|
+
const files = await promises_1.default.readdir(dirPath);
|
|
255
|
+
let result = files;
|
|
256
|
+
if (options.filter) {
|
|
257
|
+
result = result.filter((file) => options.filter.test(file));
|
|
258
|
+
}
|
|
259
|
+
if (options.fullPaths) {
|
|
260
|
+
result = result.map((file) => path_1.default.join(dirPath, file));
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Creates a directory if it doesn't exist.
|
|
266
|
+
*
|
|
267
|
+
* @param {string} dirPath - Path to the directory.
|
|
268
|
+
* @param {boolean} [recursive=true] - Whether to create parent directories.
|
|
269
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
270
|
+
*/
|
|
271
|
+
async function createDirectory(dirPath, recursive = true) {
|
|
272
|
+
try {
|
|
273
|
+
await promises_1.default.mkdir(dirPath, { recursive });
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
catch (err) {
|
|
277
|
+
// Consider it success if directory already exists
|
|
278
|
+
return err.code === "EEXIST";
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Safely reads and parses a JSON file with error details.
|
|
283
|
+
*
|
|
284
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
285
|
+
* @param {string} path - The path to the JSON file.
|
|
286
|
+
* @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
|
|
287
|
+
*/
|
|
288
|
+
async function safeReadJsonFile(path) {
|
|
289
|
+
try {
|
|
290
|
+
const content = await promises_1.default.readFile(path, "utf-8");
|
|
291
|
+
try {
|
|
292
|
+
const data = JSON.parse(content);
|
|
293
|
+
return { data, error: null };
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
return {
|
|
297
|
+
data: null,
|
|
298
|
+
error: new Error(`Invalid JSON in file ${path}: ${error.message}`),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
return {
|
|
304
|
+
data: null,
|
|
305
|
+
error: new Error(`Failed to read file ${path}: ${error.message}`),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Checks if a path points to a file (not a directory).
|
|
311
|
+
*
|
|
312
|
+
* @param {string} path - Path to check.
|
|
313
|
+
* @returns {Promise<boolean>} True if the path is a file, false otherwise.
|
|
314
|
+
*/
|
|
315
|
+
async function isFile(path) {
|
|
316
|
+
try {
|
|
317
|
+
const stat = await promises_1.default.stat(path);
|
|
318
|
+
return stat.isFile();
|
|
319
|
+
}
|
|
320
|
+
catch (_a) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Gets the size of a file in bytes.
|
|
326
|
+
*
|
|
327
|
+
* @param {string} path - Path to the file.
|
|
328
|
+
* @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
|
|
329
|
+
*/
|
|
330
|
+
async function getFileSize(path) {
|
|
331
|
+
try {
|
|
332
|
+
const stat = await promises_1.default.stat(path);
|
|
333
|
+
return stat.size;
|
|
334
|
+
}
|
|
335
|
+
catch (_a) {
|
|
336
|
+
return -1;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Reads a file as a Buffer.
|
|
341
|
+
*
|
|
342
|
+
* @param {string} path - Path to the file.
|
|
343
|
+
* @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
|
|
344
|
+
*/
|
|
345
|
+
async function readFileBuffer(path) {
|
|
346
|
+
try {
|
|
347
|
+
return await promises_1.default.readFile(path);
|
|
348
|
+
}
|
|
349
|
+
catch (_a) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
71
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O,IAAI,CAAC;QACvC,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC","sourcesContent":["import fs from \"fs/promises\";\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(\n path: string,\n data: any,\n space = 2,\n): 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"]}
|
|
1
|
+
{"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":";;;;;AAcA,gCAOC;AASD,oCAOC;AAUD,sCAOC;AAQD,gDAQC;AASD,oCASC;AAUD,sCAWC;AAUD,wCAWC;AAUD,4BAYC;AASD,4BAiBC;AAQD,oCAMC;AAYD,wCA0BC;AAWD,gCAe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AACH,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,WAAW,CAAC,IAAY;IAC5C,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,kBAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,CAAC,CAAC,CAAC;IACZ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,cAAc,CAAC,IAAY;IAC/C,IAAI,CAAC;QACH,OAAO,MAAM,kBAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { 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(\n path: string,\n data: any,\n space = 2,\n): 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(\n path: string,\n encoding: BufferEncoding = \"utf-8\",\n): 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(\n source: string,\n destination: string,\n overwrite = false,\n): 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(\n oldPath: string,\n newPath: string,\n): 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\n ? extension.startsWith(\".\")\n ? extension\n : `.${extension}`\n : \"\";\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(\n source: string,\n destination: string,\n): 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(\n dirPath: string,\n recursive = true,\n): 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>(\n path: string,\n): 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"]}
|