Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

BREAKING: Do not error when copy destination exists & clobber: false #330

Merged
merged 1 commit into from
Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ Methods
Copy a file or directory. The directory can have contents. Like `cp -r`.

Options:
- clobber (boolean): overwrite existing file or directory, default is `true`.
- clobber (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
- errorOnExist (boolean): when `clobber` is `false` and the destination exists, throw an error. Default is `false`.
- dereference (boolean): dereference symlinks, default is `false`.
- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`.
- filter: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background). _Warning: `copySync` currently applies the filter only to files (see [#180](https://github.com/jprichardson/node-fs-extra/issues/180)). This will be fixed in a future release._
Expand Down
11 changes: 9 additions & 2 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,16 @@ describe('+ copySync()', function () {
})

describe('> when clobber is false', function () {
it('should copy the file and THROW an error', function () {
it('should not throw an error', function () {
fs.copySync(src, dest, {clobber: false})

// copy never happened
var destDataNew = fs.readFileSync(dest, 'utf8')
assert.strictEqual(destData, destDataNew)
})
it('should throw an error when errorOnExist is true', function () {
assert.throws(function () {
fs.copySync(src, dest, {clobber: false})
fs.copySync(src, dest, {clobber: false, errorOnExist: true})
})

// copy never happened
Expand Down
11 changes: 4 additions & 7 deletions lib/copy-sync/copy-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ var _buff = new Buffer(BUF_LENGTH)

function copyFileSync (srcFile, destFile, options) {
var clobber = options.clobber
var errorOnExist = options.errorOnExist
var preserveTimestamps = options.preserveTimestamps

if (fs.existsSync(destFile)) {
if (clobber) {
fs.unlinkSync(destFile)
} else {
var err = new Error('EEXIST: ' + destFile + ' already exists.')
err.code = 'EEXIST'
err.errno = -17
err.path = destFile
throw err
}
} else if (errorOnExist) {
throw new Error(destFile + ' already exists')
} else return
}

var fdr = fs.openSync(srcFile, 'r')
Expand Down
6 changes: 5 additions & 1 deletion lib/copy-sync/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function copySync (src, dest, options) {

if (stats.isFile() && performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {clobber: options.clobber, preserveTimestamps: options.preserveTimestamps})
copyFileSync(src, dest, {
clobber: options.clobber,
errorOnExist: options.errorOnExist,
preserveTimestamps: options.preserveTimestamps
})
} else if (stats.isDirectory() && performCopy) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
var contents = fs.readdirSync(src)
Expand Down
13 changes: 12 additions & 1 deletion lib/copy/__tests__/ncp/ncp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,20 @@ describe('ncp', function () {
cb()
})
})
it('errors if files exist', function (cb) {
it('should not error if files exist', function (cb) {
ncp(src, out, function () {
ncp(src, out, {clobber: false}, function (err) {
assert.ifError(err)
cb()
})
})
})
it('should error if errorOnExist and file exists', function (cb) {
ncp(src, out, function () {
ncp(src, out, {
clobber: false,
errorOnExist: true
}, function (err) {
assert(err)
cb()
})
Expand Down
9 changes: 4 additions & 5 deletions lib/copy/ncp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function ncp (source, dest, options, callback) {
var filter = options.filter
var transform = options.transform
var clobber = options.clobber !== false // default true
var errorOnExist = options.errorOnExist
var dereference = options.dereference
var preserveTimestamps = options.preserveTimestamps === true

Expand Down Expand Up @@ -81,12 +82,10 @@ function ncp (source, dest, options, callback) {
rmFile(target, function () {
copyFile(file, target)
})
} else if (errorOnExist) {
onError(new Error(target + ' already exists'))
} else {
var err = new Error('EEXIST: ' + target + ' already exists.')
err.code = 'EEXIST'
err.errno = -17
err.path = target
onError(err)
doneOne()
}
}
})
Expand Down