Skip to content

Commit

Permalink
Merge pull request jprichardson#324 from jprichardson/copy-sync
Browse files Browse the repository at this point in the history
copySync() should apply filter to directories like copy()
  • Loading branch information
jprichardson authored Dec 6, 2016
2 parents 3345e63 + e3d37fd commit 5f319b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
22 changes: 21 additions & 1 deletion lib/copy-sync/__tests__/copy-sync-dir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ describe('+ copySync()', function () {
it('should should apply filter recursively', function () {
var FILES = 2
var filter = function (s) {
return /0$/i.test(s)
// Don't match anything that ends with a digit higher than 0:
return /(0|\D)$/i.test(s)
}

fs.mkdirsSync(src)
Expand Down Expand Up @@ -105,6 +106,25 @@ describe('+ copySync()', function () {
}
})

it('should apply the filter to directory names', function () {
var IGNORE = 'ignore'
var filter = function (p) {
return !~p.indexOf(IGNORE)
}

fs.mkdirsSync(src)

var ignoreDir = path.join(src, IGNORE)
fs.mkdirsSync(ignoreDir)

fs.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE))

fs.copySync(src, dest, filter)

assert(!fs.existsSync(path.join(dest, IGNORE)), 'directory was not ignored')
assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored')
})

describe('> when the destination dir does not exist', function () {
it('should create the destination directory and copy the file', function () {
var src = path.join(TEST_DIR, 'data/')
Expand Down
22 changes: 10 additions & 12 deletions lib/copy-sync/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,23 @@ function copySync (src, dest, options) {
var destFolderExists = fs.existsSync(destFolder)
var performCopy = false

if (stats.isFile()) {
if (options.filter instanceof RegExp) {
console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function')
performCopy = options.filter.test(src)
} else if (typeof options.filter === 'function') performCopy = options.filter(src)

if (performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {clobber: options.clobber, preserveTimestamps: options.preserveTimestamps})
}
} else if (stats.isDirectory()) {
if (options.filter instanceof RegExp) {
console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function')
performCopy = options.filter.test(src)
} else if (typeof options.filter === 'function') performCopy = options.filter(src)

if (stats.isFile() && performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {clobber: options.clobber, preserveTimestamps: options.preserveTimestamps})
} else if (stats.isDirectory() && performCopy) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
var contents = fs.readdirSync(src)
contents.forEach(function (content) {
var opts = options
opts.recursive = true
copySync(path.join(src, content), path.join(dest, content), opts)
})
} else if (options.recursive && stats.isSymbolicLink()) {
} else if (options.recursive && stats.isSymbolicLink() && performCopy) {
var srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
Expand Down

0 comments on commit 5f319b6

Please sign in to comment.