Comparing version 2.3.0 to 3.0.0
89
index.js
'use strict'; | ||
var Promise = require('pinkie-promise'); | ||
var pify = require('pify'); | ||
var path = require('path'); | ||
var fs = require('graceful-fs'); | ||
var fsP = pify.all(fs, Promise); | ||
var mkdirp = require('mkdirp'); | ||
var mkdirpP = pify(mkdirp, Promise); | ||
var objectAssign = require('object-assign'); | ||
var onetime = require('onetime'); | ||
var NestedError = require('nested-error-stacks'); | ||
@@ -19,31 +22,33 @@ var util = require('util'); | ||
module.exports = function (src, dest, opts, cb) { | ||
module.exports = function (src, dest, opts) { | ||
if (!src || !dest) { | ||
throw new CpFileError('`src` and `dest` required'); | ||
return Promise.reject(new CpFileError('`src` and `dest` required')); | ||
} | ||
if (typeof opts !== 'object') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
cb = onetime(cb || function () {}); | ||
opts = objectAssign({overwrite: true}, opts); | ||
var read = fs.createReadStream(src); | ||
var readListener = onetime(startWrite); | ||
return new Promise(function startRead(resolve, reject) { | ||
var read = fs.createReadStream(src); | ||
read.on('error', function (err) { | ||
cb(new CpFileError('cannot read from `' + src + '`: ' + err.message, err)); | ||
}); | ||
read.on('readable', readListener); | ||
read.on('end', readListener); | ||
function startWrite() { | ||
mkdirp(path.dirname(dest), function (err) { | ||
if (err && err.code !== 'EEXIST') { | ||
cb(new CpFileError('cannot create directory `' + path.dirname(dest) + '`: ' + err.message, err)); | ||
return; | ||
read.on('error', function (err) { | ||
reject(new CpFileError('cannot read from `' + src + '`: ' + err.message, err)); | ||
}); | ||
read.on('readable', function () { | ||
resolve(read); | ||
}); | ||
read.on('end', function () { | ||
resolve(read); | ||
}); | ||
}).then(function mkdirpDestDirectory(read) { | ||
return mkdirpP(path.dirname(dest)).then(function () { | ||
return read; | ||
}).catch(function (err) { | ||
if (err.code !== 'EEXIST') { | ||
throw new CpFileError('cannot create directory `' + path.dirname(dest) + '`: ' + err.message, err); | ||
} | ||
return read; | ||
}); | ||
}).then(function (read) { | ||
return new Promise(function pipeToDest(resolve, reject) { | ||
var write = fs.createWriteStream(dest, {flags: opts.overwrite ? 'w' : 'wx'}); | ||
@@ -53,23 +58,11 @@ | ||
if (!opts.overwrite && err.code === 'EEXIST') { | ||
cb(); | ||
resolve(false); | ||
return; | ||
} | ||
cb(new CpFileError('cannot write to `' + dest + '`: ' + err.message, err)); | ||
reject(new CpFileError('cannot write to `' + dest + '`: ' + err.message, err)); | ||
}); | ||
write.on('close', function () { | ||
fs.lstat(src, function (err, stats) { | ||
if (err) { | ||
cb(new CpFileError('lstat `' + src + '` failed: ' + err.message, err)); | ||
return; | ||
} | ||
fs.utimes(dest, stats.atime, stats.mtime, function (err) { | ||
if (err) { | ||
cb(new CpFileError('utimes `' + dest + '` failed: ' + err.message, err)); | ||
return; | ||
} | ||
cb(); | ||
}); | ||
}); | ||
resolve(true); | ||
}); | ||
@@ -79,3 +72,13 @@ | ||
}); | ||
} | ||
}).then(function (updateTimes) { | ||
if (updateTimes) { | ||
return fsP.lstat(src).catch(function (err) { | ||
throw new CpFileError('lstat `' + src + '` failed: ' + err.message, err); | ||
}).then(function (stats) { | ||
return fsP.utimes(dest, stats.atime, stats.mtime).catch(function (err) { | ||
throw new CpFileError('utimes `' + dest + '` failed: ' + err.message, err); | ||
}); | ||
}); | ||
} | ||
}); | ||
}; | ||
@@ -90,5 +93,9 @@ | ||
var read; | ||
var bytesRead; | ||
var pos; | ||
var write; | ||
var stat; | ||
var BUF_LENGTH = 100 * 1024; | ||
var buf = new Buffer(BUF_LENGTH); | ||
var read, bytesRead, pos, write, stat; | ||
@@ -133,2 +140,3 @@ function readSync(pos) { | ||
} | ||
throw new CpFileError('cannot write to `' + dest + '`: ' + err.message, err); | ||
@@ -138,2 +146,3 @@ } | ||
writeSync(); | ||
while (bytesRead === BUF_LENGTH) { | ||
@@ -140,0 +149,0 @@ bytesRead = readSync(pos); |
{ | ||
"name": "cp-file", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"description": "Copy a file", | ||
@@ -23,3 +23,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha", | ||
"test": "xo && mocha", | ||
"cover": "istanbul cover -x test.js node_modules/mocha/bin/_mocha" | ||
@@ -50,3 +50,4 @@ }, | ||
"object-assign": "^3.0.0", | ||
"onetime": "^1.0.0" | ||
"pify": "^2.2.0", | ||
"pinkie-promise": "^1.0.0" | ||
}, | ||
@@ -58,4 +59,5 @@ "devDependencies": { | ||
"rewire": "^2.3.1", | ||
"rimraf": "^2.3.2" | ||
"rimraf": "^2.3.2", | ||
"xo": "*" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # cp-file [![Build Status](https://travis-ci.org/sindresorhus/cp-file.svg?branch=master)](https://travis-ci.org/sindresorhus/cp-file) | ||
- Resilient by using [graceful-fs](https://github.com/isaacs/node-graceful-fs). | ||
- User-friendly by creating non-existant destination directories for you. | ||
- User-friendly by creating non-existent destination directories for you. | ||
- Can be safe by turning off [overwriting](#optionsoverwrite). | ||
@@ -23,5 +23,5 @@ - User-friendly errors. | ||
```js | ||
var cpFile = require('cp-file'); | ||
const cpFile = require('cp-file'); | ||
cpFile('src/unicorn.png', 'dist/unicorn.png', function (err) { | ||
cpFile('src/unicorn.png', 'dist/unicorn.png').then(() => { | ||
console.log('file copied'); | ||
@@ -34,3 +34,6 @@ }); | ||
### cpFile(source, destination, [options], [callback]) | ||
### cpFile(source, destination, [options]) | ||
Returns a promise. | ||
### cpFile.sync(source, destination, [options]) | ||
@@ -40,3 +43,2 @@ | ||
*Required* | ||
Type: `string` | ||
@@ -48,3 +50,2 @@ | ||
*Required* | ||
Type: `string` | ||
@@ -65,7 +66,3 @@ | ||
#### callback(err) | ||
Type: `function` | ||
## Related | ||
@@ -72,0 +69,0 @@ |
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
7464
135
6
6
70
+ Addedpify@^2.2.0
+ Addedpinkie-promise@^1.0.0
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@1.0.0(transitive)
+ Addedpinkie-promise@1.0.0(transitive)
- Removedonetime@^1.0.0
- Removedonetime@1.1.0(transitive)