It's a very fast and efficient glob library for Node.js.
This package provides methods for traversing the file system and returning pathnames that matched a defined set of a specified pattern according to the rules used by the Unix Bash shell with some simplifications, meanwhile results are returned in arbitrary order. Quick, simple, effective.
:warning: This package does not respect the order of patterns. First, all the negative patterns are applied, and only then the positive patterns. If you want to get a certain order of records, use sorting or split calls.
Returns the internal representation of patterns (Task is a combining patterns by base directory).
fg.generateTasks('*');[{ base: '.', // Parent directory for all patterns inside this task dynamic: true, // Dynamic or static patterns are in this task patterns: ['*'], positive: ['*'], negative: []}]
Converts a path to a pattern depending on the platform, including special character escaping.
Posix. Works similarly to the fg.posix.escapePath method.
Windows. Works similarly to the fg.win32.escapePath method, additionally converting backslashes to forward slashes in cases where they are not escape characters (!()+@{}[]).
Specifies the maximum number of concurrent requests from a reader to read directories.
:book: The higher the number, the higher the performance and load on the file system. If you want to read in quiet mode, set the value to a comfortable number or 1.
More details
In Node, there are two types of threads: Event Loop (code) and a Thread Pool (fs, dns, …). The thread pool size controlled by the UV_THREADPOOL_SIZE environment variable. Its default size is 4 (documentation). The pool is one for all tasks within a single Node process.
Any code can make 4 real concurrent accesses to the file system. The rest of the FS requests will wait in the queue.
:book: Each new instance of FG in the same Node process will use the same Thread pool.
But this package also has the concurrency option. This option allows you to control the number of concurrent accesses to the FS at the package level. By default, this package has a value equal to the number of cores available for the current Node process. This allows you to set a value smaller than the pool size (concurrency: 1) or, conversely, to prepare tasks for the pool queue more quickly (concurrency: Number.POSITIVE_INFINITY).
So, in fact, this package can only make 4 concurrent requests to the FS. You can increase this value by using an environment variable (UV_THREADPOOL_SIZE), but in practice this does not give a multiple advantage.
cwd
Type: string
Default: process.cwd()
The current working directory in which to search.
deep
Type: number
Default: Infinity
Specifies the maximum depth of a read directory relative to the start directory.
:book: If you specify a pattern with some base directory, this directory will not participate in the calculation of the depth of the found directories. Think of it as a cwd option.
followSymbolicLinks
Type: boolean
Default: true
Indicates whether to traverse descendants of symbolic link directories when expanding ** patterns.
:book: Note that this option does not affect the base directory of the pattern. For example, if ./a is a symlink to directory ./b and you specified ['./a**', './b/**'] patterns, then directory ./a will still be read.
:book: If the stats option is specified, the information about the symbolic link (fs.lstat) will be replaced with information about the entry (fs.stat) behind it.
fs
Type: FileSystemAdapter
Default: fs.*
Custom implementation of methods for working with the file system. Supports objects with enumerable properties only.
:book: Returns fs.stat instead of fs.lstat for symbolic links when the followSymbolicLinks option is specified.
:warning: Unlike object mode this mode requires additional calls to the file system. On average, this mode is slower at least twice. See old and modern mode for more details.
static. A pattern is considered static if it can be used to get an entry on the file system without using matching mechanisms. For example, the file.js pattern is a static pattern because we can just verify that it exists on the file system.
dynamic. A pattern is considered dynamic if it cannot be used directly to find occurrences without using a matching mechanisms. For example, the * pattern is a dynamic pattern because we cannot use this pattern directly.
A pattern is considered dynamic if it contains the following characters (… — any characters or their absence) or options:
Always use forward-slashes in glob expressions (patterns and ignore option). Use backslashes for escaping characters. With the cwd option use a convenient format.
:warning: When you write !**/second/**/* it means that the directory will be read, but all the entries will not be included in the results.
You have to understand that if you write the pattern to exclude directories, then the directory will not be read under any circumstances.
How to use UNC path?
You cannot use Uniform Naming Convention (UNC) paths as patterns (due to syntax) directly, but you can use them as cwd directory or use the fg.convertPathToPattern method.