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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,32 @@ describe('+ copySync()', () => { | |
assert.strictEqual(link, 'destination') | ||
}) | ||
|
||
it('should should apply filter recursively', () => { | ||
describe('> when the destination dir does not exist', () => { | ||
it('should create the destination directory and copy the file', () => { | ||
const src = path.join(TEST_DIR, 'data/') | ||
fs.mkdirSync(src) | ||
|
||
const d1 = 'file1' | ||
const d2 = 'file2' | ||
|
||
fs.writeFileSync(path.join(src, 'f1.txt'), d1) | ||
fs.writeFileSync(path.join(src, 'f2.txt'), d2) | ||
|
||
const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir') | ||
|
||
fs.copySync(src, dest) | ||
|
||
const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8') | ||
const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8') | ||
|
||
assert.strictEqual(d1, o1) | ||
assert.strictEqual(d2, o2) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('> when filter is used', () => { | ||
it('should should apply filter recursively', done => { | ||
const FILES = 2 | ||
// Don't match anything that ends with a digit higher than 0: | ||
const filter = s => /(0|\D)$/i.test(s) | ||
|
@@ -104,9 +129,10 @@ describe('+ copySync()', () => { | |
assert(!fs.existsSync(path.join(destSub, j.toString()))) | ||
} | ||
} | ||
done() | ||
}) | ||
|
||
it('should apply the filter to directory names', () => { | ||
it('should apply the filter to directory names', done => { | ||
const IGNORE = 'ignore' | ||
const filter = p => !~p.indexOf(IGNORE) | ||
|
||
|
@@ -121,29 +147,61 @@ 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this change. Using |
||
}) | ||
|
||
describe('> when the destination dir does not exist', () => { | ||
it('should create the destination directory and copy the file', () => { | ||
const src = path.join(TEST_DIR, 'data/') | ||
fs.mkdirSync(src) | ||
it('should apply filter when it is applied only to dest', done => { | ||
const timeCond = new Date().getTime() | ||
|
||
const d1 = 'file1' | ||
const d2 = 'file2' | ||
const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond | ||
|
||
fs.writeFileSync(path.join(src, 'f1.txt'), d1) | ||
fs.writeFileSync(path.join(src, 'f2.txt'), d2) | ||
const subdir = path.join(src, 'subdir') | ||
fs.mkdirsSync(subdir) | ||
|
||
const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir') | ||
const dest = path.join(TEST_DIR, 'dest') | ||
|
||
fs.copySync(src, dest) | ||
setTimeout(() => { | ||
fs.mkdirsSync(dest) | ||
try { | ||
fs.copySync(src, dest, filter) | ||
} catch (err) { | ||
assert.ifError(err) | ||
} | ||
assert(!fs.existsSync(path.join(dest, 'subdir'))) | ||
done() | ||
}, 1000) | ||
}) | ||
|
||
const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8') | ||
const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8') | ||
it('should apply filter when it is applied to both src and dest', done => { | ||
const timeCond = new Date().getTime() | ||
const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond | ||
|
||
assert.strictEqual(d1, o1) | ||
assert.strictEqual(d2, o2) | ||
}) | ||
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') | ||
|
||
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') | ||
|
||
setTimeout(() => { | ||
fs.mkdirsSync(dest) | ||
|
||
fs.copySync(srcFile1, destFile1, filter) | ||
fs.copySync(srcFile2, destFile2, filter) | ||
fs.copySync(srcFile3, destFile3, filter) | ||
|
||
assert(fs.existsSync(destFile1)) | ||
assert(!fs.existsSync(destFile2)) | ||
assert(fs.existsSync(destFile3)) | ||
done() | ||
}, 1000) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,8 @@ 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) | ||
} else if (typeof options.filter === 'function') performCopy = options.filter(src) | ||
performCopy = options.filter.test(src, dest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change doesn't do anything; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very true. I missed it. Thanks @RyanZim. Great that you caught this. |
||
} else if (typeof options.filter === 'function') performCopy = options.filter(src, dest) | ||
|
||
if (stats.isFile() && performCopy) { | ||
if (!destFolderExists) mkdir.mkdirsSync(destFolder) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,11 +37,11 @@ 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)) { | ||
if (!filter.test(source, dest)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change doesn't do anything; |
||
return doneOne(true) | ||
} | ||
} else if (typeof filter === 'function') { | ||
if (!filter(source)) { | ||
if (!filter(source, dest)) { | ||
return doneOne(true) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert this change. Using
done
isn't needed since we are using sync methods here.