jprichardson / node-fs-extra

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

When copying a directory, filter should fire on each of the contents of it, not on the directory itself. #927

Closed mig82 closed 3 years ago

mig82 commented 3 years ago

I'm trying to copy the contents of a folder whose structure is this:

Foo
├── Endpoints
│   └── default.xml
├── Meta.json
└── Operations
    ├── create.xml
    └── getAccounts.xml

I want both the Endpoints and Operations directories (and any other that might be there in the future) copied over to the target location. I want to filter out Meta.json though. Like so.

targetLocation
├── Endpoints
│   └── default.xml
└── Operations
    ├── create.xml
    └── getAccounts.xml

Now, if I use

fs.copy('path/to/Foo', targetLocation, {
    filter: (src, dest) => { src !== 'Meta.json' }
})

What happens is that the filter only fires for the directory itself. Not for each of the entries in it. So the filtering doesn't work as expected.

P.S: Note that if instead I try to use fs.readdir to get the entries in the directory, filter Meta.json and then copy on each entry in the Foo directory, I end up with:

targetLocation
├── create.xml
├── default.xml
└── getAccounts.xml

Which does not preserve the folder structure. So this is not a suitable workaround.

nikhilnayyar002 commented 3 years ago

same. Using "fs-extra": "^10.0.0",

RyanZim commented 3 years ago

The filter function should fire for the directory and each entry. So you would do

filter: (src, dest) => { !src.endsWith('Meta.json') }
nikhilnayyar002 commented 2 years ago

Got that its working!