UNPKG

18.7 kBTypeScriptView Raw
1// Type definitions for fs-extra 8.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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11// TypeScript Version: 2.2
12
13/// <reference types="node" />
14
15import * as fs from "fs";
16import Stats = fs.Stats;
17
18export * from "fs";
19
20export function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
21export function copy(src: string, dest: string, callback: (err: Error) => void): void;
22export function copy(src: string, dest: string, options: CopyOptions, callback: (err: Error) => void): void;
23export function copySync(src: string, dest: string, options?: CopyOptionsSync): void;
24
25export function copyFile(src: string, dest: string, flags?: number): Promise<void>;
26export function copyFile(src: string, dest: string, callback: (err: Error) => void): void;
27export function copyFile(src: string, dest: string, flags: number, callback: (err: Error) => void): void;
28
29export function move(src: string, dest: string, options?: MoveOptions): Promise<void>;
30export function move(src: string, dest: string, callback: (err: Error) => void): void;
31export function move(src: string, dest: string, options: MoveOptions, callback: (err: Error) => void): void;
32export function moveSync(src: string, dest: string, options?: MoveOptions): void;
33
34export function createFile(file: string): Promise<void>;
35export function createFile(file: string, callback: (err: Error) => void): void;
36export function createFileSync(file: string): void;
37
38export function ensureDir(path: string, options?: EnsureOptions | number): Promise<void>;
39export function ensureDir(path: string, options?: EnsureOptions | number, callback?: (err: Error) => void): void;
40export function ensureDirSync(path: string, options?: EnsureOptions | number): void;
41
42export function mkdirs(dir: string): Promise<void>;
43export function mkdirs(dir: string, callback: (err: Error) => void): void;
44export function mkdirp(dir: string): Promise<void>;
45export function mkdirp(dir: string, callback: (err: Error) => void): void;
46export function mkdirsSync(dir: string): void;
47export function mkdirpSync(dir: string): void;
48
49export function outputFile(file: string, data: any, options?: WriteFileOptions | string): Promise<void>;
50export function outputFile(file: string, data: any, callback: (err: Error) => void): void;
51export function outputFile(file: string, data: any, options: WriteFileOptions | string, callback: (err: Error) => void): void;
52export function outputFileSync(file: string, data: any, options?: WriteFileOptions | string): void;
53
54export function readJson(file: string, options?: ReadOptions): Promise<any>;
55export function readJson(file: string, callback: (err: Error, jsonObject: any) => void): void;
56export function readJson(file: string, options: ReadOptions, callback: (err: Error, jsonObject: any) => void): void;
57export function readJSON(file: string, options?: ReadOptions): Promise<any>;
58export function readJSON(file: string, callback: (err: Error, jsonObject: any) => void): void;
59export function readJSON(file: string, options: ReadOptions, callback: (err: Error, jsonObject: any) => void): void;
60
61export function readJsonSync(file: string, options?: ReadOptions): any;
62export function readJSONSync(file: string, options?: ReadOptions): any;
63
64export function remove(dir: string): Promise<void>;
65export function remove(dir: string, callback: (err: Error) => void): void;
66export function removeSync(dir: string): void;
67
68export function outputJSON(file: string, data: any, options?: WriteOptions): Promise<void>;
69export function outputJSON(file: string, data: any, options: WriteOptions, callback: (err: Error) => void): void;
70export function outputJSON(file: string, data: any, callback: (err: Error) => void): void;
71export function outputJson(file: string, data: any, options?: WriteOptions): Promise<void>;
72export function outputJson(file: string, data: any, options: WriteOptions, callback: (err: Error) => void): void;
73export function outputJson(file: string, data: any, callback: (err: Error) => void): void;
74export function outputJsonSync(file: string, data: any, options?: WriteOptions): void;
75export function outputJSONSync(file: string, data: any, options?: WriteOptions): void;
76
77export function writeJSON(file: string, object: any, options?: WriteOptions): Promise<void>;
78export function writeJSON(file: string, object: any, callback: (err: Error) => void): void;
79export function writeJSON(file: string, object: any, options: WriteOptions, callback: (err: Error) => void): void;
80export function writeJson(file: string, object: any, options?: WriteOptions): Promise<void>;
81export function writeJson(file: string, object: any, callback: (err: Error) => void): void;
82export function writeJson(file: string, object: any, options: WriteOptions, callback: (err: Error) => void): void;
83
84export function writeJsonSync(file: string, object: any, options?: WriteOptions): void;
85export function writeJSONSync(file: string, object: any, options?: WriteOptions): void;
86
87export function ensureFile(path: string): Promise<void>;
88export function ensureFile(path: string, callback: (err: Error) => void): void;
89export function ensureFileSync(path: string): void;
90
91export function ensureLink(src: string, dest: string): Promise<void>;
92export function ensureLink(src: string, dest: string, callback: (err: Error) => void): void;
93export function ensureLinkSync(src: string, dest: string): void;
94
95export function ensureSymlink(src: string, dest: string, type?: SymlinkType): Promise<void>;
96export function ensureSymlink(src: string, dest: string, type: SymlinkType, callback: (err: Error) => void): void;
97export function ensureSymlink(src: string, dest: string, callback: (err: Error) => void): void;
98export function ensureSymlinkSync(src: string, dest: string, type?: SymlinkType): void;
99
100export function emptyDir(path: string): Promise<void>;
101export function emptyDir(path: string, callback: (err: Error) => void): void;
102export function emptyDirSync(path: string): void;
103
104export function pathExists(path: string): Promise<boolean>;
105export function pathExists(path: string, callback: (err: Error, exists: boolean) => void): void;
106export function pathExistsSync(path: string): boolean;
107
108// fs async methods
109// copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v6/index.d.ts
110
111export function access(path: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
112export function access(path: string | Buffer, mode: number, callback: (err: NodeJS.ErrnoException) => void): void;
113export function access(path: string | Buffer, mode?: number): Promise<void>;
114
115export function appendFile(file: string | Buffer | number, data: any, options: { encoding?: string; mode?: number | string; flag?: string; },
116 callback: (err: NodeJS.ErrnoException) => void): void;
117export function appendFile(file: string | Buffer | number, data: any, callback: (err: NodeJS.ErrnoException) => void): void;
118export function appendFile(file: string | Buffer | number, data: any, options?: { encoding?: string; mode?: number | string; flag?: string; }): Promise<void>;
119
120export function chmod(path: string | Buffer, mode: string | number, callback: (err: NodeJS.ErrnoException) => void): void;
121export function chmod(path: string | Buffer, mode: string | number): Promise<void>;
122
123export function chown(path: string | Buffer, uid: number, gid: number): Promise<void>;
124export function chown(path: string | Buffer, uid: number, gid: number, callback: (err: NodeJS.ErrnoException) => void): void;
125
126export function close(fd: number, callback: (err: NodeJS.ErrnoException) => void): void;
127export function close(fd: number): Promise<void>;
128
129export function fchmod(fd: number, mode: string | number, callback: (err: NodeJS.ErrnoException) => void): void;
130export function fchmod(fd: number, mode: string | number): Promise<void>;
131
132export function fchown(fd: number, uid: number, gid: number, callback: (err: NodeJS.ErrnoException) => void): void;
133export function fchown(fd: number, uid: number, gid: number): Promise<void>;
134
135export function fdatasync(fd: number, callback: () => void): void;
136export function fdatasync(fd: number): Promise<void>;
137
138export function fstat(fd: number, callback: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
139export function fstat(fd: number): Promise<Stats>;
140
141export function fsync(fd: number, callback: (err: NodeJS.ErrnoException) => void): void;
142export function fsync(fd: number): Promise<void>;
143
144export function ftruncate(fd: number, callback: (err: NodeJS.ErrnoException) => void): void;
145export function ftruncate(fd: number, len: number, callback: (err: NodeJS.ErrnoException) => void): void;
146export function ftruncate(fd: number, len?: number): Promise<void>;
147
148export function futimes(fd: number, atime: number, mtime: number, callback: (err: NodeJS.ErrnoException) => void): void;
149export function futimes(fd: number, atime: Date, mtime: Date, callback: (err: NodeJS.ErrnoException) => void): void;
150export function futimes(fd: number, atime: number, mtime: number): Promise<void>;
151export function futimes(fd: number, atime: Date, mtime: Date): Promise<void>;
152
153export function lchown(path: string | Buffer, uid: number, gid: number, callback: (err: NodeJS.ErrnoException) => void): void;
154export function lchown(path: string | Buffer, uid: number, gid: number): Promise<void>;
155
156export function link(srcpath: string | Buffer, dstpath: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
157export function link(srcpath: string | Buffer, dstpath: string | Buffer): Promise<void>;
158
159export function lstat(path: string | Buffer, callback: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
160export function lstat(path: string | Buffer): Promise<Stats>;
161
162/**
163 * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
164 *
165 * @param callback No arguments other than a possible exception are given to the completion callback.
166 */
167export function mkdir(path: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
168/**
169 * Asynchronous mkdir - creates the directory specified in {path}. Parameter {mode} defaults to 0777.
170 *
171 * @param callback No arguments other than a possible exception are given to the completion callback.
172 */
173export function mkdir(path: string | Buffer, mode: number | string, callback: (err: NodeJS.ErrnoException) => void): void;
174export function mkdir(path: string | Buffer): Promise<void>;
175
176export function open(path: string | Buffer, flags: string | number, callback: (err: NodeJS.ErrnoException, fd: number) => void): void;
177export function open(path: string | Buffer, flags: string | number, mode: number, callback: (err: NodeJS.ErrnoException, fd: number) => void): void;
178export function open(path: string | Buffer, flags: string | number, mode?: number): Promise<number>;
179
180export function read(fd: number, buffer: Buffer, offset: number, length: number, position: number | null,
181 callback: (err: NodeJS.ErrnoException, bytesRead: number, buffer: Buffer) => void): void;
182export function read(fd: number, buffer: Buffer, offset: number, length: number, position: number | null): Promise<ReadResult>;
183
184export function readFile(file: string | Buffer | number, callback: (err: NodeJS.ErrnoException, data: Buffer) => void): void;
185export function readFile(file: string | Buffer | number, encoding: string, callback: (err: NodeJS.ErrnoException, data: string) => void): void;
186export function readFile(file: string | Buffer | number, options: { flag?: string; } | { encoding: string; flag?: string; }, callback: (err: NodeJS.ErrnoException, data: Buffer) => void): void;
187export function readFile(file: string | Buffer | number, options: { flag?: string; } | { encoding: string; flag?: string; }): Promise<string>;
188// tslint:disable-next-line:unified-signatures
189export function readFile(file: string | Buffer | number, encoding: string): Promise<string>;
190export function readFile(file: string | Buffer | number): Promise<Buffer>;
191
192export function readdir(path: string | Buffer, callback: (err: NodeJS.ErrnoException, files: string[]) => void): void;
193export function readdir(path: string | Buffer): Promise<string[]>;
194
195export function readlink(path: string | Buffer, callback: (err: NodeJS.ErrnoException, linkString: string) => any): void;
196export function readlink(path: string | Buffer): Promise<string>;
197
198export function realpath(path: string | Buffer, callback: (err: NodeJS.ErrnoException, resolvedPath: string) => any): void;
199export function realpath(path: string | Buffer, cache: { [path: string]: string }, callback: (err: NodeJS.ErrnoException, resolvedPath: string) => any): void;
200export function realpath(path: string | Buffer, cache?: { [path: string]: string }): Promise<string>;
201
202export function rename(oldPath: string, newPath: string, callback: (err: NodeJS.ErrnoException) => void): void;
203export function rename(oldPath: string, newPath: string): Promise<void>;
204
205/**
206 * Asynchronous rmdir - removes the directory specified in {path}
207 *
208 * @param callback No arguments other than a possible exception are given to the completion callback.
209 */
210export function rmdir(path: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
211export function rmdir(path: string | Buffer): Promise<void>;
212
213export function stat(path: string | Buffer, callback: (err: NodeJS.ErrnoException, stats: Stats) => any): void;
214export function stat(path: string | Buffer): Promise<Stats>;
215
216export function symlink(srcpath: string | Buffer, dstpath: string | Buffer, type: FsSymlinkType | undefined, callback: (err: NodeJS.ErrnoException) => void): void;
217export function symlink(srcpath: string | Buffer, dstpath: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
218export function symlink(srcpath: string | Buffer, dstpath: string | Buffer, type?: FsSymlinkType): Promise<void>;
219
220export function truncate(path: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
221export function truncate(path: string | Buffer, len: number, callback: (err: NodeJS.ErrnoException) => void): void;
222export function truncate(path: string | Buffer, len?: number): Promise<void>;
223
224/**
225 * Asynchronous unlink - deletes the file specified in {path}
226 *
227 * @param callback No arguments other than a possible exception are given to the completion callback.
228 */
229export function unlink(path: string | Buffer, callback: (err: NodeJS.ErrnoException) => void): void;
230export function unlink(path: string | Buffer): Promise<void>;
231
232export function utimes(path: string | Buffer, atime: number, mtime: number, callback: (err: NodeJS.ErrnoException) => void): void;
233export function utimes(path: string | Buffer, atime: Date, mtime: Date, callback: (err: NodeJS.ErrnoException) => void): void;
234export function utimes(path: string | Buffer, atime: number, mtime: number): Promise<void>;
235export function utimes(path: string | Buffer, atime: Date, mtime: Date): Promise<void>;
236
237export function write(fd: number, buffer: Buffer, offset: number, length: number, position: number | null, callback: (err: NodeJS.ErrnoException, written: number, buffer: Buffer) => void): void;
238export function write(fd: number, buffer: Buffer, offset: number, length: number, callback: (err: NodeJS.ErrnoException, written: number, buffer: Buffer) => void): void;
239export function write(fd: number, data: any, callback: (err: NodeJS.ErrnoException, written: number, str: string) => void): void;
240export function write(fd: number, data: any, offset: number, callback: (err: NodeJS.ErrnoException, written: number, str: string) => void): void;
241export function write(fd: number, data: any, offset: number, encoding: string, callback: (err: NodeJS.ErrnoException, written: number, str: string) => void): void;
242export function write(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number | null): Promise<WriteResult>;
243export function write(fd: number, data: any, offset?: number, encoding?: string): Promise<WriteResult>;
244
245export function writeFile(file: string | Buffer | number, data: any, callback: (err: NodeJS.ErrnoException) => void): void;
246export function writeFile(file: string | Buffer | number, data: any, options?: WriteFileOptions | string): Promise<void>;
247export function writeFile(file: string | Buffer | number, data: any, options: WriteFileOptions | string, callback: (err: NodeJS.ErrnoException) => void): void;
248
249/**
250 * Asynchronous mkdtemp - Creates a unique temporary directory. Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
251 *
252 * @param callback The created folder path is passed as a string to the callback's second parameter.
253 */
254export function mkdtemp(prefix: string): Promise<string>;
255export function mkdtemp(prefix: string, callback: (err: NodeJS.ErrnoException, folder: string) => void): void;
256
257export interface PathEntry {
258 path: string;
259 stats: Stats;
260}
261
262export interface PathEntryStream {
263 read(): PathEntry | null;
264}
265
266export type CopyFilterSync = (src: string, dest: string) => boolean;
267export type CopyFilterAsync = (src: string, dest: string) => Promise<boolean>;
268
269export type SymlinkType = "dir" | "file";
270export type FsSymlinkType = "dir" | "file" | "junction";
271
272export interface CopyOptions {
273 dereference?: boolean;
274 overwrite?: boolean;
275 preserveTimestamps?: boolean;
276 errorOnExist?: boolean;
277 filter?: CopyFilterSync | CopyFilterAsync;
278 recursive?: boolean;
279}
280
281export interface CopyOptionsSync extends CopyOptions {
282 filter?: CopyFilterSync;
283}
284
285export interface EnsureOptions {
286 mode?: number;
287}
288
289export interface MoveOptions {
290 overwrite?: boolean;
291 limit?: number;
292}
293
294export interface ReadOptions {
295 throws?: boolean;
296 fs?: object;
297 reviver?: any;
298 encoding?: string;
299 flag?: string;
300}
301
302export interface WriteFileOptions {
303 encoding?: string;
304 flag?: string;
305 mode?: number;
306}
307
308export interface WriteOptions extends WriteFileOptions {
309 fs?: object;
310 replacer?: any;
311 spaces?: number | string;
312 EOL?: string;
313}
314
315export interface ReadResult {
316 bytesRead: number;
317 buffer: Buffer;
318}
319
320export interface WriteResult {
321 bytesWritten: number;
322 buffer: Buffer;
323}