6afda81f61efc16868da6115f402b4f5199450c6.svn-base
2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# cp-file [![Build Status](https://travis-ci.org/sindresorhus/cp-file.svg?branch=master)](https://travis-ci.org/sindresorhus/cp-file) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/cp-file/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/cp-file?branch=master)
> Copy a file
## Highlights
- Fast by using streams in the async version and [`fs.copyFileSync()`](https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_flags) (when available) in the synchronous version.
- Resilient by using [graceful-fs](https://github.com/isaacs/node-graceful-fs).
- User-friendly by creating non-existent destination directories for you.
- Can be safe by turning off [overwriting](#optionsoverwrite).
- User-friendly errors.
## Install
```
$ npm install cp-file
```
## Usage
```js
const cpFile = require('cp-file');
(async () => {
await cpFile('source/unicorn.png', 'destination/unicorn.png');
console.log('File copied');
})();
```
## API
### cpFile(source, destination, [options])
Returns a `Promise` that resolves when the file is copied.
### cpFile.sync(source, destination, [options])
#### source
Type: `string`
File you want to copy.
#### destination
Type: `string`
Where you want the file copied.
#### options
Type: `Object`
##### overwrite
Type: `boolean`<br>
Default: `true`
Overwrite existing file.
### cpFile.on('progress', handler)
Progress reporting. Only available when using the async method.
#### handler(data)
Type: `Function`
##### data
```js
{
src: string,
dest: string,
size: number,
written: number,
percent: number
}
```
- `src` and `dest` are absolute paths.
- `size` and `written` are in bytes.
- `percent` is a value between `0` and `1`.
###### Notes
- For empty files, the `progress` event is emitted only once.
- The `.on()` method is available only right after the initial `cpFile()` call. So make sure
you add a `handler` before `.then()`:
```js
(async () => {
await cpFile(source, destination).on('progress', data => {
// …
});
})();
```
## Related
- [cpy](https://github.com/sindresorhus/cpy) - Copy files
- [cpy-cli](https://github.com/sindresorhus/cpy-cli) - Copy files on the command-line
- [move-file](https://github.com/sindresorhus/move-file) - Move a file
- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)