UNPKG

23.8 kBTypeScriptView Raw
1// Type definitions for fs-extra 9.0
2// Project: https://github.com/jprichardson/node-fs-extra
3// Definitions by: Alan Agius <https://github.com/alan-agius4>,
4// midknight41 <https://github.com/midknight41>,
5// Brendan Forster <https://github.com/shiftkey>,
6// Mees van Dijk <https://github.com/mees->,
7// Justin Rockwood <https://github.com/jrockwood>,
8// Sang Dang <https://github.com/sangdth>,
9// Florian Keller <https://github.com/ffflorian>
10// Piotr Błażejewicz <https://github.com/peterblazejewicz>
11// Tiger Oakes <https://github.com/NotWoods>
12// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
13// Minimum TypeScript Version: 3.9
14
15/// <reference types="node" />
16
17import * as fs from 'fs';
18import Stats = fs.Stats;
19import PathLike = fs.PathLike;
20
21export * from 'fs';
22
23export function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
24export function copy(src: string, dest: string, callback: (err: Error) => void): void;
25export function copy(src: string, dest: string, options: CopyOptions, callback: (err: Error) => void): void;
26export function copySync(src: string, dest: string, options?: CopyOptionsSync): void;
27
28export function copyFile(src: string, dest: string, flags?: number): Promise<void>;
29export function copyFile(src: string, dest: string, callback: (err: Error) => void): void;
30export function copyFile(src: string, dest: string, flags: number, callback: (err: Error) => void): void;
31
32export function move(src: string, dest: string, options?: MoveOptions): Promise<void>;
33export function move(src: string, dest: string, callback: (err: Error) => void): void;
34export function move(src: string, dest: string, options: MoveOptions, callback: (err: Error) => void): void;
35export function moveSync(src: string, dest: string, options?: MoveOptions): void;
36
37export function createFile(file: string): Promise<void>;
38export function createFile(file: string, callback: (err: Error) => void): void;
39export function createFileSync(file: string): void;
40
41export function createSymlink(src: string, dest: string, type: SymlinkType): Promise<void>;
42export function createSymlink(src: string, dest: string, type: SymlinkType, callback?: (err: Error) => void): void;
43export function createSymlinkSync(src: string, dest: string, type: SymlinkType): void;
44
45export function ensureDir(path: string, options?: EnsureOptions | number): Promise<void>;
46export function ensureDir(path: string, callback?: (err: Error) => void): void;
47export function ensureDir(path: string, options?: EnsureOptions | number, callback?: (err: Error) => void): void;
48export function ensureDirSync(path: string, options?: EnsureOptions | number): void;
49
50export function mkdirs(dir: string): Promise<void>;
51export function mkdirs(dir: string, callback: (err: Error) => void): void;
52export function mkdirp(dir: string): Promise<void>;
53export function mkdirp(dir: string, callback: (err: Error) => void): void;
54export function mkdirsSync(dir: string): void;
55export function mkdirpSync(dir: string): void;
56
57export function outputFile(
58 file: string,
59 data: any,
60 options?: WriteFileOptions | BufferEncoding | string,
61): Promise<void>;
62export function outputFile(file: string, data: any, callback: (err: Error) => void): void;
63export function outputFile(
64 file: string,
65 data: any,
66 options: WriteFileOptions | string,
67 callback: (err: Error) => void,
68): void;
69export function outputFileSync(file: string, data: any, options?: WriteFileOptions | BufferEncoding | string): void;
70
71export function readJson(file: string, options?: ReadOptions | BufferEncoding | string): Promise<any>;
72export function readJson(file: string, callback: (err: Error, jsonObject: any) => void): void;
73export function readJson(
74 file: string,
75 options: ReadOptions | BufferEncoding | string,
76 callback: (err: Error, jsonObject: any) => void,
77): void;
78export function readJSON(file: string, options?: ReadOptions | BufferEncoding | string): Promise<any>;
79export function readJSON(file: string, callback: (err: Error, jsonObject: any) => void): void;
80export function readJSON(
81 file: string,
82 options: ReadOptions | BufferEncoding | string,
83 callback: (err: Error, jsonObject: any) => void,
84): void;
85
86export function readJsonSync(file: string, options?: ReadOptions | BufferEncoding | string): any;
87export function readJSONSync(file: string, options?: ReadOptions | BufferEncoding | string): any;
88
89export function remove(dir: string, callback: (err: Error) => void): void;
90export function remove(dir: string, callback?: (err: Error) => void): Promise<void>;
91export function removeSync(dir: string): void;
92
93export function outputJSON(file: string, data: any, options?: WriteOptions | BufferEncoding | string): Promise<void>;
94export function outputJSON(
95 file: string,
96 data: any,
97 options: WriteOptions | BufferEncoding | string,
98 callback: (err: Error) => void,
99): void;
100export function outputJSON(file: string, data: any, callback: (err: Error) => void): void;
101export function outputJson(file: string, data: any, options?: WriteOptions | BufferEncoding | string): Promise<void>;
102export function outputJson(
103 file: string,
104 data: any,
105 options: WriteOptions | BufferEncoding | string,
106 callback: (err: Error) => void,
107): void;
108export function outputJson(file: string, data: any, callback: (err: Error) => void): void;
109export function outputJsonSync(file: string, data: any, options?: WriteOptions | BufferEncoding | string): void;
110export function outputJSONSync(file: string, data: any, options?: WriteOptions | BufferEncoding | string): void;
111
112export function writeJSON(file: string, object: any, options?: WriteOptions | BufferEncoding | string): Promise<void>;
113export function writeJSON(file: string, object: any, callback: (err: Error) => void): void;
114export function writeJSON(
115 file: string,
116 object: any,
117 options: WriteOptions | BufferEncoding | string,
118 callback: (err: Error) => void,
119): void;
120export function writeJson(file: string, object: any, options?: WriteOptions | BufferEncoding | string): Promise<void>;
121export function writeJson(file: string, object: any, callback: (err: Error) => void): void;
122export function writeJson(
123 file: string,
124 object: any,
125 options: WriteOptions | BufferEncoding | string,
126 callback: (err: Error) => void,
127): void;
128
129export function writeJsonSync(file: string, object: any, options?: WriteOptions | BufferEncoding | string): void;
130export function writeJSONSync(file: string, object: any, options?: WriteOptions | BufferEncoding | string): void;
131
132export function ensureFile(path: string): Promise<void>;
133export function ensureFile(path: string, callback: (err: Error) => void): void;
134export function ensureFileSync(path: string): void;
135
136export function ensureLink(src: string, dest: string): Promise<void>;
137export function ensureLink(src: string, dest: string, callback: (err: Error) => void): void;
138// alias for ensureLink
139export const createLink: typeof ensureLink;
140export function ensureLinkSync(src: string, dest: string): void;
141// aliased as
142export const createLinkSync: typeof ensureLinkSync;
143
144export function ensureSymlink(src: string, dest: string, type?: SymlinkType): Promise<void>;
145export function ensureSymlink(src: string, dest: string, type: SymlinkType, callback: (err: Error) => void): void;
146export function ensureSymlink(src: string, dest: string, callback: (err: Error) => void): void;
147export function ensureSymlinkSync(src: string, dest: string, type?: SymlinkType): void;
148
149export function emptyDir(path: string): Promise<void>;
150export function emptyDir(path: string, callback: (err: Error) => void): void;
151export const emptydir: typeof emptyDir;
152
153export function emptyDirSync(path: string): void;
154export const emptydirSync: typeof emptyDirSync;
155
156export function pathExists(path: string): Promise<boolean>;
157export function pathExists(path: string, callback: (err: Error, exists: boolean) => void): void;
158export function pathExistsSync(path: string): boolean;
159
160// fs async methods
161// copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v6/index.d.ts
162
163export function access(path: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
164export function access(path: PathLike, mode: number, callback: (err: NodeJS.ErrnoException) => void): void;
165export function access(path: PathLike, mode?: number): Promise<void>;
166
167export function appendFile(
168 file: PathLike | number,
169 data: any,
170 options: {
171 encoding?: BufferEncoding | string | undefined;
172 mode?: number | string | undefined;
173 flag?: string | undefined;
174 },
175 callback: (err: NodeJS.ErrnoException) => void,
176): void;
177export function appendFile(file: PathLike | number, data: any, callback: (err: NodeJS.ErrnoException) => void): void;
178export function appendFile(
179 file: PathLike | number,
180 data: any,
181 options?:
182 | {
183 encoding?: BufferEncoding | string | undefined;
184 mode?: number | string | undefined;
185 flag?: string | undefined;
186 }
187 | BufferEncoding
188 | string,
189): Promise<void>;
190
191export function chmod(path: PathLike, mode: Mode, callback: (err: NodeJS.ErrnoException) => void): void;
192export function chmod(path: PathLike, mode: Mode): Promise<void>;
193
194export function chown(path: PathLike, uid: number, gid: number): Promise<void>;
195export function chown(path: PathLike, uid: number, gid: number, callback: (err: NodeJS.ErrnoException) => void): void;
196
197export function close(fd: number, callback: (err: NodeJS.ErrnoException) => void): void;
198export function close(fd: number): Promise<void>;
199
200export function fchmod(fd: number, mode: Mode, callback: (err: NodeJS.ErrnoException) => void): void;
201export function fchmod(fd: number, mode: Mode): Promise<void>;
202
203export function fchown(fd: number, uid: number, gid: number, callback: (err: NodeJS.ErrnoException) => void): void;
204export function fchown(fd: number, uid: number, gid: number): Promise<void>;
205
206export function fdatasync(fd: number, callback: () => void): void;
207export function fdatasync(fd: number): Promise<void>;
208
209export function fstat(fd: number, callback: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
210export function fstat(fd: number): Promise<Stats>;
211
212export function fsync(fd: number, callback: (err: NodeJS.ErrnoException) => void): void;
213export function fsync(fd: number): Promise<void>;
214
215export function ftruncate(fd: number, callback: (err: NodeJS.ErrnoException) => void): void;
216export function ftruncate(fd: number, len: number, callback: (err: NodeJS.ErrnoException) => void): void;
217export function ftruncate(fd: number, len?: number): Promise<void>;
218
219export function futimes(fd: number, atime: number, mtime: number, callback: (err: NodeJS.ErrnoException) => void): void;
220export function futimes(fd: number, atime: Date, mtime: Date, callback: (err: NodeJS.ErrnoException) => void): void;
221export function futimes(fd: number, atime: number, mtime: number): Promise<void>;
222export function futimes(fd: number, atime: Date, mtime: Date): Promise<void>;
223
224export function lchown(path: PathLike, uid: number, gid: number, callback: (err: NodeJS.ErrnoException) => void): void;
225export function lchown(path: PathLike, uid: number, gid: number): Promise<void>;
226
227export function link(existingPath: PathLike, newPath: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
228export function link(existingPath: PathLike, newPath: PathLike): Promise<void>;
229
230export function lstat(path: PathLike, callback: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
231export function lstat(path: PathLike): Promise<Stats>;
232
233/**
234 * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
235 *
236 * @param callback No arguments other than a possible exception are given to the completion callback.
237 */
238export function mkdir(path: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
239/**
240 * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
241 *
242 * @param callback No arguments other than a possible exception are given to the completion callback.
243 */
244export function mkdir(
245 path: PathLike,
246 options: Mode | fs.MakeDirectoryOptions | null,
247 callback: (err: NodeJS.ErrnoException) => void,
248): void;
249export function mkdir(path: PathLike, options?: Mode | fs.MakeDirectoryOptions | null): Promise<void>;
250export function mkdirSync(path: PathLike, options?: Mode | fs.MakeDirectoryOptions | null): void;
251
252export function open(
253 path: PathLike,
254 flags: string | number,
255 callback: (err: NodeJS.ErrnoException, fd: number) => void,
256): void;
257export function open(
258 path: PathLike,
259 flags: string | number,
260 mode: Mode,
261 callback: (err: NodeJS.ErrnoException, fd: number) => void,
262): void;
263export function open(path: PathLike, flags: string | number, mode?: Mode | null): Promise<number>;
264
265export function opendir(path: string, cb: (err: NodeJS.ErrnoException | null, dir: fs.Dir) => void): void;
266export function opendir(
267 path: string,
268 options: fs.OpenDirOptions,
269 cb: (err: NodeJS.ErrnoException | null, dir: fs.Dir) => void,
270): void;
271export function opendir(path: string, options?: fs.OpenDirOptions): Promise<fs.Dir>;
272
273export function read<TBuffer extends ArrayBufferView>(
274 fd: number,
275 buffer: TBuffer,
276 offset: number,
277 length: number,
278 position: number | null,
279 callback: (err: NodeJS.ErrnoException, bytesRead: number, buffer: TBuffer) => void,
280): void;
281export function read<TBuffer extends ArrayBufferView>(
282 fd: number,
283 buffer: TBuffer,
284 offset: number,
285 length: number,
286 position: number | null,
287): Promise<{ bytesRead: number; buffer: TBuffer }>;
288
289export function readFile(file: PathLike | number, callback: (err: NodeJS.ErrnoException, data: Buffer) => void): void;
290export function readFile(
291 file: PathLike | number,
292 encoding: BufferEncoding | string,
293 callback: (err: NodeJS.ErrnoException, data: string) => void,
294): void;
295export function readFile(
296 file: PathLike | number,
297 options: { flag?: string | undefined } | { encoding: BufferEncoding | string; flag?: string | undefined },
298 callback: (err: NodeJS.ErrnoException, data: Buffer) => void,
299): void;
300export function readFile(
301 file: PathLike | number,
302 options: { flag?: string | undefined } | { encoding: BufferEncoding | string; flag?: string | undefined },
303): Promise<string>;
304// tslint:disable-next-line:unified-signatures
305export function readFile(file: PathLike | number, encoding: BufferEncoding | string): Promise<string>;
306export function readFile(file: PathLike | number): Promise<Buffer>;
307
308export function readdir(path: PathLike, callback: (err: NodeJS.ErrnoException, files: string[]) => void): void;
309export function readdir(
310 path: PathLike,
311 options: 'buffer' | { encoding: 'buffer'; withFileTypes?: false | undefined },
312): Promise<Buffer[]>;
313export function readdir(
314 path: PathLike,
315 options?:
316 | { encoding: BufferEncoding | string | null; withFileTypes?: false | undefined }
317 | BufferEncoding
318 | string
319 | null,
320): Promise<string[]>;
321export function readdir(
322 path: PathLike,
323 options?: { encoding?: BufferEncoding | string | null | undefined; withFileTypes?: false | undefined },
324): Promise<string[] | Buffer[]>;
325export function readdir(
326 path: PathLike,
327 options: { encoding?: BufferEncoding | string | null | undefined; withFileTypes: true },
328): Promise<fs.Dirent[]>;
329
330export function readlink(path: PathLike, callback: (err: NodeJS.ErrnoException, linkString: string) => any): void;
331export function readlink(path: PathLike): Promise<string>;
332
333export function realpath(path: PathLike, callback: (err: NodeJS.ErrnoException, resolvedPath: string) => any): void;
334export function realpath(
335 path: PathLike,
336 cache: { [path: string]: string },
337 callback: (err: NodeJS.ErrnoException, resolvedPath: string) => any,
338): void;
339export function realpath(path: PathLike, cache?: { [path: string]: string }): Promise<string>;
340
341/* tslint:disable:unified-signatures */
342export namespace realpath {
343 const native: {
344 (path: PathLike, options: { encoding: 'buffer' } | 'buffer'): Promise<Buffer>;
345 (
346 path: PathLike,
347 options: { encoding: BufferEncoding | string | null } | BufferEncoding | string | undefined | null,
348 ): Promise<string>;
349 (path: PathLike, options: { encoding: BufferEncoding | string | null } | string | undefined | null): Promise<
350 string | Buffer
351 >;
352 (path: PathLike): Promise<string>;
353 } & typeof fs.realpath.native;
354}
355/* tslint:enable:unified-signatures */
356
357export function rename(oldPath: PathLike, newPath: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
358export function rename(oldPath: PathLike, newPath: PathLike): Promise<void>;
359
360/**
361 * Asynchronously removes files and directories (modeled on the standard POSIX
362 * `rm` utility).
363 *
364 * Only available in node >= v14.14.0
365 */
366export function rm(
367 path: PathLike,
368 options?: {
369 force?: boolean | undefined;
370 maxRetries?: number | undefined;
371 recursive?: boolean | undefined;
372 retryDelay?: number | undefined;
373 },
374): Promise<void>;
375
376/**
377 * Asynchronous rmdir - removes the directory specified in {path}
378 *
379 * @param callback No arguments other than a possible exception are given to the completion callback.
380 */
381export function rmdir(path: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
382export function rmdir(path: PathLike, options?: fs.RmDirOptions): Promise<void>;
383
384export function stat(path: PathLike, callback: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
385export function stat(path: PathLike): Promise<Stats>;
386
387export function symlink(
388 target: PathLike,
389 path: PathLike,
390 type: SymlinkType | undefined,
391 callback: (err: NodeJS.ErrnoException) => void,
392): void;
393export function symlink(target: PathLike, path: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
394export function symlink(target: PathLike, path: PathLike, type?: SymlinkType): Promise<void>;
395
396export function truncate(path: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
397export function truncate(path: PathLike, len: number, callback: (err: NodeJS.ErrnoException) => void): void;
398export function truncate(path: PathLike, len?: number): Promise<void>;
399
400/**
401 * Asynchronous unlink - deletes the file specified in {path}
402 *
403 * @param callback No arguments other than a possible exception are given to the completion callback.
404 */
405export function unlink(path: PathLike, callback: (err: NodeJS.ErrnoException) => void): void;
406export function unlink(path: PathLike): Promise<void>;
407
408export function utimes(
409 path: PathLike,
410 atime: number,
411 mtime: number,
412 callback: (err: NodeJS.ErrnoException) => void,
413): void;
414export function utimes(path: PathLike, atime: Date, mtime: Date, callback: (err: NodeJS.ErrnoException) => void): void;
415export function utimes(path: PathLike, atime: number, mtime: number): Promise<void>;
416export function utimes(path: PathLike, atime: Date, mtime: Date): Promise<void>;
417
418export function write<TBuffer extends ArrayBufferView>(
419 fd: number,
420 buffer: TBuffer,
421 offset: number,
422 length: number,
423 position: number | null,
424 callback: (err: NodeJS.ErrnoException, written: number, buffer: TBuffer) => void,
425): void;
426export function write<TBuffer extends ArrayBufferView>(
427 fd: number,
428 buffer: TBuffer,
429 offset: number,
430 length: number,
431 callback: (err: NodeJS.ErrnoException, written: number, buffer: TBuffer) => void,
432): void;
433export function write(
434 fd: number,
435 data: any,
436 callback: (err: NodeJS.ErrnoException, written: number, str: string) => void,
437): void;
438export function write(
439 fd: number,
440 data: any,
441 offset: number,
442 callback: (err: NodeJS.ErrnoException, written: number, str: string) => void,
443): void;
444export function write(
445 fd: number,
446 data: any,
447 offset: number,
448 encoding: BufferEncoding | string,
449 callback: (err: NodeJS.ErrnoException, written: number, str: string) => void,
450): void;
451export function write<TBuffer extends ArrayBufferView>(
452 fd: number,
453 buffer: TBuffer,
454 offset?: number,
455 length?: number,
456 position?: number | null,
457): Promise<{ bytesWritten: number; buffer: TBuffer }>;
458export function write(
459 fd: number,
460 data: any,
461 offset?: number,
462 encoding?: BufferEncoding | string,
463): Promise<{ bytesWritten: number; buffer: string }>;
464
465export function writeFile(file: PathLike | number, data: any, callback: (err: NodeJS.ErrnoException) => void): void;
466export function writeFile(
467 file: PathLike | number,
468 data: any,
469 options?: WriteFileOptions | BufferEncoding | string,
470): Promise<void>;
471export function writeFile(
472 file: PathLike | number,
473 data: any,
474 options: WriteFileOptions | BufferEncoding | string,
475 callback: (err: NodeJS.ErrnoException) => void,
476): void;
477
478export function writev(
479 fd: number,
480 buffers: NodeJS.ArrayBufferView[],
481 position: number,
482 cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void,
483): void;
484export function writev(
485 fd: number,
486 buffers: NodeJS.ArrayBufferView[],
487 cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void,
488): void;
489export function writev(fd: number, buffers: NodeJS.ArrayBufferView[], position?: number): Promise<WritevResult>;
490
491/**
492 * Asynchronous mkdtemp - Creates a unique temporary directory. Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
493 *
494 * @param callback The created folder path is passed as a string to the callback's second parameter.
495 */
496export function mkdtemp(prefix: string): Promise<string>;
497export function mkdtemp(prefix: string, callback: (err: NodeJS.ErrnoException, folder: string) => void): void;
498
499export interface PathEntry {
500 path: string;
501 stats: Stats;
502}
503
504export interface PathEntryStream {
505 read(): PathEntry | null;
506}
507
508export type CopyFilterSync = (src: string, dest: string) => boolean;
509export type CopyFilterAsync = (src: string, dest: string) => Promise<boolean>;
510
511export type SymlinkType = 'dir' | 'file' | 'junction';
512
513export type Mode = string | number;
514
515export type ArrayBufferView = NodeJS.TypedArray | DataView;
516
517export interface CopyOptions {
518 dereference?: boolean | undefined;
519 overwrite?: boolean | undefined;
520 preserveTimestamps?: boolean | undefined;
521 errorOnExist?: boolean | undefined;
522 filter?: CopyFilterSync | CopyFilterAsync | undefined;
523 recursive?: boolean | undefined;
524}
525
526export interface CopyOptionsSync extends CopyOptions {
527 filter?: CopyFilterSync | undefined;
528}
529
530export interface EnsureOptions {
531 mode?: number | undefined;
532}
533
534export interface MoveOptions {
535 overwrite?: boolean | undefined;
536 limit?: number | undefined;
537}
538
539export interface ReadOptions {
540 throws?: boolean | undefined;
541 fs?: object | undefined;
542 reviver?: any;
543 encoding?: BufferEncoding | string | undefined;
544 flag?: string | undefined;
545}
546
547export interface WriteFileOptions {
548 encoding?: BufferEncoding | string | null | undefined;
549 flag?: string | undefined;
550 mode?: number | undefined;
551}
552
553export interface WriteOptions extends WriteFileOptions {
554 fs?: object | undefined;
555 replacer?: any;
556 spaces?: number | string | undefined;
557 EOL?: string | undefined;
558}
559
560export interface WritevResult {
561 bytesWritten: number;
562 buffers: ArrayBufferView[];
563}