Comparing version 2.1.0 to 2.2.0
87
index.js
@@ -7,6 +7,17 @@ 'use strict'; | ||
var onetime = require('onetime'); | ||
var NestedError = require('nested-error-stacks'); | ||
var util = require('util'); | ||
function CpFileError(message, nested) { | ||
NestedError.call(this, message, nested); | ||
objectAssign(this, nested); | ||
} | ||
util.inherits(CpFileError, NestedError); | ||
CpFileError.prototype.name = 'CpFileError'; | ||
module.exports = function (src, dest, opts, cb) { | ||
if (!src || !dest) { | ||
throw new Error('`src` and `dest` required'); | ||
throw new CpFileError('`src` and `dest` required'); | ||
} | ||
@@ -25,3 +36,5 @@ | ||
read.on('error', cb); | ||
read.on('error', function (err) { | ||
cb(new CpFileError('cannot read from `' + src + '`: ' + err.message, err)); | ||
}); | ||
read.on('readable', readListener); | ||
@@ -33,3 +46,3 @@ read.on('end', readListener); | ||
if (err && err.code !== 'EEXIST') { | ||
cb(err); | ||
cb(new CpFileError('cannot create directory `' + path.dirname(dest) + '`: ' + err.message, err)); | ||
return; | ||
@@ -45,4 +58,3 @@ } | ||
} | ||
cb(err); | ||
cb(new CpFileError('cannot write to `' + dest + '`: ' + err.message, err)); | ||
}); | ||
@@ -53,7 +65,13 @@ | ||
if (err) { | ||
cb(err); | ||
cb(new CpFileError('lstat `' + src + '` failed: ' + err.message, err)); | ||
return; | ||
} | ||
fs.utimes(dest, stats.atime, stats.mtime, cb); | ||
fs.utimes(dest, stats.atime, stats.mtime, function (err) { | ||
if (err) { | ||
cb(new CpFileError('utimes `' + dest + '` failed: ' + err.message, err)); | ||
return; | ||
} | ||
cb(); | ||
}); | ||
}); | ||
@@ -69,3 +87,3 @@ }); | ||
if (!src || !dest) { | ||
throw new Error('`src` and `dest required'); | ||
throw new CpFileError('`src` and `dest` required'); | ||
} | ||
@@ -75,14 +93,35 @@ | ||
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; | ||
var read, bytesRead, pos, write, stat; | ||
function readSync(pos) { | ||
try { | ||
return fs.readSync(read, buf, 0, BUF_LENGTH, pos); | ||
} catch (err) { | ||
throw new CpFileError('cannot read from `' + src + '`: ' + err.message, err); | ||
} | ||
} | ||
function writeSync() { | ||
try { | ||
fs.writeSync(write, buf, 0, bytesRead); | ||
} catch (err) { | ||
throw new CpFileError('cannot write to `' + dest + '`: ' + err.message, err); | ||
} | ||
} | ||
try { | ||
read = fs.openSync(src, 'r'); | ||
} catch (err) { | ||
throw new CpFileError('cannot open `' + src + '`: ' + err.message, err); | ||
} | ||
pos = bytesRead = readSync(0); | ||
try { | ||
mkdirp.sync(path.dirname(dest)); | ||
} catch (err) { | ||
if (err.code !== 'EEXIST') { | ||
throw err; | ||
throw new CpFileError('cannot create directory `' + path.dirname(dest) + '`: ' + err.message, err); | ||
} | ||
@@ -97,16 +136,26 @@ } | ||
} | ||
throw new CpFileError('cannot write to `' + dest + '`: ' + err.message, err); | ||
} | ||
fs.writeSync(write, buf, 0, bytesRead); | ||
writeSync(); | ||
while (bytesRead === BUF_LENGTH) { | ||
bytesRead = fs.readSync(read, buf, 0, BUF_LENGTH, pos); | ||
fs.writeSync(write, buf, 0, bytesRead); | ||
bytesRead = readSync(pos); | ||
writeSync(); | ||
pos += bytesRead; | ||
} | ||
var stat = fs.fstatSync(read); | ||
fs.futimesSync(write, stat.atime, stat.mtime); | ||
try { | ||
stat = fs.fstatSync(read); | ||
} catch (err) { | ||
throw new CpFileError('stat `' + src + '` failed: ' + err.message, err); | ||
} | ||
try { | ||
fs.futimesSync(write, stat.atime, stat.mtime); | ||
} catch (err) { | ||
throw new CpFileError('utimes `' + dest + '` failed: ' + err.message, err); | ||
} | ||
fs.closeSync(read); | ||
fs.closeSync(write); | ||
}; |
{ | ||
"name": "cp-file", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Copy a file", | ||
@@ -10,4 +10,11 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "http://sindresorhus.com" | ||
"url": "sindresorhus.com" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "Michael Mayer", | ||
"email": "michael@schnittstabil.de", | ||
"url": "schnittstabil.de" | ||
} | ||
], | ||
"engines": { | ||
@@ -17,3 +24,4 @@ "node": ">=0.10.0" | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"cover": "istanbul cover -x test.js node_modules/mocha/bin/_mocha" | ||
}, | ||
@@ -29,3 +37,10 @@ "files": [ | ||
"fs", | ||
"stream" | ||
"stream", | ||
"file-system", | ||
"ncp", | ||
"fast", | ||
"quick", | ||
"data", | ||
"content", | ||
"contents" | ||
], | ||
@@ -35,2 +50,3 @@ "dependencies": { | ||
"mkdirp": "^0.5.0", | ||
"nested-error-stacks": "1.0.0", | ||
"object-assign": "^2.0.0", | ||
@@ -40,5 +56,8 @@ "onetime": "^1.0.0" | ||
"devDependencies": { | ||
"buffer-compare": "0.0.1", | ||
"istanbul": "^0.3.13", | ||
"mocha": "*", | ||
"rewire": "^2.3.1", | ||
"rimraf": "^2.3.2" | ||
} | ||
} |
@@ -8,3 +8,4 @@ # cp-file [![Build Status](https://travis-ci.org/sindresorhus/cp-file.svg?branch=master)](https://travis-ci.org/sindresorhus/cp-file) | ||
- User-friendly by creating non-existant destination directories for you. | ||
- Can be safe by turning off [overwriting](#optionsoverwrite). | ||
- Can be safe by turning off [overwriting](#optionsoverwrite). | ||
- User-friendly errors. | ||
@@ -14,3 +15,3 @@ | ||
```sh | ||
``` | ||
$ npm install --save cp-file | ||
@@ -17,0 +18,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
7139
4
126
73
5
5
+ Addednested-error-stacks@1.0.0
+ Addednested-error-stacks@1.0.0(transitive)