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

Add dest parameter to filter option in copy and copySync #366

Merged
merged 3 commits into from Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 18 additions & 23 deletions lib/copy-sync/__tests__/copy-sync-dir.test.js
Expand Up @@ -89,12 +89,12 @@ describe('+ copySync()', () => {
})

describe('> when filter is used', () => {
it('should should apply filter recursively', done => {
it('should should apply filter recursively', () => {
const FILES = 2
// Don't match anything that ends with a digit higher than 0:
const filter = s => /(0|\D)$/i.test(s)

fs.mkdirsSync(src)
fs.mkdirSync(src)

for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
Expand Down Expand Up @@ -129,10 +129,9 @@ describe('+ copySync()', () => {
assert(!fs.existsSync(path.join(destSub, j.toString())))
}
}
done()
})

it('should apply the filter to directory names', done => {
it('should apply the filter to directory names', () => {
const IGNORE = 'ignore'
const filter = p => !~p.indexOf(IGNORE)

Expand All @@ -147,50 +146,47 @@ describe('+ copySync()', () => {

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

it('should apply filter when it is applied only to dest', done => {
it('should apply filter when it is applied only to dest', () => {
const timeCond = new Date().getTime()

const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond

const subdir = path.join(src, 'subdir')
fs.mkdirsSync(subdir)

const dest = path.join(TEST_DIR, 'dest')

setTimeout(() => {
fs.mkdirsSync(src)
fs.writeFileSync(path.join(src, 'somefile.html'), 'some data')
fs.mkdirsSync(dest)
try {
fs.copySync(src, dest, filter)
} catch (err) {
assert.ifError(err)
}
assert(!fs.existsSync(path.join(dest, 'subdir')))
done()
assert(!fs.existsSync(path.join(dest, 'somefile.html')))
}, 1000)
})

it('should apply filter when it is applied to both src and dest', done => {
it('should apply filter when it is applied to both src and dest', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need done when you're using setTimeout (since that makes the code async).

const timeCond = new Date().getTime()
const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond

const dest = path.join(TEST_DIR, 'dest')

const srcFile1 = path.join(TEST_DIR, '1.html')
const srcFile2 = path.join(TEST_DIR, '2.css')
const srcFile3 = path.join(TEST_DIR, '3.jade')
setTimeout(() => {
const srcFile1 = path.join(TEST_DIR, '1.html')
const srcFile2 = path.join(TEST_DIR, '2.css')
const srcFile3 = path.join(TEST_DIR, '3.jade')

fs.writeFileSync(srcFile1, '')
fs.writeFileSync(srcFile2, '')
fs.writeFileSync(srcFile3, '')
fs.writeFileSync(srcFile1, '')
fs.writeFileSync(srcFile2, '')
fs.writeFileSync(srcFile3, '')

const destFile1 = path.join(dest, 'dest1.html')
const destFile2 = path.join(dest, 'dest2.css')
const destFile3 = path.join(dest, 'dest3.jade')
const destFile1 = path.join(dest, 'dest1.html')
const destFile2 = path.join(dest, 'dest2.css')
const destFile3 = path.join(dest, 'dest3.jade')

setTimeout(() => {
fs.mkdirsSync(dest)

fs.copySync(srcFile1, destFile1, filter)
Expand All @@ -200,7 +196,6 @@ describe('+ copySync()', () => {
assert(fs.existsSync(destFile1))
assert(!fs.existsSync(destFile2))
assert(fs.existsSync(destFile3))
done()
}, 1000)
})
})
Expand Down
2 changes: 1 addition & 1 deletion lib/copy-sync/copy-sync.js
Expand Up @@ -35,7 +35,7 @@ function copySync (src, dest, options) {

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

if (stats.isFile() && performCopy) {
Expand Down
2 changes: 1 addition & 1 deletion lib/copy/ncp.js
Expand Up @@ -37,7 +37,7 @@ function ncp (source, dest, options, callback) {
if (filter) {
if (filter instanceof RegExp) {
console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function')
if (!filter.test(source, dest)) {
if (!filter.test(source)) {
return doneOne(true)
}
} else if (typeof filter === 'function') {
Expand Down