@catbee/utils 0.0.8-rc.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/build/index.cjs +6912 -0
- package/build/index.d.ts +5284 -0
- package/build/index.mjs +6635 -0
- package/package.json +13 -292
- package/build/cjs/config.d.ts +0 -121
- package/build/cjs/config.js +0 -137
- package/build/cjs/index.d.ts +0 -26
- package/build/cjs/index.js +0 -68
- package/build/cjs/servers/server.builder.d.ts +0 -507
- package/build/cjs/servers/server.builder.js +0 -658
- package/build/cjs/servers/server.d.ts +0 -255
- package/build/cjs/servers/server.js +0 -970
- package/build/cjs/types/api-response.d.ts +0 -151
- package/build/cjs/types/api-response.js +0 -36
- package/build/cjs/types/index.d.ts +0 -124
- package/build/cjs/types/index.js +0 -25
- package/build/cjs/types/server.d.ts +0 -267
- package/build/cjs/types/server.js +0 -25
- package/build/cjs/utils/array.utils.d.ts +0 -167
- package/build/cjs/utils/array.utils.js +0 -363
- package/build/cjs/utils/async.utils.d.ts +0 -264
- package/build/cjs/utils/async.utils.js +0 -640
- package/build/cjs/utils/cache.utils.d.ts +0 -152
- package/build/cjs/utils/cache.utils.js +0 -298
- package/build/cjs/utils/context-store.utils.d.ts +0 -188
- package/build/cjs/utils/context-store.utils.js +0 -297
- package/build/cjs/utils/crypto.utils.d.ts +0 -159
- package/build/cjs/utils/crypto.utils.js +0 -295
- package/build/cjs/utils/date.utils.d.ts +0 -158
- package/build/cjs/utils/date.utils.js +0 -395
- package/build/cjs/utils/decorators.utils.d.ts +0 -511
- package/build/cjs/utils/decorators.utils.js +0 -1035
- package/build/cjs/utils/dir.utils.d.ts +0 -195
- package/build/cjs/utils/dir.utils.js +0 -500
- package/build/cjs/utils/env.utils.d.ts +0 -376
- package/build/cjs/utils/env.utils.js +0 -786
- package/build/cjs/utils/exception.utils.d.ts +0 -229
- package/build/cjs/utils/exception.utils.js +0 -408
- package/build/cjs/utils/fs.utils.d.ts +0 -163
- package/build/cjs/utils/fs.utils.js +0 -371
- package/build/cjs/utils/http-status-codes.d.ts +0 -265
- package/build/cjs/utils/http-status-codes.js +0 -297
- package/build/cjs/utils/id.utils.d.ts +0 -35
- package/build/cjs/utils/id.utils.js +0 -90
- package/build/cjs/utils/logger.utils.d.ts +0 -159
- package/build/cjs/utils/logger.utils.js +0 -350
- package/build/cjs/utils/middleware.utils.d.ts +0 -99
- package/build/cjs/utils/middleware.utils.js +0 -243
- package/build/cjs/utils/obj.utils.d.ts +0 -123
- package/build/cjs/utils/obj.utils.js +0 -428
- package/build/cjs/utils/performance.utils.d.ts +0 -135
- package/build/cjs/utils/performance.utils.js +0 -280
- package/build/cjs/utils/request.utils.d.ts +0 -85
- package/build/cjs/utils/request.utils.js +0 -199
- package/build/cjs/utils/response.utils.d.ts +0 -162
- package/build/cjs/utils/response.utils.js +0 -274
- package/build/cjs/utils/stream.utils.d.ts +0 -87
- package/build/cjs/utils/stream.utils.js +0 -217
- package/build/cjs/utils/string.utils.d.ts +0 -92
- package/build/cjs/utils/string.utils.js +0 -178
- package/build/cjs/utils/type.utils.d.ts +0 -89
- package/build/cjs/utils/type.utils.js +0 -195
- package/build/cjs/utils/url.utils.d.ts +0 -140
- package/build/cjs/utils/url.utils.js +0 -316
- package/build/cjs/utils/validate.utils.d.ts +0 -176
- package/build/cjs/utils/validate.utils.js +0 -344
- package/build/esm/config.d.ts +0 -121
- package/build/esm/config.js +0 -132
- package/build/esm/index.d.ts +0 -26
- package/build/esm/index.js +0 -49
- package/build/esm/servers/server.builder.d.ts +0 -507
- package/build/esm/servers/server.builder.js +0 -654
- package/build/esm/servers/server.d.ts +0 -255
- package/build/esm/servers/server.js +0 -963
- package/build/esm/types/api-response.d.ts +0 -151
- package/build/esm/types/api-response.js +0 -33
- package/build/esm/types/index.d.ts +0 -124
- package/build/esm/types/index.js +0 -24
- package/build/esm/types/server.d.ts +0 -267
- package/build/esm/types/server.js +0 -24
- package/build/esm/utils/array.utils.d.ts +0 -167
- package/build/esm/utils/array.utils.js +0 -344
- package/build/esm/utils/async.utils.d.ts +0 -264
- package/build/esm/utils/async.utils.js +0 -619
- package/build/esm/utils/cache.utils.d.ts +0 -152
- package/build/esm/utils/cache.utils.js +0 -294
- package/build/esm/utils/context-store.utils.d.ts +0 -188
- package/build/esm/utils/context-store.utils.js +0 -290
- package/build/esm/utils/crypto.utils.d.ts +0 -159
- package/build/esm/utils/crypto.utils.js +0 -278
- package/build/esm/utils/date.utils.d.ts +0 -158
- package/build/esm/utils/date.utils.js +0 -383
- package/build/esm/utils/decorators.utils.d.ts +0 -511
- package/build/esm/utils/decorators.utils.js +0 -1013
- package/build/esm/utils/dir.utils.d.ts +0 -195
- package/build/esm/utils/dir.utils.js +0 -476
- package/build/esm/utils/env.utils.d.ts +0 -376
- package/build/esm/utils/env.utils.js +0 -782
- package/build/esm/utils/exception.utils.d.ts +0 -229
- package/build/esm/utils/exception.utils.js +0 -382
- package/build/esm/utils/fs.utils.d.ts +0 -163
- package/build/esm/utils/fs.utils.js +0 -348
- package/build/esm/utils/http-status-codes.d.ts +0 -265
- package/build/esm/utils/http-status-codes.js +0 -294
- package/build/esm/utils/id.utils.d.ts +0 -35
- package/build/esm/utils/id.utils.js +0 -83
- package/build/esm/utils/logger.utils.d.ts +0 -159
- package/build/esm/utils/logger.utils.js +0 -304
- package/build/esm/utils/middleware.utils.d.ts +0 -99
- package/build/esm/utils/middleware.utils.js +0 -235
- package/build/esm/utils/obj.utils.d.ts +0 -123
- package/build/esm/utils/obj.utils.js +0 -412
- package/build/esm/utils/performance.utils.d.ts +0 -135
- package/build/esm/utils/performance.utils.js +0 -273
- package/build/esm/utils/request.utils.d.ts +0 -85
- package/build/esm/utils/request.utils.js +0 -190
- package/build/esm/utils/response.utils.d.ts +0 -162
- package/build/esm/utils/response.utils.js +0 -261
- package/build/esm/utils/stream.utils.d.ts +0 -87
- package/build/esm/utils/stream.utils.js +0 -209
- package/build/esm/utils/string.utils.d.ts +0 -92
- package/build/esm/utils/string.utils.js +0 -164
- package/build/esm/utils/type.utils.d.ts +0 -89
- package/build/esm/utils/type.utils.js +0 -186
- package/build/esm/utils/url.utils.d.ts +0 -140
- package/build/esm/utils/url.utils.js +0 -303
- package/build/esm/utils/validate.utils.d.ts +0 -176
- package/build/esm/utils/validate.utils.js +0 -319
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { BufferEncoding } from './crypto.utils';
|
|
2
|
-
import { Stats } from 'fs';
|
|
3
|
-
/**
|
|
4
|
-
* Checks whether a file or directory exists at the given path.
|
|
5
|
-
*
|
|
6
|
-
* @param {string} path - The file or directory path to check.
|
|
7
|
-
* @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
|
|
8
|
-
*/
|
|
9
|
-
export declare function fileExists(path: string): Promise<boolean>;
|
|
10
|
-
/**
|
|
11
|
-
* Reads and parses a JSON file from the specified path.
|
|
12
|
-
*
|
|
13
|
-
* @typeParam T - The expected type of the parsed JSON object.
|
|
14
|
-
* @param {string} path - The path to the JSON file.
|
|
15
|
-
* @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
|
|
16
|
-
*/
|
|
17
|
-
export declare function readJsonFile<T = any>(path: string): Promise<T | null>;
|
|
18
|
-
/**
|
|
19
|
-
* Writes a JavaScript object to a file as formatted JSON.
|
|
20
|
-
*
|
|
21
|
-
* @param {string} path - The destination file path.
|
|
22
|
-
* @param {any} data - The object to serialize and write.
|
|
23
|
-
* @param {number} [space=2] - Number of spaces for JSON formatting.
|
|
24
|
-
* @returns {Promise<void>} Resolves when writing is complete.
|
|
25
|
-
*/
|
|
26
|
-
export declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* Deletes a file if it exists.
|
|
29
|
-
*
|
|
30
|
-
* @param {string} path - The file path to delete.
|
|
31
|
-
* @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
|
|
32
|
-
*/
|
|
33
|
-
export declare function deleteFileIfExists(path: string): Promise<boolean>;
|
|
34
|
-
/**
|
|
35
|
-
* Reads a text file from the specified path.
|
|
36
|
-
*
|
|
37
|
-
* @param {string} path - The path to the text file.
|
|
38
|
-
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
39
|
-
* @returns {Promise<string | null>} The file contents, or `null` if reading fails.
|
|
40
|
-
*/
|
|
41
|
-
export declare function readTextFile(path: string, encoding?: BufferEncoding): Promise<string | null>;
|
|
42
|
-
/**
|
|
43
|
-
* Writes text content to a file.
|
|
44
|
-
*
|
|
45
|
-
* @param {string} path - The destination file path.
|
|
46
|
-
* @param {string} content - The text content to write.
|
|
47
|
-
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
48
|
-
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
49
|
-
*/
|
|
50
|
-
export declare function writeTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
|
|
51
|
-
/**
|
|
52
|
-
* Appends text content to a file.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} path - The file path to append to.
|
|
55
|
-
* @param {string} content - The text content to append.
|
|
56
|
-
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
57
|
-
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
58
|
-
*/
|
|
59
|
-
export declare function appendTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
|
|
60
|
-
/**
|
|
61
|
-
* Copies a file from source to destination.
|
|
62
|
-
*
|
|
63
|
-
* @param {string} source - Source file path.
|
|
64
|
-
* @param {string} destination - Destination file path.
|
|
65
|
-
* @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
|
|
66
|
-
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
67
|
-
*/
|
|
68
|
-
export declare function copyFile(source: string, destination: string, overwrite?: boolean): Promise<boolean>;
|
|
69
|
-
/**
|
|
70
|
-
* Renames/moves a file.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} oldPath - Current file path.
|
|
73
|
-
* @param {string} newPath - New file path.
|
|
74
|
-
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
75
|
-
*/
|
|
76
|
-
export declare function moveFile(oldPath: string, newPath: string): Promise<boolean>;
|
|
77
|
-
/**
|
|
78
|
-
* Gets file stats if the file exists.
|
|
79
|
-
*
|
|
80
|
-
* @param {string} path - Path to the file.
|
|
81
|
-
* @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
|
|
82
|
-
*/
|
|
83
|
-
export declare function getFileStats(path: string): Promise<Stats | null>;
|
|
84
|
-
/**
|
|
85
|
-
* Creates a temporary file with optional content.
|
|
86
|
-
*
|
|
87
|
-
* @param {object} [options] - Options for creating the temp file.
|
|
88
|
-
* @param {string} [options.prefix="tmp-"] - Filename prefix.
|
|
89
|
-
* @param {string} [options.extension=""] - File extension.
|
|
90
|
-
* @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
|
|
91
|
-
* @param {string | Buffer} [options.content] - Optional content to write to the file.
|
|
92
|
-
* @returns {Promise<string>} Path to the created temporary file.
|
|
93
|
-
*/
|
|
94
|
-
export declare function createTempFile({ prefix, extension, dir, content }?: {
|
|
95
|
-
prefix?: string;
|
|
96
|
-
extension?: string;
|
|
97
|
-
dir?: string;
|
|
98
|
-
content?: string | Buffer;
|
|
99
|
-
}): Promise<string>;
|
|
100
|
-
/**
|
|
101
|
-
* Streams a file from source to destination.
|
|
102
|
-
* Useful for large files to avoid loading the entire file into memory.
|
|
103
|
-
*
|
|
104
|
-
* @param {string} source - Source file path.
|
|
105
|
-
* @param {string} destination - Destination file path.
|
|
106
|
-
* @returns {Promise<void>} Resolves when streaming completes.
|
|
107
|
-
* @throws {Error} If streaming fails.
|
|
108
|
-
*/
|
|
109
|
-
export declare function streamFile(source: string, destination: string): Promise<void>;
|
|
110
|
-
/**
|
|
111
|
-
* Reads a directory and returns file names.
|
|
112
|
-
*
|
|
113
|
-
* @param {string} dirPath - Path to the directory.
|
|
114
|
-
* @param {object} [options] - Options for reading the directory.
|
|
115
|
-
* @param {boolean} [options.fullPaths=false] - Whether to return full paths.
|
|
116
|
-
* @param {RegExp} [options.filter] - Optional regex to filter files.
|
|
117
|
-
* @returns {Promise<string[]>} Array of file names or paths.
|
|
118
|
-
* @throws {Error} If directory cannot be read.
|
|
119
|
-
*/
|
|
120
|
-
export declare function readDirectory(dirPath: string, options?: {
|
|
121
|
-
fullPaths?: boolean;
|
|
122
|
-
filter?: RegExp;
|
|
123
|
-
}): Promise<string[]>;
|
|
124
|
-
/**
|
|
125
|
-
* Creates a directory if it doesn't exist.
|
|
126
|
-
*
|
|
127
|
-
* @param {string} dirPath - Path to the directory.
|
|
128
|
-
* @param {boolean} [recursive=true] - Whether to create parent directories.
|
|
129
|
-
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
130
|
-
*/
|
|
131
|
-
export declare function createDirectory(dirPath: string, recursive?: boolean): Promise<boolean>;
|
|
132
|
-
/**
|
|
133
|
-
* Safely reads and parses a JSON file with error details.
|
|
134
|
-
*
|
|
135
|
-
* @typeParam T - The expected type of the parsed JSON object.
|
|
136
|
-
* @param {string} path - The path to the JSON file.
|
|
137
|
-
* @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
|
|
138
|
-
*/
|
|
139
|
-
export declare function safeReadJsonFile<T = any>(path: string): Promise<{
|
|
140
|
-
data: T | null;
|
|
141
|
-
error: Error | null;
|
|
142
|
-
}>;
|
|
143
|
-
/**
|
|
144
|
-
* Checks if a path points to a file (not a directory).
|
|
145
|
-
*
|
|
146
|
-
* @param {string} path - Path to check.
|
|
147
|
-
* @returns {Promise<boolean>} True if the path is a file, false otherwise.
|
|
148
|
-
*/
|
|
149
|
-
export declare function isFile(path: string): Promise<boolean>;
|
|
150
|
-
/**
|
|
151
|
-
* Gets the size of a file in bytes.
|
|
152
|
-
*
|
|
153
|
-
* @param {string} path - Path to the file.
|
|
154
|
-
* @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
|
|
155
|
-
*/
|
|
156
|
-
export declare function getFileSize(path: string): Promise<number>;
|
|
157
|
-
/**
|
|
158
|
-
* Reads a file as a Buffer.
|
|
159
|
-
*
|
|
160
|
-
* @param {string} path - Path to the file.
|
|
161
|
-
* @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
|
|
162
|
-
*/
|
|
163
|
-
export declare function readFileBuffer(path: string): Promise<Buffer | null>;
|
|
@@ -1,348 +0,0 @@
|
|
|
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
|
-
import fs from 'fs/promises';
|
|
25
|
-
import { createReadStream, createWriteStream } from 'fs';
|
|
26
|
-
import { tmpdir } from 'os';
|
|
27
|
-
import path from 'path';
|
|
28
|
-
import { pipeline } from 'stream/promises';
|
|
29
|
-
import { randomUUID } from 'crypto';
|
|
30
|
-
/**
|
|
31
|
-
* Checks whether a file or directory exists at the given path.
|
|
32
|
-
*
|
|
33
|
-
* @param {string} path - The file or directory path to check.
|
|
34
|
-
* @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
|
|
35
|
-
*/
|
|
36
|
-
export async function fileExists(path) {
|
|
37
|
-
try {
|
|
38
|
-
await fs.access(path);
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
catch {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Reads and parses a JSON file from the specified path.
|
|
47
|
-
*
|
|
48
|
-
* @typeParam T - The expected type of the parsed JSON object.
|
|
49
|
-
* @param {string} path - The path to the JSON file.
|
|
50
|
-
* @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
|
|
51
|
-
*/
|
|
52
|
-
export async function readJsonFile(path) {
|
|
53
|
-
try {
|
|
54
|
-
const data = await fs.readFile(path, 'utf-8');
|
|
55
|
-
return JSON.parse(data);
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Writes a JavaScript object to a file as formatted JSON.
|
|
63
|
-
*
|
|
64
|
-
* @param {string} path - The destination file path.
|
|
65
|
-
* @param {any} data - The object to serialize and write.
|
|
66
|
-
* @param {number} [space=2] - Number of spaces for JSON formatting.
|
|
67
|
-
* @returns {Promise<void>} Resolves when writing is complete.
|
|
68
|
-
*/
|
|
69
|
-
export async function writeJsonFile(path, data, space = 2) {
|
|
70
|
-
const json = JSON.stringify(data, null, space);
|
|
71
|
-
await fs.writeFile(path, json, 'utf-8');
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Deletes a file if it exists.
|
|
75
|
-
*
|
|
76
|
-
* @param {string} path - The file path to delete.
|
|
77
|
-
* @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
|
|
78
|
-
*/
|
|
79
|
-
export async function deleteFileIfExists(path) {
|
|
80
|
-
try {
|
|
81
|
-
await fs.unlink(path);
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
catch (err) {
|
|
85
|
-
if (err.code === 'ENOENT')
|
|
86
|
-
return true;
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
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
|
-
export async function readTextFile(path, encoding = 'utf-8') {
|
|
98
|
-
try {
|
|
99
|
-
return await fs.readFile(path, encoding);
|
|
100
|
-
}
|
|
101
|
-
catch {
|
|
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
|
-
export async function writeTextFile(path, content, encoding = 'utf-8') {
|
|
114
|
-
try {
|
|
115
|
-
await fs.writeFile(path, content, encoding);
|
|
116
|
-
return true;
|
|
117
|
-
}
|
|
118
|
-
catch {
|
|
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
|
-
export async function appendTextFile(path, content, encoding = 'utf-8') {
|
|
131
|
-
try {
|
|
132
|
-
await fs.appendFile(path, content, encoding);
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
catch {
|
|
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
|
-
export async function copyFile(source, destination, overwrite = false) {
|
|
148
|
-
try {
|
|
149
|
-
const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
|
|
150
|
-
await fs.copyFile(source, destination, flags);
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
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
|
-
export async function moveFile(oldPath, newPath) {
|
|
165
|
-
try {
|
|
166
|
-
await fs.rename(oldPath, newPath);
|
|
167
|
-
return true;
|
|
168
|
-
}
|
|
169
|
-
catch {
|
|
170
|
-
// If rename fails (e.g., across devices), try copy+delete
|
|
171
|
-
try {
|
|
172
|
-
await fs.copyFile(oldPath, newPath);
|
|
173
|
-
await fs.unlink(oldPath);
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
catch {
|
|
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
|
-
export async function getFileStats(path) {
|
|
188
|
-
try {
|
|
189
|
-
return await fs.stat(path);
|
|
190
|
-
}
|
|
191
|
-
catch {
|
|
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
|
-
export async function createTempFile({ prefix = 'tmp-', extension = '', dir = tmpdir(), content } = {}) {
|
|
206
|
-
const ext = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : '';
|
|
207
|
-
const filename = `${prefix}${randomUUID()}${ext}`;
|
|
208
|
-
const filepath = path.join(dir, filename);
|
|
209
|
-
if (content) {
|
|
210
|
-
await fs.writeFile(filepath, content);
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
await fs.writeFile(filepath, '');
|
|
214
|
-
}
|
|
215
|
-
return filepath;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Streams a file from source to destination.
|
|
219
|
-
* Useful for large files to avoid loading the entire file into memory.
|
|
220
|
-
*
|
|
221
|
-
* @param {string} source - Source file path.
|
|
222
|
-
* @param {string} destination - Destination file path.
|
|
223
|
-
* @returns {Promise<void>} Resolves when streaming completes.
|
|
224
|
-
* @throws {Error} If streaming fails.
|
|
225
|
-
*/
|
|
226
|
-
export async function streamFile(source, destination) {
|
|
227
|
-
const readStream = createReadStream(source);
|
|
228
|
-
const writeStream = createWriteStream(destination);
|
|
229
|
-
try {
|
|
230
|
-
await pipeline(readStream, writeStream);
|
|
231
|
-
}
|
|
232
|
-
catch (error) {
|
|
233
|
-
// Ensure both streams are closed in case of error
|
|
234
|
-
readStream.destroy();
|
|
235
|
-
writeStream.destroy();
|
|
236
|
-
throw error;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Reads a directory and returns file names.
|
|
241
|
-
*
|
|
242
|
-
* @param {string} dirPath - Path to the directory.
|
|
243
|
-
* @param {object} [options] - Options for reading the directory.
|
|
244
|
-
* @param {boolean} [options.fullPaths=false] - Whether to return full paths.
|
|
245
|
-
* @param {RegExp} [options.filter] - Optional regex to filter files.
|
|
246
|
-
* @returns {Promise<string[]>} Array of file names or paths.
|
|
247
|
-
* @throws {Error} If directory cannot be read.
|
|
248
|
-
*/
|
|
249
|
-
export async function readDirectory(dirPath, options = {}) {
|
|
250
|
-
const files = await fs.readdir(dirPath);
|
|
251
|
-
let result = files;
|
|
252
|
-
if (options.filter) {
|
|
253
|
-
result = result.filter(file => options.filter.test(file));
|
|
254
|
-
}
|
|
255
|
-
if (options.fullPaths) {
|
|
256
|
-
result = result.map(file => path.join(dirPath, file));
|
|
257
|
-
}
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Creates a directory if it doesn't exist.
|
|
262
|
-
*
|
|
263
|
-
* @param {string} dirPath - Path to the directory.
|
|
264
|
-
* @param {boolean} [recursive=true] - Whether to create parent directories.
|
|
265
|
-
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
266
|
-
*/
|
|
267
|
-
export async function createDirectory(dirPath, recursive = true) {
|
|
268
|
-
try {
|
|
269
|
-
await fs.mkdir(dirPath, { recursive });
|
|
270
|
-
return true;
|
|
271
|
-
}
|
|
272
|
-
catch (err) {
|
|
273
|
-
// Consider it success if directory already exists
|
|
274
|
-
return err.code === 'EEXIST';
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Safely reads and parses a JSON file with error details.
|
|
279
|
-
*
|
|
280
|
-
* @typeParam T - The expected type of the parsed JSON object.
|
|
281
|
-
* @param {string} path - The path to the JSON file.
|
|
282
|
-
* @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
|
|
283
|
-
*/
|
|
284
|
-
export async function safeReadJsonFile(path) {
|
|
285
|
-
try {
|
|
286
|
-
const content = await fs.readFile(path, 'utf-8');
|
|
287
|
-
try {
|
|
288
|
-
const data = JSON.parse(content);
|
|
289
|
-
return { data, error: null };
|
|
290
|
-
}
|
|
291
|
-
catch (error) {
|
|
292
|
-
return {
|
|
293
|
-
data: null,
|
|
294
|
-
error: new Error(`Invalid JSON in file ${path}: ${error.message}`)
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
catch (error) {
|
|
299
|
-
return {
|
|
300
|
-
data: null,
|
|
301
|
-
error: new Error(`Failed to read file ${path}: ${error.message}`)
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Checks if a path points to a file (not a directory).
|
|
307
|
-
*
|
|
308
|
-
* @param {string} path - Path to check.
|
|
309
|
-
* @returns {Promise<boolean>} True if the path is a file, false otherwise.
|
|
310
|
-
*/
|
|
311
|
-
export async function isFile(path) {
|
|
312
|
-
try {
|
|
313
|
-
const stat = await fs.stat(path);
|
|
314
|
-
return stat.isFile();
|
|
315
|
-
}
|
|
316
|
-
catch {
|
|
317
|
-
return false;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Gets the size of a file in bytes.
|
|
322
|
-
*
|
|
323
|
-
* @param {string} path - Path to the file.
|
|
324
|
-
* @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
|
|
325
|
-
*/
|
|
326
|
-
export async function getFileSize(path) {
|
|
327
|
-
try {
|
|
328
|
-
const stat = await fs.stat(path);
|
|
329
|
-
return stat.size;
|
|
330
|
-
}
|
|
331
|
-
catch {
|
|
332
|
-
return -1;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Reads a file as a Buffer.
|
|
337
|
-
*
|
|
338
|
-
* @param {string} path - Path to the file.
|
|
339
|
-
* @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
|
|
340
|
-
*/
|
|
341
|
-
export async function readFileBuffer(path) {
|
|
342
|
-
try {
|
|
343
|
-
return await fs.readFile(path);
|
|
344
|
-
}
|
|
345
|
-
catch {
|
|
346
|
-
return null;
|
|
347
|
-
}
|
|
348
|
-
}
|