ktfile 0.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/LICENSE +21 -0
- package/README.md +2 -0
- package/index.min.js +1 -0
- package/package.json +44 -0
- package/types/IFile.d.ts +91 -0
- package/types/Utils.d.ts +4 -0
- package/types/async/FileAsync.d.ts +675 -0
- package/types/async/IAsyncFS.d.ts +64 -0
- package/types/kfile.d.ts +7 -0
- package/types/sync/FileSync.d.ts +667 -0
- package/types/sync/ISyncFS.d.ts +57 -0
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
import { IFile } from "../IFile";
|
|
2
|
+
import { IAsyncFS } from "./IAsyncFS";
|
|
3
|
+
import { FileSync } from "../sync/FileSync";
|
|
4
|
+
export declare function pass(x: () => Promise<unknown>): Promise<boolean>;
|
|
5
|
+
export declare function ret<T>(x: () => Promise<T>): Promise<T | null>;
|
|
6
|
+
export declare class FileAsync extends IFile<IAsyncFS> {
|
|
7
|
+
static fs: IAsyncFS;
|
|
8
|
+
static createTempFile(directory: FileAsync, prefix?: string, suffix?: string): FileAsync;
|
|
9
|
+
get fs(): IAsyncFS;
|
|
10
|
+
/**
|
|
11
|
+
* @description Checks if the file is executable.
|
|
12
|
+
* A file is considered executable if it has the execute permission set.
|
|
13
|
+
* This method checks if the file can be executed by the current user.
|
|
14
|
+
* @example
|
|
15
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
16
|
+
* if (await file.canExecute()) {
|
|
17
|
+
* console.log("This file is executable.");
|
|
18
|
+
* } else {
|
|
19
|
+
* console.log("This file is not executable.");
|
|
20
|
+
* }
|
|
21
|
+
* @returns {Promise<boolean>} True if the file is executable, false otherwise.
|
|
22
|
+
* If the file does not exist or cannot be accessed, it will throw an error.
|
|
23
|
+
*/
|
|
24
|
+
canExecute(): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* @description Sets the executable permission for the file.
|
|
27
|
+
* If `value` is true, it sets the permission to allow execution.
|
|
28
|
+
* If `value` is false, it sets the permission to disallow execution.
|
|
29
|
+
* @example
|
|
30
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
31
|
+
* await file.setExecutable(); // Sets the file to be executable
|
|
32
|
+
* await file.setExecutable(false); // Sets the file to be non-executable
|
|
33
|
+
* @param {boolean} value - Whether to allow execution (true) or disallow execution (false).
|
|
34
|
+
* @returns {Promise<FileAsync | null>} A promise that resolves to the FileAsync object if the operation was successful,
|
|
35
|
+
* or null if the file does not exist or the operation failed.
|
|
36
|
+
*/
|
|
37
|
+
setExecutable(value?: boolean): Promise<FileAsync | null>;
|
|
38
|
+
/**
|
|
39
|
+
* @description Checks if the file can be read.
|
|
40
|
+
* A file is considered readable if it has the read permission set.
|
|
41
|
+
* This method checks if the file can be accessed for reading by the current user.
|
|
42
|
+
* @example
|
|
43
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
44
|
+
* if (await file.canRead()) {
|
|
45
|
+
* console.log("This file can be read.");
|
|
46
|
+
* } else {
|
|
47
|
+
* console.log("This file cannot be read.");
|
|
48
|
+
* }
|
|
49
|
+
* @returns {Promise<boolean>} True if the file can be read, false otherwise.
|
|
50
|
+
* If the file does not exist or cannot be accessed, it will throw an error.
|
|
51
|
+
*/
|
|
52
|
+
canRead(): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* @description Sets the read permission for the file.
|
|
55
|
+
* If `value` is true, it sets the permission to allow reading.
|
|
56
|
+
* If `value` is false, it sets the permission to disallow reading.
|
|
57
|
+
* @example
|
|
58
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
59
|
+
* await file.setReadable(); // Sets the file to be readable
|
|
60
|
+
* await file.setReadable(false); // Sets the file to be non-readable
|
|
61
|
+
* @param {boolean} value - Whether to allow reading (true) or disallow reading (false).
|
|
62
|
+
* @returns {Promise<FileAsync | null>} A promise that resolves to the FileAsync object if the operation was successful,
|
|
63
|
+
* or null if the file does not exist or the operation failed.
|
|
64
|
+
*/
|
|
65
|
+
setReadable(value?: boolean): Promise<FileAsync | null>;
|
|
66
|
+
/**
|
|
67
|
+
* @description Checks if the file can be written to.
|
|
68
|
+
* A file is considered writable if it has the write permission set.
|
|
69
|
+
* This method checks if the file can be accessed for writing by the current user.
|
|
70
|
+
* @example
|
|
71
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
72
|
+
* if (await file.canWrite()) {
|
|
73
|
+
* console.log("This file can be written to.");
|
|
74
|
+
* } else {
|
|
75
|
+
* console.log("This file cannot be written to.");
|
|
76
|
+
* }
|
|
77
|
+
* @returns {Promise<boolean>} True if the file can be written to, false otherwise.
|
|
78
|
+
* If the file does not exist or cannot be accessed, it will throw an error.
|
|
79
|
+
*/
|
|
80
|
+
canWrite(): Promise<boolean>;
|
|
81
|
+
/**
|
|
82
|
+
* @description Sets the write permission for the file.
|
|
83
|
+
* If `value` is true, it sets the permission to allow writing.
|
|
84
|
+
* If `value` is false, it sets the permission to disallow writing.
|
|
85
|
+
* @example
|
|
86
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
87
|
+
* await file.setWritable(); // Sets the file to be writable
|
|
88
|
+
* await file.setWritable(false); // Sets the file to be non-writable
|
|
89
|
+
* @param {boolean} value - Whether to allow writing (true) or disallow writing (false).
|
|
90
|
+
* @returns {Promise<FileAsync | null>} A promise that resolves to the FileAsync object if the operation was successful,
|
|
91
|
+
* or null if the file does not exist or the operation failed.
|
|
92
|
+
*/
|
|
93
|
+
setWritable(value?: boolean): Promise<FileAsync | null>;
|
|
94
|
+
/**
|
|
95
|
+
* @description Gets the creation time of the file.
|
|
96
|
+
* The creation time is the time when the file was created in the file system.
|
|
97
|
+
* If the file does not exist, it returns null.
|
|
98
|
+
* @example
|
|
99
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
100
|
+
* console.log("Creation time:", await file.creationTime());
|
|
101
|
+
* // Output: Creation time: 2023-10-01T12:00:00.000Z
|
|
102
|
+
* @returns {Promise<Date | null>} The creation time of the file as a Date object, or null if the file does not exist.
|
|
103
|
+
*/
|
|
104
|
+
creationTime(): Promise<Date | null>;
|
|
105
|
+
/**
|
|
106
|
+
* @description Sets the creation time of the file.
|
|
107
|
+
* This method allows you to change the creation time of the file to a specified Date.
|
|
108
|
+
* If the file does not exist, it will throw an error.
|
|
109
|
+
* @example
|
|
110
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
111
|
+
* await file.setCreationTime(new Date("2023-10-01T12:00:00.000Z"));
|
|
112
|
+
* // Sets the creation time of the file to the specified date.
|
|
113
|
+
* @param {Date} value - The new creation time to set for the file.
|
|
114
|
+
* @returns {Promise<FileAsync | null>} A promise that resolves when the creation time is set successfully.
|
|
115
|
+
*/
|
|
116
|
+
setCreationTime(value: Date): Promise<FileAsync | null>;
|
|
117
|
+
/**
|
|
118
|
+
* @description Gets the last modified time of the file.
|
|
119
|
+
* The last modified time is the time when the file was last modified in the file system.
|
|
120
|
+
* If the file does not exist, it returns null.
|
|
121
|
+
* @example
|
|
122
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
123
|
+
* console.log("Last modified time:", await file.lastModified());
|
|
124
|
+
* // Output: Last modified time: 2023-10-01T12:00:00.000Z
|
|
125
|
+
* @returns {Promise<Date | null>} The last modified time of the file as a Date object, or null if the file does not exist.
|
|
126
|
+
*/
|
|
127
|
+
lastModified(): Promise<Date | null>;
|
|
128
|
+
/**
|
|
129
|
+
* @description Sets the last modified time of the file.
|
|
130
|
+
* This method allows you to change the last modified time of the file to a specified Date.
|
|
131
|
+
* If the file does not exist, it will throw an error.
|
|
132
|
+
* @example
|
|
133
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
134
|
+
* await file.setLastModified(new Date("2023-10-01T12:00:00.000Z"));
|
|
135
|
+
* // Sets the last modified time of the file to the specified date.
|
|
136
|
+
* @param {Date} value - The new last modified time to set for the file.
|
|
137
|
+
* @returns {Promise<FileAsync | null>} A promise that resolves when the last modified time is set successfully.
|
|
138
|
+
*/
|
|
139
|
+
setLastModified(value: Date): Promise<FileAsync | null>;
|
|
140
|
+
/**
|
|
141
|
+
* @description Checks if the file exists.
|
|
142
|
+
* This method checks if the file or directory at the specified path exists in the file system.
|
|
143
|
+
* If the file does not exist, it returns null.
|
|
144
|
+
* @example
|
|
145
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
146
|
+
* console.log("File exists:", await file.exists());
|
|
147
|
+
* // Output: File exists: true
|
|
148
|
+
* @returns {Promise<boolean | null>} True if the file exists, false if it does not exist, or null if an error occurs.
|
|
149
|
+
*/
|
|
150
|
+
exists(): Promise<boolean | null>;
|
|
151
|
+
/**
|
|
152
|
+
* @description Gets the last access time of the file.
|
|
153
|
+
* The last access time is the time when the file was last accessed in the file system.
|
|
154
|
+
* If the file does not exist, it returns null.
|
|
155
|
+
* @example
|
|
156
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
157
|
+
* console.log("Last access time:", await file.lastAccess());
|
|
158
|
+
* // Output: Last access time: 2023-10-01T12:00:00.000Z
|
|
159
|
+
* @returns {Promise<Date | null>} The last access time of the file as a Date object, or null if the file does not exist.
|
|
160
|
+
*/
|
|
161
|
+
lastAccess(): Promise<Date | null>;
|
|
162
|
+
/**
|
|
163
|
+
* @description Gets the name of the file.
|
|
164
|
+
* The name is the last part of the file path, which is typically the file name with its extension.
|
|
165
|
+
* If the file path ends with a separator, it returns an empty string.
|
|
166
|
+
* @example
|
|
167
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
168
|
+
* console.log("File name:", file.name);
|
|
169
|
+
* // Output: File name: file.txt
|
|
170
|
+
* @returns {string} The name of the file.
|
|
171
|
+
*/
|
|
172
|
+
get name(): string;
|
|
173
|
+
/**
|
|
174
|
+
* @description Gets the file name without the extension.
|
|
175
|
+
* The name is the part of the file name before the last dot (.).
|
|
176
|
+
* If there is no dot in the file name, it returns the full name.
|
|
177
|
+
* @example
|
|
178
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
179
|
+
* console.log("File name without extension:", file.nameWithoutExtension);
|
|
180
|
+
* // Output: File name without extension: file
|
|
181
|
+
* @returns {string} The file name without the extension.
|
|
182
|
+
* If the file name does not contain a dot, it returns the full name.
|
|
183
|
+
*/
|
|
184
|
+
get nameWithoutExtension(): string;
|
|
185
|
+
/**
|
|
186
|
+
* @description Gets the file extension.
|
|
187
|
+
* The extension is the part of the file name after the last dot (.)
|
|
188
|
+
* If there is no dot in the file name, it returns an empty string.
|
|
189
|
+
* @example
|
|
190
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
191
|
+
* console.log("File extension:", file.extension);
|
|
192
|
+
* // Output: File extension: txt
|
|
193
|
+
* @returns {string} The file extension, or an empty string if there is no extension.
|
|
194
|
+
*/
|
|
195
|
+
get extension(): string;
|
|
196
|
+
/**
|
|
197
|
+
* @description Gets the parent directory of the file.
|
|
198
|
+
* If the file is in the root directory, it returns null.
|
|
199
|
+
* @example
|
|
200
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
201
|
+
* const parent = file.parent;
|
|
202
|
+
* if (parent) {
|
|
203
|
+
* console.log("Parent directory:", parent.path);
|
|
204
|
+
* } else {
|
|
205
|
+
* console.log("This file is in the root directory.");
|
|
206
|
+
* }
|
|
207
|
+
* @returns {FileAsync | null} A new FileAsync object representing the parent directory of the file,
|
|
208
|
+
* or null if the file is in the root directory.
|
|
209
|
+
*/
|
|
210
|
+
get parent(): FileAsync | null;
|
|
211
|
+
/**
|
|
212
|
+
* @description Gets the URI of the file.
|
|
213
|
+
* The URI is a string that represents the file's location in the file system.
|
|
214
|
+
* It is typically in the format `file:///path/to/file`.
|
|
215
|
+
* @example
|
|
216
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
217
|
+
* console.log("File URI:", file.uri);
|
|
218
|
+
* // Output: File URI: file:///path/to/file.txt
|
|
219
|
+
* @returns {string} The URI of the file.
|
|
220
|
+
*/
|
|
221
|
+
get uri(): string;
|
|
222
|
+
/**
|
|
223
|
+
* @description Gets the path separator used by the file system.
|
|
224
|
+
* This is typically the forward slash (/) on Unix-like systems and the backslash (\) on Windows.
|
|
225
|
+
* @example
|
|
226
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
227
|
+
* console.log("Path separator:", file.separator);
|
|
228
|
+
* // Output: Path separator: /
|
|
229
|
+
* @returns {string} The path separator used by the file system.
|
|
230
|
+
*/
|
|
231
|
+
get separator(): string;
|
|
232
|
+
/**
|
|
233
|
+
* @description Checks if the file is a directory.
|
|
234
|
+
* A directory is a file that contains other files or directories.
|
|
235
|
+
* @example
|
|
236
|
+
* const dir = new FileAsync("path/to/directory");
|
|
237
|
+
* if (await dir.isDirectory()) {
|
|
238
|
+
* console.log("This is a directory.");
|
|
239
|
+
* } else {
|
|
240
|
+
* console.log("This is not a directory.");
|
|
241
|
+
* }
|
|
242
|
+
* @returns {Promise<boolean | null>} True if the file is a directory, false if it is not, or null if the file does not exist.
|
|
243
|
+
*/
|
|
244
|
+
isDirectory(): Promise<boolean | null>;
|
|
245
|
+
/**
|
|
246
|
+
* @description Checks if the file is a regular file.
|
|
247
|
+
* A regular file is a file that is not a directory or a symbolic link.
|
|
248
|
+
* @example
|
|
249
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
250
|
+
* if (await file.isFile()) {
|
|
251
|
+
* console.log("This is a regular file.");
|
|
252
|
+
* } else {
|
|
253
|
+
* console.log("This is not a regular file.");
|
|
254
|
+
* }
|
|
255
|
+
* @returns {Promise<boolean | null>} True if the file is a regular file, false if it is not, or null if the file does not exist.
|
|
256
|
+
*/
|
|
257
|
+
isFile(): Promise<boolean | null>;
|
|
258
|
+
/**
|
|
259
|
+
* @description Checks if the file is hidden.
|
|
260
|
+
* A file is considered hidden if its name starts with a dot (.) or if any part of its path starts with a dot.
|
|
261
|
+
* @example
|
|
262
|
+
* const file = new FileAsync("path/to/.hiddenfile");
|
|
263
|
+
* if (file.isHidden) {
|
|
264
|
+
* console.log("This file is hidden.");
|
|
265
|
+
* } else {
|
|
266
|
+
* console.log("This file is not hidden.");
|
|
267
|
+
* }
|
|
268
|
+
* @returns {boolean} True if the file is hidden, false otherwise.
|
|
269
|
+
*/
|
|
270
|
+
get isHidden(): boolean;
|
|
271
|
+
/**
|
|
272
|
+
* @description Checks if the file is a symbolic link.
|
|
273
|
+
* @example
|
|
274
|
+
* const file = new FileAsync("path/to/symlink");
|
|
275
|
+
* if (await file.isSymbolicLink()) {
|
|
276
|
+
* console.log("This is a symbolic link.");
|
|
277
|
+
* } else {
|
|
278
|
+
* console.log("This is not a symbolic link.");
|
|
279
|
+
* }
|
|
280
|
+
* @returns {Promise<boolean | null>} True if the file is a symbolic link, false if it is not, or null if the file does not exist.
|
|
281
|
+
*/
|
|
282
|
+
isSymbolicLink(): Promise<boolean | null>;
|
|
283
|
+
/**
|
|
284
|
+
* @description Gets the size of the file in bytes.
|
|
285
|
+
* @example
|
|
286
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
287
|
+
* console.log("File size:", await file.size(), "bytes");
|
|
288
|
+
* @returns {Promise<number | null>} The size of the file in bytes, or null if the file does not exist.
|
|
289
|
+
*/
|
|
290
|
+
size(): Promise<number | null>;
|
|
291
|
+
/**
|
|
292
|
+
* @description Gets the size of the file in kilobytes (KB).
|
|
293
|
+
* This is calculated by dividing the size in bytes by 1024.
|
|
294
|
+
* If the file does not exist, it returns null.
|
|
295
|
+
* @example
|
|
296
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
297
|
+
* console.log("File size:", await file.sizeKB(), "KB");
|
|
298
|
+
* @returns {Promise<number | null>} The size of the file in kilobytes (KB), or null if the file does not exist.
|
|
299
|
+
*/
|
|
300
|
+
sizeKB(): Promise<number | null>;
|
|
301
|
+
/**
|
|
302
|
+
* @description Gets the size of the file in megabytes (MB).
|
|
303
|
+
* This is calculated by dividing the size in bytes by 1024 * 1024.
|
|
304
|
+
* If the file does not exist, it returns null.
|
|
305
|
+
* @example
|
|
306
|
+
* const file = new FileSync("path/to/file.txt");
|
|
307
|
+
* console.log("File size:", file.sizeMB, "MB");
|
|
308
|
+
* @returns {Promise<number | null>} The size of the file in megabytes (MB), or null if the file does not exist.
|
|
309
|
+
*/
|
|
310
|
+
sizeMB(): Promise<number | null>;
|
|
311
|
+
/**
|
|
312
|
+
* @description Gets the size of the file in gigabytes (GB).
|
|
313
|
+
* This is calculated by dividing the size in bytes by 1024 * 1024 * 1024.
|
|
314
|
+
* If the file does not exist, it returns null.
|
|
315
|
+
* @example
|
|
316
|
+
* const file = new FileSync("path/to/file.txt");
|
|
317
|
+
* console.log("File size:", file.sizeGB, "GB");
|
|
318
|
+
* @returns {Promise<number | null>} The size of the file in gigabytes (GB), or null if the file does not exist.
|
|
319
|
+
*/
|
|
320
|
+
sizeGB(): Promise<number | null>;
|
|
321
|
+
/**
|
|
322
|
+
* @description Creates a new FileAsync object with the specified paths appended to the current file's path.
|
|
323
|
+
* This method allows you to create a new file or directory relative to the current file's path.
|
|
324
|
+
* @example
|
|
325
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
326
|
+
* const newFile = file.to("subdir", "newfile.txt");
|
|
327
|
+
* console.log("New file path:", newFile.path);
|
|
328
|
+
* // Output: New file path: path/to/file.txt/subdir/newfile.txt
|
|
329
|
+
* @param {...string} paths - The paths to append to the current file's path.
|
|
330
|
+
* @returns {FileAsync} A new FileAsync object with the updated path.
|
|
331
|
+
*/
|
|
332
|
+
to(...paths: string[]): FileAsync;
|
|
333
|
+
/**
|
|
334
|
+
* @description Checks if the current file path contains the specified path.
|
|
335
|
+
* This method checks if the current file's path is a parent of the specified path.
|
|
336
|
+
* @example
|
|
337
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
338
|
+
* const subFile = new FileAsync("path/to/file.txt/subdir/subfile.txt");
|
|
339
|
+
* console.log(file.contains(subFile)); // Output: true
|
|
340
|
+
* @param {FileAsync} path - The FileAsync object to check against the current file's path.
|
|
341
|
+
* @returns {boolean} True if the current file's path contains the specified path, false otherwise.
|
|
342
|
+
*/
|
|
343
|
+
contains(path: FileAsync): boolean;
|
|
344
|
+
/**
|
|
345
|
+
* @description Creates a new file if it does not exist.
|
|
346
|
+
* @example
|
|
347
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
348
|
+
* if (await file.createFile()) {
|
|
349
|
+
* console.log("File created or already exists.");
|
|
350
|
+
* } else {
|
|
351
|
+
* console.log("Failed to create file.");
|
|
352
|
+
* }
|
|
353
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the file was created successfully or already exists,
|
|
354
|
+
* or null if the file could not be created.
|
|
355
|
+
*/
|
|
356
|
+
createFile(): Promise<FileAsync | null>;
|
|
357
|
+
/**
|
|
358
|
+
* @description Deletes the file or directory.
|
|
359
|
+
* If the file is a directory and `recursive` is true, it will delete all files and subdirectories within it.
|
|
360
|
+
* If `force` is true, it will ignore errors when deleting files or directories.
|
|
361
|
+
* @example
|
|
362
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
363
|
+
* if (await file.delete(true)) {
|
|
364
|
+
* console.log("File deleted successfully.");
|
|
365
|
+
* } else {
|
|
366
|
+
* console.log("Failed to delete file.");
|
|
367
|
+
* }
|
|
368
|
+
* @param {boolean} recursive - Whether to delete the directory and its contents recursively.
|
|
369
|
+
* @param {boolean} [force=false] - Whether to force the deletion, ignoring errors.
|
|
370
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the file or directory was deleted successfully,
|
|
371
|
+
* or null if the file or directory does not exist or could not be deleted.
|
|
372
|
+
*/
|
|
373
|
+
delete(recursive?: boolean, force?: boolean): Promise<FileAsync | null>;
|
|
374
|
+
/**
|
|
375
|
+
* @description Schedules the file or directory for deletion on exit.
|
|
376
|
+
* This method adds the file or directory to a queue that will be processed when the process
|
|
377
|
+
* exits. If `recursive` is true, it will delete all files and subdirectories within it.
|
|
378
|
+
* @example
|
|
379
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
380
|
+
* file.deleteOnExit(true);
|
|
381
|
+
* // The file will be deleted when the process exits.
|
|
382
|
+
* @param {boolean} [recursive=false] - Whether to delete the directory and its contents recursively.
|
|
383
|
+
* @returns {void}
|
|
384
|
+
*/
|
|
385
|
+
deleteOnExit(recursive?: boolean): void;
|
|
386
|
+
/**
|
|
387
|
+
* @description Clears the contents of the file or directory.
|
|
388
|
+
* If the file is a directory and `recursive` is true, it will delete all
|
|
389
|
+
* files and subdirectories within it.
|
|
390
|
+
* If the file is not a directory, it will write an empty string to the file
|
|
391
|
+
* to clear its contents.
|
|
392
|
+
* @example
|
|
393
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
394
|
+
* if (await file.clear(true)) {
|
|
395
|
+
* console.log("Directory cleared successfully.");
|
|
396
|
+
* } else {
|
|
397
|
+
* console.log("Failed to clear directory.");
|
|
398
|
+
* }
|
|
399
|
+
* @param {boolean} recursive - Whether to clear the directory and its contents recursively.
|
|
400
|
+
* @return {Promise<FileAsync | null>} The FileAsync object if the directory was cleared successfully,
|
|
401
|
+
* or null if the file is not a directory or could not be cleared.
|
|
402
|
+
*/
|
|
403
|
+
clear(recursive: boolean): Promise<FileAsync | null>;
|
|
404
|
+
/**
|
|
405
|
+
* @description Lists all files in the directory.
|
|
406
|
+
* If the file is not a directory, it returns null.
|
|
407
|
+
* @example
|
|
408
|
+
* const dir = new FileAsync("path/to/directory");
|
|
409
|
+
* const files = await dir.listFiles();
|
|
410
|
+
* if (files) {
|
|
411
|
+
* files.forEach(file => console.log(file.name));
|
|
412
|
+
* } else {
|
|
413
|
+
* console.log("Not a directory or no files found.");
|
|
414
|
+
* }
|
|
415
|
+
* @returns {Promise<FileAsync[] | null>} An array of FileAsync objects representing the files in the directory,
|
|
416
|
+
* or null if the file is not a directory.
|
|
417
|
+
*/
|
|
418
|
+
listFiles(): Promise<FileAsync[] | null>;
|
|
419
|
+
/**
|
|
420
|
+
* @description Reads the contents of the directory.
|
|
421
|
+
* If the file is not a directory, it returns null.
|
|
422
|
+
* @example
|
|
423
|
+
* const dir = new FileAsync("path/to/directory");
|
|
424
|
+
* const contents = await dir.listFilenames();
|
|
425
|
+
* if (contents) {
|
|
426
|
+
* contents.forEach(item => console.log(item));
|
|
427
|
+
* } else {
|
|
428
|
+
* console.log("Not a directory or no contents found.");
|
|
429
|
+
* }
|
|
430
|
+
* @returns {Promise<string[] | null>} An array of strings representing the names of the files and directories
|
|
431
|
+
* in the directory, or null if the file is not a directory.
|
|
432
|
+
*/
|
|
433
|
+
listFilenames(): Promise<string[] | null>;
|
|
434
|
+
/**
|
|
435
|
+
* @description Creates a directory at the specified path.
|
|
436
|
+
* If the directory already exists, it does nothing.
|
|
437
|
+
* If `recursive` is true, it will create all necessary parent directories.
|
|
438
|
+
* @example
|
|
439
|
+
* const dir = new FileAsync("path/to/directory");
|
|
440
|
+
* if (await dir.mkdir()) {
|
|
441
|
+
* console.log("Directory created successfully.");
|
|
442
|
+
* } else {
|
|
443
|
+
* console.log("Failed to create directory.");
|
|
444
|
+
* }
|
|
445
|
+
* @param {boolean} [recursive=false] - Whether to create parent directories if they do not exist.
|
|
446
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the directory was created successfully,
|
|
447
|
+
* or null if the directory could not be created.
|
|
448
|
+
*/
|
|
449
|
+
mkdir(recursive?: boolean): Promise<FileAsync | null>;
|
|
450
|
+
/**
|
|
451
|
+
* @description Creates the directory and all necessary parent directories.
|
|
452
|
+
* If the directory already exists, it does nothing.
|
|
453
|
+
* This is a convenience method that calls `mkdir` with `recursive` set to true.
|
|
454
|
+
* @example
|
|
455
|
+
* const dir = new FileAsync("path/to/directory");
|
|
456
|
+
* if (await dir.mkdirs()) {
|
|
457
|
+
* console.log("Directories created successfully.");
|
|
458
|
+
* } else {
|
|
459
|
+
* console.log("Failed to create directories.");
|
|
460
|
+
* }
|
|
461
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the directories were created successfully,
|
|
462
|
+
*/
|
|
463
|
+
mkdirs(): Promise<FileAsync | null>;
|
|
464
|
+
/**
|
|
465
|
+
* @description Renames the file or directory to the specified destination.
|
|
466
|
+
* If the destination already exists and `overwrite` is false, it will not overwrite it.
|
|
467
|
+
* If `recursive` is true, it will create parent directories if they do not exist.
|
|
468
|
+
* If the source and destination paths are the same, it does nothing and returns the current FileAsync object.
|
|
469
|
+
* @example
|
|
470
|
+
* const file = new FileAsync("path/to/source.txt");
|
|
471
|
+
* const dest = new FileAsync("path/to/destination.txt");
|
|
472
|
+
* if (await file.renameTo(dest, true)) {
|
|
473
|
+
* console.log("File renamed successfully.");
|
|
474
|
+
* } else {
|
|
475
|
+
* console.log("Failed to rename file.");
|
|
476
|
+
* }
|
|
477
|
+
* @param {FileAsync} dest - The destination directory to rename to.
|
|
478
|
+
* @param {boolean} [overwrite=false] - Whether to overwrite the destination if it already exists.
|
|
479
|
+
* @param {boolean} [recursive=false] - Whether to create parent directories if they do not exist and
|
|
480
|
+
* delete the destination directory recursively if it already exists.
|
|
481
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the directory was renamed successfully,
|
|
482
|
+
* or null if the directory could not be renamed.
|
|
483
|
+
*/
|
|
484
|
+
renameTo(dest: FileAsync, overwrite?: boolean, recursive?: boolean): Promise<FileAsync | null>;
|
|
485
|
+
/**
|
|
486
|
+
* @description Copies the file to the specified destination.
|
|
487
|
+
* If the destination file already exists and `overwrite` is false, it will not overwrite it.
|
|
488
|
+
* If `recursive` is true, it will copy the contents of the directory recursively.
|
|
489
|
+
* @example
|
|
490
|
+
* const file = new FileAsync("path/to/source.txt");
|
|
491
|
+
* const dest = new FileAsync("path/to/destination.txt");
|
|
492
|
+
* if (await file.copyTo(dest, true)) {
|
|
493
|
+
* console.log("File copied successfully.");
|
|
494
|
+
* } else {
|
|
495
|
+
* console.log("Failed to copy file.");
|
|
496
|
+
* }
|
|
497
|
+
* @param {FileAsync} dest - The destination file to copy to.
|
|
498
|
+
* @param {boolean} [overwrite=false] - Whether to overwrite the destination file if it already exists.
|
|
499
|
+
* @param {boolean} [recursive=false] - Whether to copy the contents of the directory recursively and
|
|
500
|
+
* delete the destination directory recursively if it already exists.
|
|
501
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the file was copied successfully,
|
|
502
|
+
* or null if the file could not be copied.
|
|
503
|
+
*/
|
|
504
|
+
copyTo(dest: FileAsync, overwrite?: boolean, recursive?: boolean): Promise<FileAsync | null>;
|
|
505
|
+
/**
|
|
506
|
+
* @description Walks through the directory and yields each file.
|
|
507
|
+
* If the file is a directory, it will yield the directory and then recursively yield all files within it.
|
|
508
|
+
* If the file is not a directory, it will yield the file itself.
|
|
509
|
+
* @example
|
|
510
|
+
* const dir = new FileAsync("path/to/directory");
|
|
511
|
+
* for await (const file of dir.walk()) {
|
|
512
|
+
* console.log(file.name);
|
|
513
|
+
* }
|
|
514
|
+
* @returns {AsyncGenerator<FileAsync>} A generator that yields FileAsync objects representing the files
|
|
515
|
+
* in the directory and its subdirectories.
|
|
516
|
+
* If the file is not a directory, it will yield the file itself.
|
|
517
|
+
* @yields {FileAsync} Each file in the directory and its subdirectories.
|
|
518
|
+
*/
|
|
519
|
+
walk(): AsyncGenerator<FileAsync>;
|
|
520
|
+
/**
|
|
521
|
+
* @description Reads the contents of the file.
|
|
522
|
+
* If the file is a text file, it will return the contents as a string.
|
|
523
|
+
* If the file is a binary file, it will return the contents as a Buffer.
|
|
524
|
+
* @example
|
|
525
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
526
|
+
* const contents = await file.read("utf8");
|
|
527
|
+
* if (contents) {
|
|
528
|
+
* console.log("File contents:", contents);
|
|
529
|
+
* } else {
|
|
530
|
+
* console.log("Failed to read file.");
|
|
531
|
+
* }
|
|
532
|
+
* @param {BufferEncoding} [encoding="utf8"] - The encoding to use when reading the file.
|
|
533
|
+
* If not specified, it will return a Buffer for binary files.
|
|
534
|
+
* If specified, it will return a string for text files.
|
|
535
|
+
* @returns {Promise<string | Buffer>} The contents of the file as a string if encoding is specified,
|
|
536
|
+
* or as a Buffer if encoding is not specified.
|
|
537
|
+
* If reading fails, it will return null.
|
|
538
|
+
*/
|
|
539
|
+
read(encoding?: BufferEncoding): Promise<string | null>;
|
|
540
|
+
/**
|
|
541
|
+
* @description Reads the contents of the file as a Buffer.
|
|
542
|
+
* If the file is a binary file, it will return the contents as a Buffer.
|
|
543
|
+
* If the file is a text file, it will return the contents as a Buffer.
|
|
544
|
+
* @example
|
|
545
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
546
|
+
* const contents = await file.read();
|
|
547
|
+
* if (contents) {
|
|
548
|
+
* console.log("File contents as Buffer:", contents);
|
|
549
|
+
* } else {
|
|
550
|
+
* console.log("Failed to read file.");
|
|
551
|
+
* }
|
|
552
|
+
* @returns {Promise<Buffer>} The contents of the file as a Buffer.
|
|
553
|
+
* If reading fails, it will return null.
|
|
554
|
+
*/
|
|
555
|
+
read(): Promise<Buffer | null>;
|
|
556
|
+
/**
|
|
557
|
+
* @description Reads the contents of the file as an array of lines.
|
|
558
|
+
* If the file is a text file, it will split the contents by new lines.
|
|
559
|
+
* If the file is a binary file, it will return null.
|
|
560
|
+
* @example
|
|
561
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
562
|
+
* const lines = await file.readLines("utf8");
|
|
563
|
+
* if (lines) {
|
|
564
|
+
* lines.forEach(line => console.log(line));
|
|
565
|
+
* } else {
|
|
566
|
+
* console.log("Failed to read file or not a text file.");
|
|
567
|
+
* }
|
|
568
|
+
* @param {BufferEncoding} [encoding="utf8"] - The encoding to use when reading the file.
|
|
569
|
+
* If not specified, it will return null for binary files.
|
|
570
|
+
* @returns {Promise<string[] | null>} An array of strings representing the lines in the file,
|
|
571
|
+
* or null if the file is a binary file or if reading failed.
|
|
572
|
+
*/
|
|
573
|
+
readLines(encoding?: BufferEncoding): Promise<string[] | null>;
|
|
574
|
+
/**
|
|
575
|
+
* @description Reads the contents of the file as JSON.
|
|
576
|
+
* If the file is a valid JSON file, it will parse and return the contents as an object.
|
|
577
|
+
* If the file is not a valid JSON file or reading fails, it will return null.
|
|
578
|
+
* @example
|
|
579
|
+
* const file = new FileAsync("path/to/file.json");
|
|
580
|
+
* const jsonData = await file.readJSON();
|
|
581
|
+
* if (jsonData) {
|
|
582
|
+
* console.log("JSON data:", jsonData);
|
|
583
|
+
* } else {
|
|
584
|
+
* console.log("Failed to read JSON or not a valid JSON file.");
|
|
585
|
+
* }
|
|
586
|
+
* @returns {Promise} The parsed JSON data as an object of type T, or null if reading failed or not valid JSON.
|
|
587
|
+
*/
|
|
588
|
+
readJSON<T = unknown>(): Promise<T | null>;
|
|
589
|
+
/**
|
|
590
|
+
* @description Reads the symbolic link target of the file.
|
|
591
|
+
* If the file is a symbolic link, it will return a new FileAsync object pointing to the target.
|
|
592
|
+
* If the file is not a symbolic link, it will return null.
|
|
593
|
+
* @example
|
|
594
|
+
* const link = new FileAsync("path/to/symlink");
|
|
595
|
+
* const target = await link.readlink();
|
|
596
|
+
* if (target) {
|
|
597
|
+
* console.log("Symbolic link points to:", target.path);
|
|
598
|
+
* } else {
|
|
599
|
+
* console.log("Not a symbolic link or reading failed.");
|
|
600
|
+
* }
|
|
601
|
+
* @returns {Promise<FileAsync | null>} A new FileAsync object pointing to the target of the symbolic link,
|
|
602
|
+
* or null if the file is not a symbolic link.
|
|
603
|
+
*/
|
|
604
|
+
readlink(): Promise<FileAsync | null>;
|
|
605
|
+
/**
|
|
606
|
+
* @description Writes data to the file.
|
|
607
|
+
* If the file does not exist, it will create the file.
|
|
608
|
+
* If the file exists, it will overwrite the contents with the provided data.
|
|
609
|
+
* @example
|
|
610
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
611
|
+
* if (await file.write("Hello, World!", "utf8")) {
|
|
612
|
+
* console.log("Data written successfully.");
|
|
613
|
+
* } else {
|
|
614
|
+
* console.log("Failed to write data.");
|
|
615
|
+
* }
|
|
616
|
+
* @param data - The data to write to the file.
|
|
617
|
+
* @param {Object} encoding - The encoding to use when writing the data.
|
|
618
|
+
* @returns {Promise<FileAsync | null>} True if the data was written successfully, false otherwise.
|
|
619
|
+
*/
|
|
620
|
+
write<T>(data: T, encoding: {
|
|
621
|
+
write(): T;
|
|
622
|
+
}): Promise<FileAsync | null>;
|
|
623
|
+
/**
|
|
624
|
+
* @description Writes data to the file.
|
|
625
|
+
* If the file does not exist, it will create the file.
|
|
626
|
+
* If the file exists, it will overwrite the contents with the provided data.
|
|
627
|
+
* @example
|
|
628
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
629
|
+
* if (await file.write("Hello, World!", "utf8")) {
|
|
630
|
+
* console.log("Data written successfully.");
|
|
631
|
+
* } else {
|
|
632
|
+
* console.log("Failed to write data.");
|
|
633
|
+
* }
|
|
634
|
+
* @param {string | Buffer} data - The data to write to the file.
|
|
635
|
+
* @param {BufferEncoding} [encoding="utf8"] - The encoding to use when writing the data.
|
|
636
|
+
* If not specified, it will write the data as a Buffer.
|
|
637
|
+
* @returns {Promise<FileAsync | null>} True if the data was written successfully, false otherwise.
|
|
638
|
+
*/
|
|
639
|
+
write(data: string | Buffer, encoding?: BufferEncoding): Promise<FileAsync | null>;
|
|
640
|
+
/**
|
|
641
|
+
* @description Writes JSON data to the file.
|
|
642
|
+
* If the file does not exist, it will create the file.
|
|
643
|
+
* If the file exists, it will overwrite the contents with the provided JSON data.
|
|
644
|
+
* @example
|
|
645
|
+
* const file = new FileAsync("path/to/file.json");
|
|
646
|
+
* const jsonData = { key: "value" };
|
|
647
|
+
* if (await file.writeJSON(jsonData)) {
|
|
648
|
+
* console.log("JSON data written successfully.");
|
|
649
|
+
* } else {
|
|
650
|
+
* console.log("Failed to write JSON data.");
|
|
651
|
+
* }
|
|
652
|
+
* @param {unknown} data - The JSON data to write to the file.
|
|
653
|
+
* @returns {boolean} True if the JSON data was written successfully, false otherwise.
|
|
654
|
+
*/
|
|
655
|
+
writeJSON(data: unknown): Promise<FileAsync | null>;
|
|
656
|
+
/**
|
|
657
|
+
* @description Appends data to the file.
|
|
658
|
+
* If the file does not exist, it will create the file.
|
|
659
|
+
* If the file exists, it will append the data to the end of the file.
|
|
660
|
+
* @example
|
|
661
|
+
* const file = new FileAsync("path/to/file.txt");
|
|
662
|
+
* if (await file.append("Hello, World!", "utf8")) {
|
|
663
|
+
* console.log("Data appended successfully.");
|
|
664
|
+
* } else {
|
|
665
|
+
* console.log("Failed to append data.");
|
|
666
|
+
* }
|
|
667
|
+
* @param {string | Buffer} data - The data to append to the file.
|
|
668
|
+
* @param {BufferEncoding} [encoding="utf8"] - The encoding to use when appending the data.
|
|
669
|
+
* If not specified, it will append the data as a Buffer.
|
|
670
|
+
* @returns {Promise<FileAsync | null>} The FileAsync object if the data was appended successfully,
|
|
671
|
+
* or null if appending failed.
|
|
672
|
+
*/
|
|
673
|
+
append(data: string | Buffer, encoding?: BufferEncoding): Promise<FileAsync | null>;
|
|
674
|
+
get sync(): FileSync;
|
|
675
|
+
}
|