Skip to content

Commit

Permalink
Merge pull request #330 from jprichardson/copy-clobber
Browse files Browse the repository at this point in the history
BREAKING: Do not error when copy destination exists & clobber: false
  • Loading branch information
jprichardson authored Dec 29, 2016
2 parents 6d94a67 + 3fa83d4 commit 2dd4c0e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
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

0 comments on commit 2dd4c0e

Please sign in to comment.