Comparing version 2.0.2 to 2.1.0
76
index.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
var objectAssign = require('object-assign'); | ||
var onetime = require('onetime'); | ||
@@ -18,17 +19,13 @@ module.exports = function (src, dest, opts, cb) { | ||
cb = cb || function () {}; | ||
cb = onetime(cb || function () {}); | ||
opts = objectAssign({overwrite: true}, opts); | ||
var cbCalled = false; | ||
var read = fs.createReadStream(src); | ||
var readListener = onetime(startWrite); | ||
function done(err) { | ||
if (cbCalled) { | ||
return; | ||
} | ||
read.on('error', cb); | ||
read.on('readable', readListener); | ||
read.on('end', readListener); | ||
cbCalled = true; | ||
cb(err); | ||
} | ||
function copy() { | ||
function startWrite() { | ||
mkdirp(path.dirname(dest), function (err) { | ||
@@ -40,15 +37,21 @@ if (err && err.code !== 'EEXIST') { | ||
var read = fs.createReadStream(src); | ||
var write = fs.createWriteStream(dest); | ||
var write = fs.createWriteStream(dest, {flags: opts.overwrite ? 'w' : 'wx'}); | ||
read.on('error', done); | ||
write.on('error', done); | ||
write.on('error', function (err) { | ||
if (!opts.overwrite && err.code === 'EEXIST') { | ||
cb(); | ||
return; | ||
} | ||
cb(err); | ||
}); | ||
write.on('close', function () { | ||
fs.lstat(src, function (err, stats) { | ||
if (err) { | ||
done(err); | ||
cb(err); | ||
return; | ||
} | ||
fs.utimes(dest, stats.atime, stats.mtime, done); | ||
fs.utimes(dest, stats.atime, stats.mtime, cb); | ||
}); | ||
@@ -60,15 +63,2 @@ }); | ||
} | ||
if (opts.overwrite) { | ||
copy(); | ||
} else { | ||
fs.exists(dest, function (exists) { | ||
if (exists) { | ||
cb(); | ||
return; | ||
} | ||
copy(); | ||
}); | ||
} | ||
}; | ||
@@ -83,5 +73,8 @@ | ||
if (!opts.overwrite && fs.existsSync(dest)) { | ||
return; | ||
} | ||
var read = fs.openSync(src, 'r'); | ||
var BUF_LENGTH = 100 * 1024; | ||
var buf = new Buffer(BUF_LENGTH); | ||
var bytesRead = fs.readSync(read, buf, 0, BUF_LENGTH, 0); | ||
var pos = bytesRead; | ||
var write; | ||
@@ -96,10 +89,12 @@ try { | ||
var BUF_LENGTH = 100 * 1024; | ||
var buf = new Buffer(BUF_LENGTH); | ||
var read = fs.openSync(src, 'r'); | ||
var write = fs.openSync(dest, 'w'); | ||
var stat = fs.fstatSync(read); | ||
var bytesRead = BUF_LENGTH; | ||
var pos = 0; | ||
try { | ||
write = fs.openSync(dest, opts.overwrite ? 'w' : 'wx'); | ||
} catch (err) { | ||
if (!opts.overwrite && err.code === 'EEXIST') { | ||
return; | ||
} | ||
} | ||
fs.writeSync(write, buf, 0, bytesRead); | ||
while (bytesRead === BUF_LENGTH) { | ||
@@ -111,2 +106,3 @@ bytesRead = fs.readSync(read, buf, 0, BUF_LENGTH, pos); | ||
var stat = fs.fstatSync(read); | ||
fs.futimesSync(write, stat.atime, stat.mtime); | ||
@@ -113,0 +109,0 @@ fs.closeSync(read); |
{ | ||
"name": "cp-file", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Copy a file", | ||
@@ -32,7 +32,9 @@ "license": "MIT", | ||
"mkdirp": "^0.5.0", | ||
"object-assign": "^2.0.0" | ||
"object-assign": "^2.0.0", | ||
"onetime": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"mocha": "*", | ||
"rimraf": "^2.3.2" | ||
} | ||
} |
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
4065
4
2
84
+ Addedonetime@^1.0.0
+ Addedonetime@1.1.0(transitive)