Exclude pattern hack
See original GitHub issueI’ve wrote a function to simplify writing exclude pattern, due to the fact that ** matches all children and the parent.
del.sync(extendNegGlobs(['public/assets/**', '!public/assets/goat.png']));
// is equivalent with
del.sync(['public/assets/**', '!public/assets/goat.png', '!public/assets', '!public']);
Here is my function:
function extendNegGlobs(arr) {
var ext = [];
var negs = {};
arr.forEach((g, i) => {
if ( g.slice(0,1) == '!' ) {
negs[g] = i;
}
});
Object.keys(negs).forEach((g) => {
var d;
if ( g.slice(-1) == '*' ) do {
g = path.dirname(d = g);
if ( g == '!' || g == '.' || g == d ) break;
if ( !(g in negs) ) {
negs[g] = ext.push(g);
}
}
while ( true );
});
return arr.concat(ext);
}
I don’t pretend this is a complete/nice solution, but it might be a starting point to finding one!
Comments ?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:8
Top Results From Across the Web
FileSet and dynamic exclude patterns - VSoft Technologies Forums
Hello, Is it possible to add exclude patterns to a fileset define while the script ... As I said… bit of a hack...
Read more >Using exclusion patterns when grepping - or emacs
Using exclusion patterns when grepping ... grep for a line in all files tracked by Git, using ripgrep as the backend. ... Happy...
Read more >Safe Repositories With Priority Resolution - JFrog
Going Beyond Exclude Patterns: Safe Repositories With Priority ... The client resolves the latest version (1.7.0) and gets hacked.
Read more >grep for "term" and exclude "another term" - Super User
makes sure the string matches the previous pattern, ... I found this hack in Regular expression to match string not containing a word?...
Read more >How to exclude files and folders from Windows Defender scans
It is important to remember that you should never exclude a file or folder from antivirus scans unless you know for 100% that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I think a call like
del.sync(['public/assets/**', '!public/assets/goat.png']);should remove all files insidepublic/assets/exceptpublic/assets/goat.pngfile. The intention is clear in the pattern, but the behavior is not doing what “is obvious”.The intention of my hack was to make the “obvious” thing happen, but I agree it is not universal and only works with simple patterns.
The universal solution would be to make
delnot remove folders that contain at least one file that matches an exclude pattern.The issue lays in the implementation: del gets a files & folders list using globby and then removes these files and folders. But at this point exclude patterns are already applied, and the list might contains folders of excluded files, thus folders get removed together with their files.
I think the algo should be changed if we want to solve this issue. If my understanding of glob is right, there is only one pattern that excludes files (and folders) from previous patterns, and it starts with “!”. So
delshould not remove any folder that contains at least one file that matches any exclude pattern (in any subfolder).Hi @duzun,
I have the problems with the nested folder and how can apply your solution for this problem or we have another solution for this.
Structure:
Test folder ---- A folder ---- B folder ---- Resource folder ---- ---- assets folder ---- ---- ---- css folder ---- ---- ---- tools folder ---- ---- ---- ---- node_modules ---- ---- ---- ---- bower_components
How can I delete the Test folder without delete the node & bower folder.
My setting doesn’t work
Thanks,