Comparing version 3.1.0 to 3.2.0
72
index.js
'use strict'; | ||
var EventEmitter = require('events').EventEmitter; | ||
var path = require('path'); | ||
var util = require('util'); | ||
var stream = require('readable-stream'); | ||
var Promise = require('pinkie-promise'); | ||
var pify = require('pify'); | ||
var path = require('path'); | ||
var fs = require('graceful-fs'); | ||
var fsP = pify(fs, Promise); | ||
var mkdirp = require('mkdirp'); | ||
var mkdirpP = pify(mkdirp, Promise); | ||
var objectAssign = require('object-assign'); | ||
var NestedError = require('nested-error-stacks'); | ||
var util = require('util'); | ||
var mkdirpP = pify(mkdirp, Promise); | ||
var fsP = pify(fs, Promise); | ||
function CpFileError(message, nested) { | ||
@@ -29,15 +32,26 @@ NestedError.call(this, message, nested); | ||
return new Promise(function startRead(resolve, reject) { | ||
var read = fs.createReadStream(src); | ||
var size = 0; | ||
var progressEmitter = new EventEmitter(); | ||
read.on('error', function (err) { | ||
reject(new CpFileError('cannot read from `' + src + '`: ' + err.message, err)); | ||
}); | ||
var absoluteSrc = path.resolve(src); | ||
var absoluteDest = path.resolve(dest); | ||
read.on('readable', function () { | ||
resolve(read); | ||
}); | ||
var promise = fsP.stat(src).then(function (stat) { | ||
size = stat.size; | ||
}).catch(function (err) { | ||
throw new CpFileError('cannot stat path `' + src + '`: ' + err.message, err); | ||
}).then(function () { | ||
var read = new stream.Readable().wrap(fs.createReadStream(src)); | ||
return new Promise(function (resolve, reject) { | ||
read.on('error', function (err) { | ||
reject(new CpFileError('cannot read from `' + src + '`: ' + err.message, err)); | ||
}); | ||
read.on('end', function () { | ||
resolve(read); | ||
read.on('readable', function () { | ||
resolve(read); | ||
}); | ||
read.on('end', function () { | ||
resolve(read); | ||
}); | ||
}); | ||
@@ -58,2 +72,17 @@ }).then(function mkdirpDestDirectory(read) { | ||
read.on('data', function () { | ||
if (size === 0) { | ||
return; | ||
} | ||
var written = write.bytesWritten; | ||
var percent = written / size; | ||
progressEmitter.emit('progress', { | ||
src: absoluteSrc, | ||
dest: absoluteDest, | ||
size: size, | ||
written: written, | ||
percent: percent | ||
}); | ||
}); | ||
write.on('error', function (err) { | ||
@@ -69,2 +98,10 @@ if (!opts.overwrite && err.code === 'EEXIST') { | ||
write.on('close', function () { | ||
progressEmitter.emit('progress', { | ||
src: absoluteSrc, | ||
dest: absoluteDest, | ||
size: size, | ||
written: size, | ||
percent: 1 | ||
}); | ||
resolve(true); | ||
@@ -86,2 +123,9 @@ }); | ||
}); | ||
promise.on = function () { | ||
progressEmitter.on.apply(progressEmitter, arguments); | ||
return promise; | ||
}; | ||
return promise; | ||
}; | ||
@@ -88,0 +132,0 @@ |
{ | ||
"name": "cp-file", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Copy a file", | ||
@@ -50,3 +50,4 @@ "license": "MIT", | ||
"pify": "^2.3.0", | ||
"pinkie-promise": "^2.0.0" | ||
"pinkie-promise": "^2.0.0", | ||
"readable-stream": "^2.1.4" | ||
}, | ||
@@ -53,0 +54,0 @@ "devDependencies": { |
@@ -5,6 +5,6 @@ # cp-file [![Build Status](https://travis-ci.org/sindresorhus/cp-file.svg?branch=master)](https://travis-ci.org/sindresorhus/cp-file) | ||
- Fast by using streams in the async 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). | ||
- Fast by using streams in the async 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. | ||
@@ -26,3 +26,3 @@ | ||
cpFile('src/unicorn.png', 'dist/unicorn.png').then(() => { | ||
console.log('file copied'); | ||
console.log('File copied'); | ||
}); | ||
@@ -36,3 +36,3 @@ ``` | ||
Returns a promise. | ||
Returns a `Promise`. | ||
@@ -55,7 +55,7 @@ ### cpFile.sync(source, destination, [options]) | ||
Type: `object` | ||
Type: `Object` | ||
##### options.overwrite | ||
##### overwrite | ||
Type: `boolean` | ||
Type: `boolean`<br> | ||
Default: `true` | ||
@@ -65,3 +65,41 @@ | ||
### 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 | ||
cpFile(src, dest).on('progress', data => { | ||
// ... | ||
}).then(() => { | ||
// ... | ||
}) | ||
``` | ||
## Related | ||
@@ -74,2 +112,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9198
173
108
7
+ Addedreadable-stream@^2.1.4
+ Addedcore-util-is@1.0.3(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)