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