You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently fast-glob silently omits any input files which do not exist from its output; this makes sense given that glob patterns need to be dropped in favor of the corresponding found files. However, in the context of a CLI tool for example, one might want to warn a user if a static pattern was not found given that a static path may be more intentional. As far as I can tell, because of the omission behavior, there isn't a super elegant way to achieve this.
If you pass a file list through fast-glob and intend to throw warnings when you read said files, any non-existing files will already be omitted by fast-glob:
import{sync}from"fast-glob";import{readFileSync}from"fs";letinputPaths=["path/does/not.exist","/some/*/glob/**"];inputPaths=sync(inputPaths,{unique: true});// expand globsinputPaths.forEach((filePath)=>{try{constcontents=readFileSync(filePath,"utf-8");// Do something with contents}catch(err){console.warn(`Could not open ${filePath}`);}});// will never warn about path/does/not.exist because `sync` call (line 5) strips it away
You can instead use isDynamicPattern to only pass dynamic patterns to sync...
import{isDynamicPattern,sync}from"fast-glob";import{readFileSync}from"fs";functionexpandInputPaths(paths: string[]): string[]{constsplitPaths=paths.reduce((acc,path)=>{acc[isDynamicPattern(path) ? "glob" : "globless"].push(path);returnacc;},{glob: [],globless: []}as{glob: string[];globless: string[]});constrecombinedPaths=sync(splitPaths["glob"]).concat(splitPaths["globless"]);return[...newSet(recombinedPaths)];// remove duplicates}letinputPaths=["path/does/not.exist","/some/*/glob/**"];inputPaths=expandInputPaths(inputPaths);// expand globsinputPaths.forEach((filePath)=>{try{constcontents=readFileSync(filePath,"utf-8");// Do something with contents}catch(err){console.warn(`Could not open ${filePath}`);}});
... but a) this has to repeat the iteration and checks that sync is already doing, and b) this doesn't allow you to use the nice fast-glob options like unique (because you aren't passing all the files to fast-glob)
I'm thinking if fast-glob had a way to keep the static patterns which don't exist, the first example would work. I'm welcome to suggestions of other ways to approach this as well.
The text was updated successfully, but these errors were encountered:
Currently fast-glob silently omits any input files which do not exist from its output; this makes sense given that glob patterns need to be dropped in favor of the corresponding found files. However, in the context of a CLI tool for example, one might want to warn a user if a static pattern was not found given that a static path may be more intentional. As far as I can tell, because of the omission behavior, there isn't a super elegant way to achieve this.
If you pass a file list through fast-glob and intend to throw warnings when you read said files, any non-existing files will already be omitted by fast-glob:
You can instead use
isDynamicPattern
to only pass dynamic patterns tosync
...... but a) this has to repeat the iteration and checks that
sync
is already doing, and b) this doesn't allow you to use the nice fast-glob options likeunique
(because you aren't passing all the files to fast-glob)I'm thinking if fast-glob had a way to keep the static patterns which don't exist, the first example would work. I'm welcome to suggestions of other ways to approach this as well.
The text was updated successfully, but these errors were encountered: