jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

fs.copy/copySync when filter is return false, it will break copy #1007

Closed haozi closed 1 year ago

haozi commented 1 year ago
fs.copy(path.join(__dirname, 'src'), path.join(__dirname, 'dist'), {
   filter: (src: string) => {
       return path.extname(src) !== '.js'
   }
})

when I copying a folder, I wish skip file it is not *.js ,but if I return false, it will break copy, the desired purpose is to copy all non-.js files

RyanZim commented 1 year ago

filter is passed all paths, including directories. You'll have to add logic to only run your .js check on files, and return true for directories (note that this will copy empty directories if they do not contain JS files).

haozi commented 1 year ago

filter is passed all paths, including directories. You'll have to add logic to only run your .js check on files, and return true for directories (note that this will copy empty directories if they do not contain JS files).

but if allow folder return true, it will copy a lot of empty folder

RyanZim commented 1 year ago

Honestly, the filter probably isn't the best tool for the job here, you'd be better off using something like https://www.npmjs.com/package/globby to get a list of JS files, then copying each of them individually.

haozi commented 1 year ago

Honestly, the filter probably isn't the best tool for the job here, you'd be better off using something like https://www.npmjs.com/package/globby to get a list of JS files, then copying each of them individually.

Could you consider adding support for glob matching in the first parameter? Like:

fs.copy('src/**/*.png', dest)
RyanZim commented 1 year ago

Glob support adds another entire layer of complexity and edge cases to deal with, it's unlikely we'll add it.