mrmlnc / fast-glob

:rocket: It's a very fast and efficient glob library for Node.js
MIT License
2.57k stars 112 forks source link

Feature request: early exit for sync implementation #225

Open joscha opened 5 years ago

joscha commented 5 years ago

Currently it is not possible to stop a sync glob task. Let's say I run a glob to see if any file exists (similar to Array.some()) then I currently have to do:

fg.sync('pattern').length > 0

but it means that I still create the result. Ideally I would be able to:

fg.sync('pattern', { maxMatches: 1 }).length === 1

where .sync would exit once it has found maxMatches matches.

joscha commented 5 years ago

I just stumbled across https://github.com/micromatch/glob-fs#middleware-examples which has the notion of a middleware, the above could also be implemented in that way, e.g.:

fg.sync('pattern', {
  middleware: (matchedFile, abort) => {
    if (foundWhatWeareLookingFor) {
      abort(); // this is the last iteration, `fg.sync` will return after this middleware
    }
    // acts like a filter, returning `true` means we want to include the match
    return true;
  }
});

which would be more flexible but also a fair bit more complex.

joscha commented 5 years ago

@mrmlnc would you accept a pull request for this feature?

mrmlnc commented 5 years ago

Now it just looks like the functionality is beyond the scope of this package. Perhaps in the future we should think about hooks inside the fs.walk package.

What problem are you trying to solve? Config search?

The maxMatches option right now look better.

joscha commented 5 years ago

Implementing maxMatches sounds great. The reason I need it is because I am searching for the existence of at least one of a specific type of file.