globby icon indicating copy to clipboard operation
globby copied to clipboard

Feature request: negative patterns removing parent paths too

Open derMart opened this issue 9 years ago • 4 comments

If you have a match like ['parent/**','!/parent/level1/level2/test.txt'] currently the parent directories of test.txt will remain in the matching list:

'parent/'
'parent/level1/'
'parent/level1/level2/'

If you want to use f.e. https://github.com/sindresorhus/del to delete these matches test.txt will be removed too. To delete a directory, but preserve some subset, the parent directories of any negative match need to be removed too. See also https://github.com/sindresorhus/del/issues/3

derMart avatar Apr 21 '15 15:04 derMart

I believe that's the expected globbing behavior: ['parent/**','!/parent/level1/level2/test.txt'] gets all files and directories descendant of parent and excludes a single leaf file.

Adding your suggested magic does not only add complexity, it may also yield unexpected results for usages other than deleting. I believe the correct solution would be changing your glob patterns to:

['parent/**', '!parent/level1', '!parent/level1/level2', '!parent/level1/level2/test.txt']

@sindresorhus WDYT?

UltCombo avatar Apr 21 '15 17:04 UltCombo

Of course I don't want the default behaviour to change. I would suggest adding an option preferably in the options object, like the 'nodir' option for node-glob.

For simple situations your suggested solution would of course work, but in more complex situations it might get impractical or even impossible. Think of patterns where you want to keep the intermediate parent directories dynamic / you just don't know them. F.e. if you have some wildcards inside the negative match (see also https://github.com/sindresorhus/del/issues/3#issuecomment-69368298). How would you handle this currently?

derMart avatar Apr 21 '15 19:04 derMart

I see.

like the 'nodir' option for node-glob.

If you want the exactly same behavior as node-glob, then you can pass { nodir: true } to globby -- globby uses node-glob under the hood and forwards the options object to it.

If you want negative globs to also "match" the directories which are ancestors of a match (thus removing them from the results set), then things are more tricky. Perhaps a config option would make sense.

UltCombo avatar Apr 21 '15 20:04 UltCombo

I think this will have to be resolved in fast-glob first. So I suggest opening in issue over there.

sindresorhus avatar Jan 06 '20 09:01 sindresorhus