UNPKG

1.7 kBMarkdownView Raw
1# copySync(src, dest, [options])
2
3Copy a file or directory. The directory can have contents. Like `cp -r`.
4
5- `src` `<String>` Note that if `src` is a directory it will copy everything inside of this directory, not the entire directory itself (see [issue #537](https://github.com/jprichardson/node-fs-extra/issues/537)).
6- `dest` `<String>` Note that if `src` is a file, `dest` cannot be a directory (see [issue #323](https://github.com/jprichardson/node-fs-extra/issues/323)).
7- `options` `<Object>`
8 - `overwrite` `<boolean>`: overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
9 - `errorOnExist` `<boolean>`: when `overwrite` is `false` and the destination exists, throw an error. Default is `false`.
10 - `dereference` `<boolean>`: dereference symlinks, default is `false`.
11 - `preserveTimestamps` `<boolean>`: When true, will set last modification and access times to the ones of the original source files. When false, timestamp behavior is OS-dependent. Default is `false`.
12 - `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude.
13
14## Example:
15
16```js
17const fs = require('fs-extra')
18
19// copy file
20fs.copySync('/tmp/myfile', '/tmp/mynewfile')
21
22// copy directory, even if it has subdirectories or files
23fs.copySync('/tmp/mydir', '/tmp/mynewdir')
24```
25
26**Using filter function**
27
28```js
29const fs = require('fs-extra')
30
31const filterFunc = (src, dest) => {
32 // your logic here
33 // it will be copied if return true
34}
35
36fs.copySync('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc })
37```