@catbee/utils 2.0.0-next.0 → 2.0.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 +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/fs/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,208 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import { BufferEncoding } from '@catbee/utils/crypto';
|
|
26
|
+
import fs from 'node:fs';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Checks whether a file or directory exists at the given path.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} path - The file or directory path to check.
|
|
32
|
+
* @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
|
|
33
|
+
*/
|
|
34
|
+
declare function fileExists(path: string): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Reads and parses a JSON file from the specified path.
|
|
37
|
+
*
|
|
38
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
39
|
+
* @param {string} path - The path to the JSON file.
|
|
40
|
+
* @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
|
|
41
|
+
*/
|
|
42
|
+
declare function readJsonFile<T = any>(path: string): Promise<T | null>;
|
|
43
|
+
/**
|
|
44
|
+
* Read and parse JSON file or return a provided default value on error.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam T
|
|
47
|
+
* @param {string} path - Path to JSON file
|
|
48
|
+
* @param {T} defaultValue - Default value to return when read/parse fails
|
|
49
|
+
* @returns {Promise<T>} Parsed JSON or defaultValue
|
|
50
|
+
*/
|
|
51
|
+
declare function readJsonOrDefault<T = any>(path: string, defaultValue: T): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Writes a JavaScript object to a file as formatted JSON.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} path - The destination file path.
|
|
56
|
+
* @param {any} data - The object to serialize and write.
|
|
57
|
+
* @param {number} [space=2] - Number of spaces for JSON formatting.
|
|
58
|
+
* @returns {Promise<void>} Resolves when writing is complete.
|
|
59
|
+
*/
|
|
60
|
+
declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Deletes a file if it exists.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} path - The file path to delete.
|
|
65
|
+
* @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
|
|
66
|
+
*/
|
|
67
|
+
declare function deleteFileIfExists(path: string): Promise<boolean>;
|
|
68
|
+
/**
|
|
69
|
+
* Reads a file from the specified path.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} path - The path to the file.
|
|
72
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
73
|
+
* @returns {Promise<string | null>} The file contents, or `null` if reading fails.
|
|
74
|
+
*/
|
|
75
|
+
declare function readFile(path: string, encoding?: BufferEncoding): Promise<string>;
|
|
76
|
+
/**
|
|
77
|
+
* Reads a file from the specified path synchronously.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} path - The path to the file.
|
|
80
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
81
|
+
* @returns {string | null} The file contents, or `null` if reading fails.
|
|
82
|
+
*/
|
|
83
|
+
declare function readFileSync(path: string, encoding?: BufferEncoding): string;
|
|
84
|
+
/**
|
|
85
|
+
* Safely reads a file and returns its content along with any error encountered.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} path - The path to the file.
|
|
88
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
89
|
+
* @returns {Promise<{ data: string | null; error: Error | null }>} Object with data and error properties.
|
|
90
|
+
*/
|
|
91
|
+
declare function safeReadFile(path: string, encoding?: BufferEncoding): Promise<{
|
|
92
|
+
data: string | null;
|
|
93
|
+
error: Error | null;
|
|
94
|
+
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Safely reads a file synchronously and returns its content along with any error encountered.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} path - The path to the file.
|
|
99
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
100
|
+
* @returns {{ data: string | null; error: Error | null }} Object with data and error properties.
|
|
101
|
+
*/
|
|
102
|
+
declare function safeReadFileSync(path: string, encoding?: BufferEncoding): {
|
|
103
|
+
data: string | null;
|
|
104
|
+
error: Error | null;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Writes content to a file.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} path - The destination file path.
|
|
110
|
+
* @param {string} content - The text content to write.
|
|
111
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
112
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
113
|
+
*/
|
|
114
|
+
declare function writeFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
|
|
115
|
+
/**
|
|
116
|
+
* Appends text content to a file.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} path - The file path to append to.
|
|
119
|
+
* @param {string} content - The text content to append.
|
|
120
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
121
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
122
|
+
*/
|
|
123
|
+
declare function appendFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* Copies a file from source to destination.
|
|
126
|
+
*
|
|
127
|
+
* @param {string} source - Source file path.
|
|
128
|
+
* @param {string} destination - Destination file path.
|
|
129
|
+
* @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
|
|
130
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
131
|
+
*/
|
|
132
|
+
declare function copyFile(source: string, destination: string, overwrite?: boolean): Promise<boolean>;
|
|
133
|
+
/**
|
|
134
|
+
* Renames/moves a file.
|
|
135
|
+
*
|
|
136
|
+
* @param {string} oldPath - Current file path.
|
|
137
|
+
* @param {string} newPath - New file path.
|
|
138
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
139
|
+
*/
|
|
140
|
+
declare function moveFile(oldPath: string, newPath: string): Promise<boolean>;
|
|
141
|
+
/**
|
|
142
|
+
* Gets file stats if the file exists.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} path - Path to the file.
|
|
145
|
+
* @returns {Promise<fsp.Stats | null>} File stats object or null if file doesn't exist.
|
|
146
|
+
*/
|
|
147
|
+
declare function getFileStats(path: string): Promise<fs.Stats | null>;
|
|
148
|
+
/**
|
|
149
|
+
* Creates a temporary file with optional content.
|
|
150
|
+
*
|
|
151
|
+
* @param {object} [options] - Options for creating the temp file.
|
|
152
|
+
* @param {string} [options.prefix="tmp-"] - Filename prefix.
|
|
153
|
+
* @param {string} [options.extension=""] - File extension.
|
|
154
|
+
* @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
|
|
155
|
+
* @param {string | Buffer} [options.content] - Optional content to write to the file.
|
|
156
|
+
* @returns {Promise<string>} Path to the created temporary file.
|
|
157
|
+
*/
|
|
158
|
+
declare function createTempFile({ prefix, extension, dir, content }?: {
|
|
159
|
+
prefix?: string;
|
|
160
|
+
extension?: string;
|
|
161
|
+
dir?: string;
|
|
162
|
+
content?: string | Buffer;
|
|
163
|
+
}): Promise<string>;
|
|
164
|
+
/**
|
|
165
|
+
* Streams a file from source to destination.
|
|
166
|
+
* Useful for large files to avoid loading the entire file into memory.
|
|
167
|
+
*
|
|
168
|
+
* @param {string} source - Source file path.
|
|
169
|
+
* @param {string} destination - Destination file path.
|
|
170
|
+
* @returns {Promise<void>} Resolves when streaming completes.
|
|
171
|
+
* @throws {Error} If streaming fails.
|
|
172
|
+
*/
|
|
173
|
+
declare function streamFile(source: string, destination: string): Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Reads a directory and returns file names.
|
|
176
|
+
*
|
|
177
|
+
* @param {string} dirPath - Path to the directory.
|
|
178
|
+
* @param {object} [options] - Options for reading the directory.
|
|
179
|
+
* @param {boolean} [options.fullPaths=false] - Whether to return full paths.
|
|
180
|
+
* @param {RegExp} [options.filter] - Optional regex to filter files.
|
|
181
|
+
* @returns {Promise<string[]>} Array of file names or paths.
|
|
182
|
+
* @throws {Error} If directory cannot be read.
|
|
183
|
+
*/
|
|
184
|
+
declare function readDirectory(dirPath: string, options?: {
|
|
185
|
+
fullPaths?: boolean;
|
|
186
|
+
filter?: RegExp;
|
|
187
|
+
}): Promise<string[]>;
|
|
188
|
+
/**
|
|
189
|
+
* Creates a directory if it doesn't exist.
|
|
190
|
+
*
|
|
191
|
+
* @param {string} dirPath - Path to the directory.
|
|
192
|
+
* @param {boolean} [recursive=true] - Whether to create parent directories.
|
|
193
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
194
|
+
*/
|
|
195
|
+
declare function createDirectory(dirPath: string, recursive?: boolean): Promise<boolean>;
|
|
196
|
+
/**
|
|
197
|
+
* Safely reads and parses a JSON file with error details.
|
|
198
|
+
*
|
|
199
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
200
|
+
* @param {string} path - The path to the JSON file.
|
|
201
|
+
* @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
|
|
202
|
+
*/
|
|
203
|
+
declare function safeReadJsonFile<T = any>(path: string): Promise<{
|
|
204
|
+
data: T | null;
|
|
205
|
+
error: Error | null;
|
|
206
|
+
}>;
|
|
207
|
+
/**
|
|
208
|
+
* Checks if a path points to a file (not a directory).
|
|
209
|
+
*
|
|
210
|
+
* @param {string} path - Path to check.
|
|
211
|
+
* @returns {Promise<boolean>} True if the path is a file, false otherwise.
|
|
212
|
+
*/
|
|
213
|
+
declare function isFile(path: string): Promise<boolean>;
|
|
214
|
+
/**
|
|
215
|
+
* Gets the size of a file in bytes.
|
|
216
|
+
*
|
|
217
|
+
* @param {string} path - Path to the file.
|
|
218
|
+
* @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
|
|
219
|
+
*/
|
|
220
|
+
declare function getFileSize(path: string): Promise<number>;
|
|
221
|
+
/**
|
|
222
|
+
* Reads a file as a Buffer.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} path - Path to the file.
|
|
225
|
+
* @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
|
|
226
|
+
*/
|
|
227
|
+
declare function readFileBuffer(path: string): Promise<Buffer | null>;
|
|
228
|
+
|
|
229
|
+
export { appendFile, copyFile, createDirectory, createTempFile, deleteFileIfExists, fileExists, getFileSize, getFileStats, isFile, moveFile, readDirectory, readFile, readFileBuffer, readFileSync, readJsonFile, readJsonOrDefault, safeReadFile, safeReadFileSync, safeReadJsonFile, streamFile, writeFile, writeJsonFile };
|
package/fs/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import
|
|
26
|
-
import
|
|
25
|
+
import fs from 'fs';
|
|
26
|
+
import fsp from 'fs/promises';
|
|
27
27
|
import { tmpdir } from 'os';
|
|
28
28
|
import path from 'path';
|
|
29
29
|
import { pipeline } from 'stream/promises';
|
|
@@ -33,7 +33,7 @@ var __defProp = Object.defineProperty;
|
|
|
33
33
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
34
34
|
async function fileExists(path2) {
|
|
35
35
|
try {
|
|
36
|
-
await
|
|
36
|
+
await fsp.access(path2);
|
|
37
37
|
return true;
|
|
38
38
|
} catch {
|
|
39
39
|
return false;
|
|
@@ -42,7 +42,7 @@ async function fileExists(path2) {
|
|
|
42
42
|
__name(fileExists, "fileExists");
|
|
43
43
|
async function readJsonFile(path2) {
|
|
44
44
|
try {
|
|
45
|
-
const data = await
|
|
45
|
+
const data = await fsp.readFile(path2, "utf-8");
|
|
46
46
|
return JSON.parse(data);
|
|
47
47
|
} catch {
|
|
48
48
|
return null;
|
|
@@ -56,12 +56,12 @@ async function readJsonOrDefault(path2, defaultValue) {
|
|
|
56
56
|
__name(readJsonOrDefault, "readJsonOrDefault");
|
|
57
57
|
async function writeJsonFile(path2, data, space = 2) {
|
|
58
58
|
const json = JSON.stringify(data, null, space);
|
|
59
|
-
await
|
|
59
|
+
await fsp.writeFile(path2, json, "utf-8");
|
|
60
60
|
}
|
|
61
61
|
__name(writeJsonFile, "writeJsonFile");
|
|
62
62
|
async function deleteFileIfExists(path2) {
|
|
63
63
|
try {
|
|
64
|
-
await
|
|
64
|
+
await fsp.unlink(path2);
|
|
65
65
|
return true;
|
|
66
66
|
} catch (err) {
|
|
67
67
|
if (err.code === "ENOENT") return true;
|
|
@@ -69,36 +69,66 @@ async function deleteFileIfExists(path2) {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
__name(deleteFileIfExists, "deleteFileIfExists");
|
|
72
|
-
async function
|
|
72
|
+
async function readFile(path2, encoding = "utf-8") {
|
|
73
|
+
return await fsp.readFile(path2, encoding);
|
|
74
|
+
}
|
|
75
|
+
__name(readFile, "readFile");
|
|
76
|
+
function readFileSync(path2, encoding = "utf-8") {
|
|
77
|
+
return fs.readFileSync(path2, encoding);
|
|
78
|
+
}
|
|
79
|
+
__name(readFileSync, "readFileSync");
|
|
80
|
+
async function safeReadFile(path2, encoding = "utf-8") {
|
|
73
81
|
try {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
const data = await fsp.readFile(path2, encoding);
|
|
83
|
+
return {
|
|
84
|
+
data,
|
|
85
|
+
error: null
|
|
86
|
+
};
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return {
|
|
89
|
+
data: null,
|
|
90
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
__name(safeReadFile, "safeReadFile");
|
|
95
|
+
function safeReadFileSync(path2, encoding = "utf-8") {
|
|
96
|
+
try {
|
|
97
|
+
const data = fs.readFileSync(path2, encoding);
|
|
98
|
+
return {
|
|
99
|
+
data,
|
|
100
|
+
error: null
|
|
101
|
+
};
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return {
|
|
104
|
+
data: null,
|
|
105
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
106
|
+
};
|
|
77
107
|
}
|
|
78
108
|
}
|
|
79
|
-
__name(
|
|
80
|
-
async function
|
|
109
|
+
__name(safeReadFileSync, "safeReadFileSync");
|
|
110
|
+
async function writeFile(path2, content, encoding = "utf-8") {
|
|
81
111
|
try {
|
|
82
|
-
await
|
|
112
|
+
await fsp.writeFile(path2, content, encoding);
|
|
83
113
|
return true;
|
|
84
114
|
} catch {
|
|
85
115
|
return false;
|
|
86
116
|
}
|
|
87
117
|
}
|
|
88
|
-
__name(
|
|
89
|
-
async function
|
|
118
|
+
__name(writeFile, "writeFile");
|
|
119
|
+
async function appendFile(path2, content, encoding = "utf-8") {
|
|
90
120
|
try {
|
|
91
|
-
await
|
|
121
|
+
await fsp.appendFile(path2, content, encoding);
|
|
92
122
|
return true;
|
|
93
123
|
} catch {
|
|
94
124
|
return false;
|
|
95
125
|
}
|
|
96
126
|
}
|
|
97
|
-
__name(
|
|
127
|
+
__name(appendFile, "appendFile");
|
|
98
128
|
async function copyFile(source, destination, overwrite = false) {
|
|
99
129
|
try {
|
|
100
|
-
const flags = overwrite ? 0 :
|
|
101
|
-
await
|
|
130
|
+
const flags = overwrite ? 0 : fsp.constants.COPYFILE_EXCL;
|
|
131
|
+
await fsp.copyFile(source, destination, flags);
|
|
102
132
|
return true;
|
|
103
133
|
} catch {
|
|
104
134
|
return false;
|
|
@@ -107,12 +137,12 @@ async function copyFile(source, destination, overwrite = false) {
|
|
|
107
137
|
__name(copyFile, "copyFile");
|
|
108
138
|
async function moveFile(oldPath, newPath) {
|
|
109
139
|
try {
|
|
110
|
-
await
|
|
140
|
+
await fsp.rename(oldPath, newPath);
|
|
111
141
|
return true;
|
|
112
142
|
} catch {
|
|
113
143
|
try {
|
|
114
|
-
await
|
|
115
|
-
await
|
|
144
|
+
await fsp.copyFile(oldPath, newPath);
|
|
145
|
+
await fsp.unlink(oldPath);
|
|
116
146
|
return true;
|
|
117
147
|
} catch {
|
|
118
148
|
return false;
|
|
@@ -122,7 +152,7 @@ async function moveFile(oldPath, newPath) {
|
|
|
122
152
|
__name(moveFile, "moveFile");
|
|
123
153
|
async function getFileStats(path2) {
|
|
124
154
|
try {
|
|
125
|
-
return await
|
|
155
|
+
return await fsp.stat(path2);
|
|
126
156
|
} catch {
|
|
127
157
|
return null;
|
|
128
158
|
}
|
|
@@ -133,16 +163,16 @@ async function createTempFile({ prefix = "tmp-", extension = "", dir = tmpdir(),
|
|
|
133
163
|
const filename = `${prefix}${randomUUID()}${ext}`;
|
|
134
164
|
const filepath = path.join(dir, filename);
|
|
135
165
|
if (content) {
|
|
136
|
-
await
|
|
166
|
+
await fsp.writeFile(filepath, content);
|
|
137
167
|
} else {
|
|
138
|
-
await
|
|
168
|
+
await fsp.writeFile(filepath, "");
|
|
139
169
|
}
|
|
140
170
|
return filepath;
|
|
141
171
|
}
|
|
142
172
|
__name(createTempFile, "createTempFile");
|
|
143
173
|
async function streamFile(source, destination) {
|
|
144
|
-
const readStream = createReadStream(source);
|
|
145
|
-
const writeStream = createWriteStream(destination);
|
|
174
|
+
const readStream = fs.createReadStream(source);
|
|
175
|
+
const writeStream = fs.createWriteStream(destination);
|
|
146
176
|
try {
|
|
147
177
|
await pipeline(readStream, writeStream);
|
|
148
178
|
} catch (error) {
|
|
@@ -153,7 +183,7 @@ async function streamFile(source, destination) {
|
|
|
153
183
|
}
|
|
154
184
|
__name(streamFile, "streamFile");
|
|
155
185
|
async function readDirectory(dirPath, options = {}) {
|
|
156
|
-
const files = await
|
|
186
|
+
const files = await fsp.readdir(dirPath);
|
|
157
187
|
let result = files;
|
|
158
188
|
if (options.filter) {
|
|
159
189
|
result = result.filter((file) => options.filter.test(file));
|
|
@@ -166,7 +196,7 @@ async function readDirectory(dirPath, options = {}) {
|
|
|
166
196
|
__name(readDirectory, "readDirectory");
|
|
167
197
|
async function createDirectory(dirPath, recursive = true) {
|
|
168
198
|
try {
|
|
169
|
-
await
|
|
199
|
+
await fsp.mkdir(dirPath, {
|
|
170
200
|
recursive
|
|
171
201
|
});
|
|
172
202
|
return true;
|
|
@@ -177,7 +207,7 @@ async function createDirectory(dirPath, recursive = true) {
|
|
|
177
207
|
__name(createDirectory, "createDirectory");
|
|
178
208
|
async function safeReadJsonFile(path2) {
|
|
179
209
|
try {
|
|
180
|
-
const content = await
|
|
210
|
+
const content = await fsp.readFile(path2, "utf-8");
|
|
181
211
|
try {
|
|
182
212
|
const data = JSON.parse(content);
|
|
183
213
|
return {
|
|
@@ -200,7 +230,7 @@ async function safeReadJsonFile(path2) {
|
|
|
200
230
|
__name(safeReadJsonFile, "safeReadJsonFile");
|
|
201
231
|
async function isFile(path2) {
|
|
202
232
|
try {
|
|
203
|
-
const stat = await
|
|
233
|
+
const stat = await fsp.stat(path2);
|
|
204
234
|
return stat.isFile();
|
|
205
235
|
} catch {
|
|
206
236
|
return false;
|
|
@@ -209,7 +239,7 @@ async function isFile(path2) {
|
|
|
209
239
|
__name(isFile, "isFile");
|
|
210
240
|
async function getFileSize(path2) {
|
|
211
241
|
try {
|
|
212
|
-
const stat = await
|
|
242
|
+
const stat = await fsp.stat(path2);
|
|
213
243
|
return stat.size;
|
|
214
244
|
} catch {
|
|
215
245
|
return -1;
|
|
@@ -218,11 +248,11 @@ async function getFileSize(path2) {
|
|
|
218
248
|
__name(getFileSize, "getFileSize");
|
|
219
249
|
async function readFileBuffer(path2) {
|
|
220
250
|
try {
|
|
221
|
-
return await
|
|
251
|
+
return await fsp.readFile(path2);
|
|
222
252
|
} catch {
|
|
223
253
|
return null;
|
|
224
254
|
}
|
|
225
255
|
}
|
|
226
256
|
__name(readFileBuffer, "readFileBuffer");
|
|
227
257
|
|
|
228
|
-
export {
|
|
258
|
+
export { appendFile, copyFile, createDirectory, createTempFile, deleteFileIfExists, fileExists, getFileSize, getFileStats, isFile, moveFile, readDirectory, readFile, readFileBuffer, readFileSync, readJsonFile, readJsonOrDefault, safeReadFile, safeReadFileSync, safeReadJsonFile, streamFile, writeFile, writeJsonFile };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|