question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exclude pattern hack

See original GitHub issue

I’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:open
  • Created 8 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

2reactions
duzuncommented, Aug 27, 2016

I think a call like del.sync(['public/assets/**', '!public/assets/goat.png']); should remove all files inside public/assets/ except public/assets/goat.png file. 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 del not 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 del should not remove any folder that contains at least one file that matches any exclude pattern (in any subfolder).

0reactions
tantvcommented, Jun 9, 2017

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

gulp.task('clean', function () {
        return del([
            'Test/**/*',
            'Test/'
            '!Test/Resource/',
            '!Test/Resource/**',
            '!Test/Resource/assets/',
            '!Test/Resource/assets/**',
            '!Test/Resource/assets/tools/',
            '!Test/Resource/assets/tools/**'
        ], {force: true});
});

Thanks,

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found