Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

BREAKING: Don't allow copy()/copySync()'s filter option to be a Regex #512

Merged
merged 1 commit into from Nov 9, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/copy-sync.md
Expand Up @@ -9,7 +9,7 @@ Copy a file or directory. The directory can have contents. Like `cp -r`.
- `errorOnExist` `<boolean>`: when `overwrite` 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>`: 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).
- `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude.

## Example:

Expand Down
2 changes: 1 addition & 1 deletion docs/copy.md
Expand Up @@ -9,7 +9,7 @@ Copy a file or directory. The directory can have contents. Like `cp -r`.
- `errorOnExist` `<boolean>`: when `overwrite` 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>`: 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).
- `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude.
- `callback` `<Function>`

## Example:
Expand Down
12 changes: 4 additions & 8 deletions lib/copy-sync/copy-sync.js
Expand Up @@ -31,29 +31,25 @@ function copySync (src, dest, options) {
const stats = (options.recursive && !options.dereference) ? fs.lstatSync(src) : fs.statSync(src)
const destFolder = path.dirname(dest)
const destFolderExists = fs.existsSync(destFolder)
let performCopy = false

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, dest)
if (!options.filter(src, dest)) return

if (stats.isFile() && performCopy) {
if (stats.isFile()) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.errorOnExist,
preserveTimestamps: options.preserveTimestamps
})
} else if (stats.isDirectory() && performCopy) {
} else if (stats.isDirectory()) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
const contents = fs.readdirSync(src)
contents.forEach(content => {
const opts = options
opts.recursive = true
copySync(path.join(src, content), path.join(dest, content), opts)
})
} else if (options.recursive && stats.isSymbolicLink() && performCopy) {
} else if (options.recursive && stats.isSymbolicLink()) {
const srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
Expand Down
9 changes: 1 addition & 8 deletions lib/copy/copy.js
Expand Up @@ -47,14 +47,7 @@ function copy (src, dest, opts, cb) {
}

function startCopy (src, dest, opts, cb) {
if (opts.filter) {
if (opts.filter instanceof RegExp) {
console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function')
if (!opts.filter.test(src)) return cb()
} else if (typeof opts.filter === 'function') {
if (!opts.filter(src, dest)) return cb()
}
}
if (opts.filter && !opts.filter(src, dest)) return cb()
return getStats(src, dest, opts, cb)
}

Expand Down