1 | // Type definitions for fs-extra 11.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 | // BendingBender <https://github.com/BendingBender>
|
13 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
14 | // Minimum TypeScript Version: 4.5
|
15 |
|
16 | /// <reference types="node" />
|
17 |
|
18 | import * as fs from 'fs';
|
19 | import * as jsonfile from 'jsonfile';
|
20 | import { StringifyOptions } from 'jsonfile/utils';
|
21 |
|
22 | export * from 'fs';
|
23 |
|
24 | /**
|
25 | * Copy a file or directory. The directory can have contents.
|
26 | *
|
27 | * @param src Note that if `src` is a directory it will copy everything inside of this directory,
|
28 | * not the entire directory itself (see [issue #537](https://github.com/jprichardson/node-fs-extra/issues/537)).
|
29 | * @param dest Note that if `src` is a file, `dest` cannot be a directory
|
30 | * (see [issue #323](https://github.com/jprichardson/node-fs-extra/issues/323)).
|
31 | *
|
32 | * @example
|
33 | * import * as fs from 'fs-extra'
|
34 | *
|
35 | * // With a callback:
|
36 | * fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
|
37 | * if (err) return console.error(err)
|
38 | * console.log('success!')
|
39 | * }) // copies file
|
40 | *
|
41 | * fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
|
42 | * if (err) return console.error(err)
|
43 | * console.log('success!')
|
44 | * }) // copies directory, even if it has subdirectories or files
|
45 | *
|
46 | * // With Promises:
|
47 | * fs.copy('/tmp/myfile', '/tmp/mynewfile')
|
48 | * .then(() => {
|
49 | * console.log('success!')
|
50 | * })
|
51 | * .catch(err => {
|
52 | * console.error(err)
|
53 | * })
|
54 | *
|
55 | * // With async/await:
|
56 | * async function asyncAwait () {
|
57 | * try {
|
58 | * await fs.copy('/tmp/myfile', '/tmp/mynewfile')
|
59 | * console.log('success!')
|
60 | * } catch (err) {
|
61 | * console.error(err)
|
62 | * }
|
63 | * }
|
64 | *
|
65 | * asyncAwait()
|
66 | *
|
67 | * // Using filter function
|
68 | * fs.copy(
|
69 | * '/tmp/mydir',
|
70 | * '/tmp/mynewdir',
|
71 | * {
|
72 | * filter(src, dest) {
|
73 | * // your logic here
|
74 | * // it will be copied if return true
|
75 | * }
|
76 | * },
|
77 | * err => {
|
78 | * if (err) return console.error(err)
|
79 | * console.log('success!')
|
80 | * }
|
81 | * )
|
82 | */
|
83 | export function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
|
84 | export function copy(src: string, dest: string, callback: NoParamCallbackWithUndefined): void;
|
85 | export function copy(src: string, dest: string, options: CopyOptions, callback: NoParamCallbackWithUndefined): void;
|
86 | /**
|
87 | * Copy a file or directory. The directory can have contents.
|
88 | *
|
89 | * @param src Note that if `src` is a directory it will copy everything inside of this directory,
|
90 | * not the entire directory itself (see [issue #537](https://github.com/jprichardson/node-fs-extra/issues/537)).
|
91 | * @param dest Note that if `src` is a file, `dest` cannot be a directory
|
92 | * (see [issue #323](https://github.com/jprichardson/node-fs-extra/issues/323)).
|
93 | *
|
94 | * @example
|
95 | * import * as fs from 'fs-extra'
|
96 | *
|
97 | * // copy file
|
98 | * fs.copySync('/tmp/myfile', '/tmp/mynewfile')
|
99 | *
|
100 | * // copy directory, even if it has subdirectories or files
|
101 | * fs.copySync('/tmp/mydir', '/tmp/mynewdir')
|
102 | *
|
103 | * // Using filter function
|
104 | * fs.copySync('/tmp/mydir', '/tmp/mynewdir', {
|
105 | * filter(src, dest) {
|
106 | * // your logic here
|
107 | * // it will be copied if return true
|
108 | * }
|
109 | * })
|
110 | */
|
111 | export function copySync(src: string, dest: string, options?: CopyOptionsSync): void;
|
112 |
|
113 | /**
|
114 | * Moves a file or directory, even across devices.
|
115 | *
|
116 | * @param dest Note: When `src` is a file, `dest` must be a file and when `src` is a directory, `dest` must be a directory.
|
117 | *
|
118 | * @example
|
119 | * import * as fs from 'fs-extra'
|
120 | *
|
121 | * const src = '/tmp/file.txt'
|
122 | * const dest = '/tmp/this/path/does/not/exist/file.txt'
|
123 | *
|
124 | * // With a callback:
|
125 | * fs.move(src, dest, err => {
|
126 | * if (err) return console.error(err)
|
127 | * console.log('success!')
|
128 | * })
|
129 | *
|
130 | * // With Promises:
|
131 | * fs.move(src, dest)
|
132 | * .then(() => {
|
133 | * console.log('success!')
|
134 | * })
|
135 | * .catch(err => {
|
136 | * console.error(err)
|
137 | * })
|
138 | *
|
139 | * // With async/await:
|
140 | * async function asyncAwait () {
|
141 | * try {
|
142 | * await fs.move(src, dest)
|
143 | * console.log('success!')
|
144 | * } catch (err) {
|
145 | * console.error(err)
|
146 | * }
|
147 | * }
|
148 | *
|
149 | * asyncAwait()
|
150 | *
|
151 | * // Using `overwrite` option
|
152 | * fs.move('/tmp/somedir', '/tmp/may/already/exist/somedir', { overwrite: true }, err => {
|
153 | * if (err) return console.error(err)
|
154 | * console.log('success!')
|
155 | * })
|
156 | */
|
157 | export function move(src: string, dest: string, options?: MoveOptions): Promise<void>;
|
158 | export function move(src: string, dest: string, callback: NoParamCallbackWithUndefined): void;
|
159 | export function move(src: string, dest: string, options: MoveOptions, callback: NoParamCallbackWithUndefined): void;
|
160 | /**
|
161 | * Moves a file or directory, even across devices.
|
162 | *
|
163 | * @param dest Note: When `src` is a file, `dest` must be a file and when `src` is a directory, `dest` must be a directory.
|
164 | *
|
165 | * @example
|
166 | * import * as fs from 'fs-extra'
|
167 | *
|
168 | * fs.moveSync('/tmp/somefile', '/tmp/does/not/exist/yet/somefile')
|
169 | *
|
170 | * // Using `overwrite` option
|
171 | * fs.moveSync('/tmp/somedir', '/tmp/may/already/exist/somedir', { overwrite: true })
|
172 | */
|
173 | export function moveSync(src: string, dest: string, options?: MoveOptions): void;
|
174 |
|
175 | /**
|
176 | * Ensures that the file exists. If the file that is requested to be created is in
|
177 | * directories that do not exist, these directories are created. If the file already
|
178 | * exists, it is **NOT MODIFIED**.
|
179 | *
|
180 | * @example
|
181 | * import * as fs from 'fs-extra'
|
182 | *
|
183 | * const file = '/tmp/this/path/does/not/exist/file.txt'
|
184 | *
|
185 | * // With a callback:
|
186 | * fs.ensureFile(file, err => {
|
187 | * console.log(err) // => null
|
188 | * // file has now been created, including the directory it is to be placed in
|
189 | * })
|
190 | *
|
191 | * // With Promises:
|
192 | * fs.ensureFile(file)
|
193 | * .then(() => {
|
194 | * console.log('success!')
|
195 | * })
|
196 | * .catch(err => {
|
197 | * console.error(err)
|
198 | * })
|
199 | *
|
200 | * // With async/await:
|
201 | * async function asyncAwait () {
|
202 | * try {
|
203 | * await fs.ensureFile(file)
|
204 | * console.log('success!')
|
205 | * } catch (err) {
|
206 | * console.error(err)
|
207 | * }
|
208 | * }
|
209 | *
|
210 | * asyncAwait()
|
211 | */
|
212 | export function ensureFile(file: string): Promise<void>;
|
213 | export function ensureFile(file: string, callback: NoParamCallbackWithUndefined): void;
|
214 | /**
|
215 | * @see ensureFile
|
216 | */
|
217 | export const createFile: typeof ensureFile;
|
218 | /**
|
219 | * Ensures that the file exists. If the file that is requested to be created is in
|
220 | * directories that do not exist, these directories are created. If the file already
|
221 | * exists, it is **NOT MODIFIED**.
|
222 | *
|
223 | * @example
|
224 | * import * as fs from 'fs-extra'
|
225 | *
|
226 | * const file = '/tmp/this/path/does/not/exist/file.txt'
|
227 | * fs.ensureFileSync(file)
|
228 | * // file has now been created, including the directory it is to be placed in
|
229 | */
|
230 | export function ensureFileSync(file: string): void;
|
231 | /**
|
232 | * @see ensureFileSync
|
233 | */
|
234 | export const createFileSync: typeof ensureFileSync;
|
235 |
|
236 | /**
|
237 | * Ensures that the link exists. If the directory structure does not exist, it is created.
|
238 | *
|
239 | * @example
|
240 | * import * as fs from 'fs-extra'
|
241 | *
|
242 | * const srcPath = '/tmp/file.txt'
|
243 | * const destPath = '/tmp/this/path/does/not/exist/file.txt'
|
244 | *
|
245 | * // With a callback:
|
246 | * fs.ensureLink(srcPath, destPath, err => {
|
247 | * console.log(err) // => null
|
248 | * // link has now been created, including the directory it is to be placed in
|
249 | * })
|
250 | *
|
251 | * // With Promises:
|
252 | * fs.ensureLink(srcPath, destPath)
|
253 | * .then(() => {
|
254 | * console.log('success!')
|
255 | * })
|
256 | * .catch(err => {
|
257 | * console.error(err)
|
258 | * })
|
259 | *
|
260 | * // With async/await:
|
261 | * async function asyncAwait () {
|
262 | * try {
|
263 | * await fs.ensureLink(srcPath, destPath)
|
264 | * console.log('success!')
|
265 | * } catch (err) {
|
266 | * console.error(err)
|
267 | * }
|
268 | * }
|
269 | *
|
270 | * asyncAwait()
|
271 | */
|
272 | export function ensureLink(src: string, dest: string): Promise<void>;
|
273 | export function ensureLink(src: string, dest: string, callback: fs.NoParamCallback): void;
|
274 | /**
|
275 | * @see ensureLink
|
276 | */
|
277 | export const createLink: typeof ensureLink;
|
278 | /**
|
279 | * Ensures that the link exists. If the directory structure does not exist, it is created.
|
280 | *
|
281 | * @example
|
282 | * import * as fs from 'fs-extra'
|
283 | *
|
284 | * const srcPath = '/tmp/file.txt'
|
285 | * const destPath = '/tmp/this/path/does/not/exist/file.txt'
|
286 | * fs.ensureLinkSync(srcPath, destPath)
|
287 | * // link has now been created, including the directory it is to be placed in
|
288 | */
|
289 | export function ensureLinkSync(src: string, dest: string): void;
|
290 | /**
|
291 | * @see ensureLinkSync
|
292 | */
|
293 | export const createLinkSync: typeof ensureLinkSync;
|
294 |
|
295 | /**
|
296 | * Ensures that the symlink exists. If the directory structure does not exist, it is created.
|
297 | *
|
298 | * @param type It is only available on Windows and ignored on other platforms.
|
299 | *
|
300 | * @example
|
301 | * import * as fs from 'fs-extra'
|
302 | *
|
303 | * const srcPath = '/tmp/file.txt'
|
304 | * const destPath = '/tmp/this/path/does/not/exist/file.txt'
|
305 | *
|
306 | * // With a callback:
|
307 | * fs.ensureSymlink(srcPath, destPath, err => {
|
308 | * console.log(err) // => null
|
309 | * // symlink has now been created, including the directory it is to be placed in
|
310 | * })
|
311 | *
|
312 | * // With Promises:
|
313 | * fs.ensureSymlink(srcPath, destPath)
|
314 | * .then(() => {
|
315 | * console.log('success!')
|
316 | * })
|
317 | * .catch(err => {
|
318 | * console.error(err)
|
319 | * })
|
320 | *
|
321 | * // With async/await:
|
322 | * async function asyncAwait () {
|
323 | * try {
|
324 | * await fs.ensureSymlink(srcPath, destPath)
|
325 | * console.log('success!')
|
326 | * } catch (err) {
|
327 | * console.error(err)
|
328 | * }
|
329 | * }
|
330 | *
|
331 | * asyncAwait()
|
332 | */
|
333 | export function ensureSymlink(src: string, dest: string, type?: SymlinkType): Promise<void>;
|
334 | export function ensureSymlink(src: string, dest: string, callback: fs.NoParamCallback): void;
|
335 | export function ensureSymlink(src: string, dest: string, type: SymlinkType, callback: fs.NoParamCallback): void;
|
336 | /**
|
337 | * @see ensureSymlink
|
338 | */
|
339 | export const createSymlink: typeof ensureSymlink;
|
340 | /**
|
341 | * Ensures that the symlink exists. If the directory structure does not exist, it is created.
|
342 | *
|
343 | * @param type It is only available on Windows and ignored on other platforms.
|
344 | *
|
345 | * @example
|
346 | * import * as fs from 'fs-extra'
|
347 | *
|
348 | * const srcPath = '/tmp/file.txt'
|
349 | * const destPath = '/tmp/this/path/does/not/exist/file.txt'
|
350 | * fs.ensureSymlinkSync(srcPath, destPath)
|
351 | * // symlink has now been created, including the directory it is to be placed in
|
352 | */
|
353 | export function ensureSymlinkSync(src: string, dest: string, type?: SymlinkType): void;
|
354 | /**
|
355 | * @see ensureSymlinkSync
|
356 | */
|
357 | export const createSymlinkSync: typeof ensureSymlinkSync;
|
358 |
|
359 | /**
|
360 | * Ensures that the directory exists. If the directory structure does not exist, it is created.
|
361 | *
|
362 | * @example
|
363 | * import * as fs from 'fs-extra'
|
364 | *
|
365 | * const dir = '/tmp/this/path/does/not/exist'
|
366 | * const desiredMode = 0o2775
|
367 | * const options = {
|
368 | * mode: 0o2775
|
369 | * }
|
370 | *
|
371 | * // With a callback:
|
372 | * fs.ensureDir(dir, err => {
|
373 | * console.log(err) // => null
|
374 | * // dir has now been created, including the directory it is to be placed in
|
375 | * })
|
376 | *
|
377 | * // With a callback and a mode integer
|
378 | * fs.ensureDir(dir, desiredMode, err => {
|
379 | * console.log(err) // => null
|
380 | * // dir has now been created with mode 0o2775, including the directory it is to be placed in
|
381 | * })
|
382 | *
|
383 | * // With Promises:
|
384 | * fs.ensureDir(dir)
|
385 | * .then(() => {
|
386 | * console.log('success!')
|
387 | * })
|
388 | * .catch(err => {
|
389 | * console.error(err)
|
390 | * })
|
391 | *
|
392 | * // With Promises and a mode integer:
|
393 | * fs.ensureDir(dir, desiredMode)
|
394 | * .then(() => {
|
395 | * console.log('success!')
|
396 | * })
|
397 | * .catch(err => {
|
398 | * console.error(err)
|
399 | * })
|
400 | *
|
401 | * // With async/await:
|
402 | * async function asyncAwait () {
|
403 | * try {
|
404 | * await fs.ensureDir(dir)
|
405 | * console.log('success!')
|
406 | * } catch (err) {
|
407 | * console.error(err)
|
408 | * }
|
409 | * }
|
410 | * asyncAwait()
|
411 | *
|
412 | * // With async/await and an options object, containing mode:
|
413 | * async function asyncAwaitMode () {
|
414 | * try {
|
415 | * await fs.ensureDir(dir, options)
|
416 | * console.log('success!')
|
417 | * } catch (err) {
|
418 | * console.error(err)
|
419 | * }
|
420 | * }
|
421 | * asyncAwaitMode()
|
422 | */
|
423 | export function ensureDir(path: string, options?: EnsureDirOptions | number): Promise<void>;
|
424 | export function ensureDir(path: string, callback: fs.NoParamCallback): void;
|
425 | export function ensureDir(path: string, options: EnsureDirOptions | number, callback: fs.NoParamCallback): void;
|
426 | /**
|
427 | * Ensures that the directory exists. If the directory structure does not exist, it is created.
|
428 | * If provided, options may specify the desired mode for the directory.
|
429 | *
|
430 | * @example
|
431 | * import * as fs from 'fs-extra'
|
432 | *
|
433 | * const dir = '/tmp/this/path/does/not/exist'
|
434 | *
|
435 | * const desiredMode = 0o2775
|
436 | * const options = {
|
437 | * mode: 0o2775
|
438 | * }
|
439 | *
|
440 | * fs.ensureDirSync(dir)
|
441 | * // dir has now been created, including the directory it is to be placed in
|
442 | *
|
443 | * fs.ensureDirSync(dir, desiredMode)
|
444 | * // dir has now been created, including the directory it is to be placed in with permission 0o2775
|
445 | *
|
446 | * fs.ensureDirSync(dir, options)
|
447 | * // dir has now been created, including the directory it is to be placed in with permission 0o2775
|
448 | */
|
449 | export function ensureDirSync(path: string, options?: EnsureDirOptions | number): void;
|
450 |
|
451 | /**
|
452 | * @see ensureDir
|
453 | */
|
454 | export const mkdirs: typeof ensureDir;
|
455 | /**
|
456 | * @see ensureDirSync
|
457 | */
|
458 | export const mkdirsSync: typeof ensureDirSync;
|
459 |
|
460 | /**
|
461 | * @see ensureDir
|
462 | */
|
463 | export const mkdirp: typeof ensureDir;
|
464 | /**
|
465 | * @see ensureDirSync
|
466 | */
|
467 | export const mkdirpSync: typeof ensureDirSync;
|
468 |
|
469 | /**
|
470 | * Almost the same as `writeFile` (i.e. it overwrites), except that if the parent directory
|
471 | * does not exist, it's created.
|
472 | *
|
473 | * @example
|
474 | * import * as fs from 'fs-extra'
|
475 | *
|
476 | * const file = '/tmp/this/path/does/not/exist/file.txt'
|
477 | *
|
478 | * // With a callback:
|
479 | * fs.outputFile(file, 'hello!', err => {
|
480 | * console.log(err) // => null
|
481 | *
|
482 | * fs.readFile(file, 'utf8', (err, data) => {
|
483 | * if (err) return console.error(err)
|
484 | * console.log(data) // => hello!
|
485 | * })
|
486 | * })
|
487 | *
|
488 | * // With Promises:
|
489 | * fs.outputFile(file, 'hello!')
|
490 | * .then(() => fs.readFile(file, 'utf8'))
|
491 | * .then(data => {
|
492 | * console.log(data) // => hello!
|
493 | * })
|
494 | * .catch(err => {
|
495 | * console.error(err)
|
496 | * })
|
497 | *
|
498 | * // With async/await:
|
499 | * async function asyncAwait () {
|
500 | * try {
|
501 | * await fs.outputFile(file, 'hello!')
|
502 | *
|
503 | * const data = await fs.readFile(file, 'utf8')
|
504 | *
|
505 | * console.log(data) // => hello!
|
506 | * } catch (err) {
|
507 | * console.error(err)
|
508 | * }
|
509 | * }
|
510 | *
|
511 | * asyncAwait()
|
512 | */
|
513 | export function outputFile(
|
514 | file: string,
|
515 | data: string | NodeJS.ArrayBufferView,
|
516 | options?: fs.WriteFileOptions,
|
517 | ): Promise<void>;
|
518 | export function outputFile(file: string, data: string | NodeJS.ArrayBufferView, callback: fs.NoParamCallback): void;
|
519 | export function outputFile(
|
520 | file: string,
|
521 | data: string | NodeJS.ArrayBufferView,
|
522 | options: fs.WriteFileOptions,
|
523 | callback: fs.NoParamCallback,
|
524 | ): void;
|
525 | /**
|
526 | * Almost the same as `writeFileSync` (i.e. it overwrites), except that if the parent directory
|
527 | * does not exist, it's created.
|
528 | *
|
529 | * @example
|
530 | * import * as fs from 'fs-extra'
|
531 | *
|
532 | * const file = '/tmp/this/path/does/not/exist/file.txt'
|
533 | * fs.outputFileSync(file, 'hello!')
|
534 | *
|
535 | * const data = fs.readFileSync(file, 'utf8')
|
536 | * console.log(data) // => hello!
|
537 | */
|
538 | export function outputFileSync(
|
539 | file: string,
|
540 | data: string | NodeJS.ArrayBufferView,
|
541 | options?: fs.WriteFileOptions,
|
542 | ): void;
|
543 |
|
544 | /**
|
545 | * Reads a JSON file and then parses it into an object.
|
546 | *
|
547 | * @example
|
548 | * import * as fs from 'fs-extra'
|
549 | *
|
550 | * // With a callback:
|
551 | * fs.readJson('./package.json', (err, packageObj) => {
|
552 | * if (err) console.error(err)
|
553 | * console.log(packageObj.version) // => 0.1.3
|
554 | * })
|
555 | *
|
556 | * // With Promises:
|
557 | * fs.readJson('./package.json')
|
558 | * .then(packageObj => {
|
559 | * console.log(packageObj.version) // => 0.1.3
|
560 | * })
|
561 | * .catch(err => {
|
562 | * console.error(err)
|
563 | * })
|
564 | *
|
565 | * // With async/await:
|
566 | * async function asyncAwait () {
|
567 | * try {
|
568 | * const packageObj = await fs.readJson('./package.json')
|
569 | * console.log(packageObj.version) // => 0.1.3
|
570 | * } catch (err) {
|
571 | * console.error(err)
|
572 | * }
|
573 | * }
|
574 | *
|
575 | * asyncAwait()
|
576 | *
|
577 | * // `readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
|
578 | * const file = '/tmp/some-invalid.json'
|
579 | * const data = '{not valid JSON'
|
580 | * fs.writeFileSync(file, data)
|
581 | *
|
582 | * // With a callback:
|
583 | * fs.readJson(file, { throws: false }, (err, obj) => {
|
584 | * if (err) console.error(err)
|
585 | * console.log(obj) // => null
|
586 | * })
|
587 | *
|
588 | * // With Promises:
|
589 | * fs.readJson(file, { throws: false })
|
590 | * .then(obj => {
|
591 | * console.log(obj) // => null
|
592 | * })
|
593 | * .catch(err => {
|
594 | * console.error(err) // Not called
|
595 | * })
|
596 | *
|
597 | * // With async/await:
|
598 | * async function asyncAwaitThrows () {
|
599 | * const obj = await fs.readJson(file, { throws: false })
|
600 | * console.log(obj) // => null
|
601 | * }
|
602 | *
|
603 | * asyncAwaitThrows()
|
604 | */
|
605 | export const readJson: typeof jsonfile.readFile;
|
606 | /**
|
607 | * @see readJson
|
608 | */
|
609 | export const readJSON: typeof jsonfile.readFile;
|
610 | /**
|
611 | * Reads a JSON file and then parses it into an object.
|
612 | *
|
613 | * @example
|
614 | * import * as fs from 'fs-extra'
|
615 | *
|
616 | * const packageObj = fs.readJsonSync('./package.json')
|
617 | * console.log(packageObj.version) // => 2.0.0
|
618 | *
|
619 | * // `readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
|
620 | * const file = '/tmp/some-invalid.json'
|
621 | * const data = '{not valid JSON'
|
622 | * fs.writeFileSync(file, data)
|
623 | *
|
624 | * const obj = fs.readJsonSync(file, { throws: false })
|
625 | * console.log(obj) // => null
|
626 | */
|
627 | export const readJsonSync: typeof jsonfile.readFileSync;
|
628 | /**
|
629 | * @see readJsonSync
|
630 | */
|
631 | export const readJSONSync: typeof jsonfile.readFileSync;
|
632 |
|
633 | /**
|
634 | * Writes an object to a JSON file.
|
635 | *
|
636 | * @example
|
637 | * import * as fs from 'fs-extra'
|
638 | *
|
639 | * // With a callback:
|
640 | * fs.writeJson('./package.json', {name: 'fs-extra'}, err => {
|
641 | * if (err) return console.error(err)
|
642 | * console.log('success!')
|
643 | * })
|
644 | *
|
645 | * // With Promises:
|
646 | * fs.writeJson('./package.json', {name: 'fs-extra'})
|
647 | * .then(() => {
|
648 | * console.log('success!')
|
649 | * })
|
650 | * .catch(err => {
|
651 | * console.error(err)
|
652 | * })
|
653 | *
|
654 | * // With async/await:
|
655 | * async function asyncAwait () {
|
656 | * try {
|
657 | * await fs.writeJson('./package.json', {name: 'fs-extra'})
|
658 | * console.log('success!')
|
659 | * } catch (err) {
|
660 | * console.error(err)
|
661 | * }
|
662 | * }
|
663 | *
|
664 | * asyncAwait()
|
665 | */
|
666 | export const writeJson: typeof jsonfile.writeFile;
|
667 | /**
|
668 | * @see writeJson
|
669 | */
|
670 | export const writeJSON: typeof jsonfile.writeFile;
|
671 | /**
|
672 | * Writes an object to a JSON file.
|
673 | *
|
674 | * @example
|
675 | * import * as fs from 'fs-extra'
|
676 | *
|
677 | * fs.writeJsonSync('./package.json', {name: 'fs-extra'})
|
678 | */
|
679 | export const writeJsonSync: typeof jsonfile.writeFileSync;
|
680 | /**
|
681 | * @see writeJsonSync
|
682 | */
|
683 | export const writeJSONSync: typeof jsonfile.writeFileSync;
|
684 |
|
685 | /**
|
686 | * Almost the same as `writeJson`, except that if the directory does not exist, it's created.
|
687 | *
|
688 | * @example
|
689 | * import * as fs from 'fs-extra'
|
690 | *
|
691 | * const file = '/tmp/this/path/does/not/exist/file.json'
|
692 | *
|
693 | * // With a callback:
|
694 | * fs.outputJson(file, {name: 'JP'}, err => {
|
695 | * console.log(err) // => null
|
696 | *
|
697 | * fs.readJson(file, (err, data) => {
|
698 | * if (err) return console.error(err)
|
699 | * console.log(data.name) // => JP
|
700 | * })
|
701 | * })
|
702 | *
|
703 | * // With Promises:
|
704 | * fs.outputJson(file, {name: 'JP'})
|
705 | * .then(() => fs.readJson(file))
|
706 | * .then(data => {
|
707 | * console.log(data.name) // => JP
|
708 | * })
|
709 | * .catch(err => {
|
710 | * console.error(err)
|
711 | * })
|
712 | *
|
713 | * // With async/await:
|
714 | * async function asyncAwait () {
|
715 | * try {
|
716 | * await fs.outputJson(file, {name: 'JP'})
|
717 | *
|
718 | * const data = await fs.readJson(file)
|
719 | *
|
720 | * console.log(data.name) // => JP
|
721 | * } catch (err) {
|
722 | * console.error(err)
|
723 | * }
|
724 | * }
|
725 | *
|
726 | * asyncAwait()
|
727 | */
|
728 | export function outputJson(file: string, data: any, options?: JsonOutputOptions): Promise<void>;
|
729 | export function outputJson(file: string, data: any, options: JsonOutputOptions, callback: fs.NoParamCallback): void;
|
730 | export function outputJson(file: string, data: any, callback: fs.NoParamCallback): void;
|
731 | /**
|
732 | * @see outputJson
|
733 | */
|
734 | export const outputJSON: typeof outputJson;
|
735 | /**
|
736 | * Almost the same as `writeJsonSync`, except that if the directory does not exist, it's created.
|
737 | *
|
738 | * @example
|
739 | * import * as fs from 'fs-extra'
|
740 | *
|
741 | * const file = '/tmp/this/path/does/not/exist/file.json'
|
742 | * fs.outputJsonSync(file, {name: 'JP'})
|
743 | *
|
744 | * const data = fs.readJsonSync(file)
|
745 | * console.log(data.name) // => JP
|
746 | */
|
747 | export function outputJsonSync(file: string, data: any, options?: JsonOutputOptions): void;
|
748 | /**
|
749 | * @see outputJsonSync
|
750 | */
|
751 | export const outputJSONSync: typeof outputJsonSync;
|
752 |
|
753 | /**
|
754 | * Removes a file or directory. The directory can have contents. If the path does not exist, silently does nothing.
|
755 | *
|
756 | * @example
|
757 | * import * as fs from 'fs-extra'
|
758 | *
|
759 | * // remove file
|
760 | * // With a callback:
|
761 | * fs.remove('/tmp/myfile', err => {
|
762 | * if (err) return console.error(err)
|
763 | * console.log('success!')
|
764 | * })
|
765 | *
|
766 | * fs.remove('/home/jprichardson', err => {
|
767 | * if (err) return console.error(err)
|
768 | * console.log('success!') // I just deleted my entire HOME directory.
|
769 | * })
|
770 | *
|
771 | * // With Promises:
|
772 | * fs.remove('/tmp/myfile')
|
773 | * .then(() => {
|
774 | * console.log('success!')
|
775 | * })
|
776 | * .catch(err => {
|
777 | * console.error(err)
|
778 | * })
|
779 | *
|
780 | * // With async/await:
|
781 | * async function asyncAwait () {
|
782 | * try {
|
783 | * await fs.remove('/tmp/myfile')
|
784 | * console.log('success!')
|
785 | * } catch (err) {
|
786 | * console.error(err)
|
787 | * }
|
788 | * }
|
789 | *
|
790 | * asyncAwait()
|
791 | */
|
792 | export function remove(dir: string): Promise<void>;
|
793 | export function remove(dir: string, callback: fs.NoParamCallback): void;
|
794 | /**
|
795 | * Removes a file or directory. The directory can have contents. If the path does not exist, silently does nothing.
|
796 | *
|
797 | * @example
|
798 | * import * as fs from 'fs-extra'
|
799 | *
|
800 | * // remove file
|
801 | * fs.removeSync('/tmp/myfile')
|
802 | *
|
803 | * fs.removeSync('/home/jprichardson') // I just deleted my entire HOME directory.
|
804 | */
|
805 | export function removeSync(dir: string): void;
|
806 |
|
807 | /**
|
808 | * Ensures that a directory is empty. Deletes directory contents if the directory is not empty.
|
809 | * If the directory does not exist, it is created. The directory itself is not deleted.
|
810 | *
|
811 | * @example
|
812 | * import * as fs from 'fs-extra'
|
813 | *
|
814 | * // assume this directory has a lot of files and folders
|
815 | * // With a callback:
|
816 | * fs.emptyDir('/tmp/some/dir', err => {
|
817 | * if (err) return console.error(err)
|
818 | * console.log('success!')
|
819 | * })
|
820 | *
|
821 | * // With Promises:
|
822 | * fs.emptyDir('/tmp/some/dir')
|
823 | * .then(() => {
|
824 | * console.log('success!')
|
825 | * })
|
826 | * .catch(err => {
|
827 | * console.error(err)
|
828 | * })
|
829 | *
|
830 | * // With async/await:
|
831 | * async function asyncAwait () {
|
832 | * try {
|
833 | * await fs.emptyDir('/tmp/some/dir')
|
834 | * console.log('success!')
|
835 | * } catch (err) {
|
836 | * console.error(err)
|
837 | * }
|
838 | * }
|
839 | *
|
840 | * asyncAwait()
|
841 | */
|
842 | export function emptyDir(path: string): Promise<void>;
|
843 | export function emptyDir(path: string, callback: fs.NoParamCallback): void;
|
844 | /**
|
845 | * @see emptyDir
|
846 | */
|
847 | export const emptydir: typeof emptyDir;
|
848 | /**
|
849 | * Ensures that a directory is empty. Deletes directory contents if the directory is not empty.
|
850 | * If the directory does not exist, it is created. The directory itself is not deleted.
|
851 | *
|
852 | * @example
|
853 | * import * as fs from 'fs-extra'
|
854 | *
|
855 | * // assume this directory has a lot of files and folders
|
856 | * fs.emptyDirSync('/tmp/some/dir')
|
857 | */
|
858 | export function emptyDirSync(path: string): void;
|
859 | /**
|
860 | * @see emptyDirSync
|
861 | */
|
862 | export const emptydirSync: typeof emptyDirSync;
|
863 |
|
864 | /**
|
865 | * Test whether or not the given path exists by checking with the file system. Like
|
866 | * [`fs.exists`](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback), but with a normal
|
867 | * callback signature (err, exists). Uses `fs.access` under the hood.
|
868 | *
|
869 | * @example
|
870 | * import * as fs from 'fs-extra'
|
871 | *
|
872 | * const file = '/tmp/this/path/does/not/exist/file.txt'
|
873 | *
|
874 | * // With a callback:
|
875 | * fs.pathExists(file, (err, exists) => {
|
876 | * console.log(err) // => null
|
877 | * console.log(exists) // => false
|
878 | * })
|
879 | *
|
880 | * // Promise usage:
|
881 | * fs.pathExists(file)
|
882 | * .then(exists => console.log(exists)) // => false
|
883 | *
|
884 | * // With async/await:
|
885 | * async function asyncAwait () {
|
886 | * const exists = await fs.pathExists(file)
|
887 | *
|
888 | * console.log(exists) // => false
|
889 | * }
|
890 | *
|
891 | * asyncAwait()
|
892 | */
|
893 | export function pathExists(path: string): Promise<boolean>;
|
894 | export function pathExists(path: string, callback: (err: NodeJS.ErrnoException | null, exists: boolean) => void): void;
|
895 | /**
|
896 | * An alias for [`fs.existsSync`](https://nodejs.org/api/fs.html#fs_fs_existssync_path), created for
|
897 | * consistency with `pathExists`.
|
898 | */
|
899 | export function pathExistsSync(path: string): boolean;
|
900 |
|
901 | export const access: typeof fs.access.__promisify__ & typeof fs.access;
|
902 | export const appendFile: typeof fs.appendFile.__promisify__ & typeof fs.appendFile;
|
903 | export const chmod: typeof fs.chmod.__promisify__ & typeof fs.chmod;
|
904 | export const chown: typeof fs.chown.__promisify__ & typeof fs.chown;
|
905 | export const close: typeof fs.close.__promisify__ & typeof fs.close;
|
906 | export const copyFile: typeof fs.copyFile.__promisify__ & typeof fs.copyFile;
|
907 | export const exists: typeof fs.exists.__promisify__ & typeof fs.exists;
|
908 | export const fchmod: typeof fs.fchmod.__promisify__ & typeof fs.fchmod;
|
909 | export const fchown: typeof fs.fchown.__promisify__ & typeof fs.fchown;
|
910 | export const fdatasync: typeof fs.fdatasync.__promisify__ & typeof fs.fdatasync;
|
911 | export const fstat: typeof fs.fstat.__promisify__ & typeof fs.fstat;
|
912 | export const fsync: typeof fs.fsync.__promisify__ & typeof fs.fsync;
|
913 | export const ftruncate: typeof fs.ftruncate.__promisify__ & typeof fs.ftruncate;
|
914 | export const futimes: typeof fs.futimes.__promisify__ & typeof fs.futimes;
|
915 | export const lchmod: typeof fs.lchmod.__promisify__ & typeof fs.lchmod;
|
916 | export const lchown: typeof fs.lchown.__promisify__ & typeof fs.lchown;
|
917 | export const link: typeof fs.link.__promisify__ & typeof fs.link;
|
918 | export const lstat: typeof fs.lstat.__promisify__ & typeof fs.lstat;
|
919 | export const mkdir: typeof fs.mkdir.__promisify__ & typeof fs.mkdir;
|
920 | export const mkdtemp: typeof fs.mkdtemp.__promisify__ & typeof fs.mkdtemp;
|
921 | export const open: typeof fs.open.__promisify__ & typeof fs.open;
|
922 | export const opendir: typeof fs.opendir.__promisify__ & typeof fs.opendir;
|
923 | export const read: typeof fs.read.__promisify__ & typeof fs.read;
|
924 | export const readv: typeof fs.readv.__promisify__ & typeof fs.readv;
|
925 | export const readdir: typeof fs.readdir.__promisify__ & typeof fs.readdir;
|
926 | export const readFile: typeof fs.readFile.__promisify__ & typeof fs.readFile;
|
927 | export const readlink: typeof fs.readlink.__promisify__ & typeof fs.readlink;
|
928 | export const realpath: typeof fs.realpath.__promisify__ &
|
929 | typeof fs.realpath & {
|
930 | native(path: fs.PathLike, options?: fs.EncodingOption): Promise<string>;
|
931 | native(path: fs.PathLike, options: fs.BufferEncodingOption): Promise<Buffer>;
|
932 | };
|
933 | export const rename: typeof fs.rename.__promisify__ & typeof fs.rename;
|
934 | export const rm: typeof fs.rm.__promisify__ & typeof fs.rm;
|
935 | export const rmdir: typeof fs.rmdir.__promisify__ & typeof fs.rmdir;
|
936 | export const stat: typeof fs.stat.__promisify__ & typeof fs.stat;
|
937 | export const symlink: typeof fs.symlink.__promisify__ & typeof fs.symlink;
|
938 | export const truncate: typeof fs.truncate.__promisify__ & typeof fs.truncate;
|
939 | export const unlink: typeof fs.unlink.__promisify__ & typeof fs.unlink;
|
940 | export const utimes: typeof fs.utimes.__promisify__ & typeof fs.utimes;
|
941 | export const write: typeof fs.write.__promisify__ & typeof fs.write;
|
942 | export const writev: typeof fs.writev.__promisify__ & typeof fs.writev;
|
943 | export const writeFile: typeof fs.writeFile.__promisify__ & typeof fs.writeFile;
|
944 |
|
945 | export type NoParamCallbackWithUndefined = (err: NodeJS.ErrnoException | null | undefined) => void;
|
946 |
|
947 | export type SymlinkType = fs.symlink.Type;
|
948 |
|
949 | export type CopyFilterSync = (src: string, dest: string) => boolean;
|
950 | export type CopyFilterAsync = (src: string, dest: string) => Promise<boolean>;
|
951 |
|
952 | export interface CopyOptions {
|
953 | /**
|
954 | * Dereference symlinks.
|
955 | * @default false
|
956 | */
|
957 | dereference?: boolean | undefined;
|
958 | /**
|
959 | * Overwrite existing file or directory.
|
960 | * _Note that the copy operation will silently fail if you set this to `false` and the destination exists._
|
961 | * Use the `errorOnExist` option to change this behavior.
|
962 | * @default true
|
963 | */
|
964 | overwrite?: boolean | undefined;
|
965 | /**
|
966 | * When `true`, will set last modification and access times to the ones of the original source files.
|
967 | * When `false`, timestamp behavior is OS-dependent.
|
968 | * @default false
|
969 | */
|
970 | preserveTimestamps?: boolean | undefined;
|
971 | /**
|
972 | * When `overwrite` is `false` and the destination exists, throw an error.
|
973 | * @default false
|
974 | */
|
975 | errorOnExist?: boolean | undefined;
|
976 | /**
|
977 | * Function to filter copied files/directories. Return `true` to copy the item, `false` to ignore it.
|
978 | * Can also return a `Promise` that resolves to `true` or `false` (or pass in an `async` function).
|
979 | */
|
980 | filter?: CopyFilterSync | CopyFilterAsync | undefined;
|
981 | }
|
982 |
|
983 | export interface CopyOptionsSync extends CopyOptions {
|
984 | /**
|
985 | * Function to filter copied files/directories. Return `true` to copy the item, `false` to ignore it.
|
986 | */
|
987 | filter?: CopyFilterSync | undefined;
|
988 | }
|
989 |
|
990 | export interface EnsureDirOptions {
|
991 | mode?: number | undefined;
|
992 | }
|
993 |
|
994 | export interface MoveOptions {
|
995 | /**
|
996 | * Overwrite existing file or directory.
|
997 | * @default false
|
998 | */
|
999 | overwrite?: boolean | undefined;
|
1000 | /**
|
1001 | * Dereference symlinks.
|
1002 | * @default false
|
1003 | */
|
1004 | dereference?: boolean | undefined;
|
1005 | }
|
1006 |
|
1007 | export { JFReadOptions as JsonReadOptions, JFWriteOptions as JsonWriteOptions } from 'jsonfile';
|
1008 |
|
1009 | export type JsonOutputOptions = fs.WriteFileOptions & StringifyOptions;
|