What is dir-glob?
The dir-glob npm package is designed to convert directory paths into glob patterns, which can then be used with other tools that work with globs, such as file system search utilities. This can be particularly useful for including or excluding entire directories when working with file patterns.
What are dir-glob's main functionalities?
Convert directories to glob patterns
This feature allows you to convert an array of directory paths into glob patterns that match all files within those directories. The example demonstrates converting two directory paths into their corresponding glob patterns.
const dirGlob = require('dir-glob');
(async () => {
const paths = await dirGlob(['assets', 'uploads']);
console.log(paths);
//=> ['assets/**', 'uploads/**']
})();
Include file extensions in glob patterns
This feature extends the basic directory-to-glob conversion by allowing you to specify file extensions. This results in glob patterns that match only files with the specified extensions within the given directories. The example shows how to generate glob patterns for jpg and png files only.
const dirGlob = require('dir-glob');
(async () => {
const paths = await dirGlob(['assets', 'uploads'], {extensions: ['jpg', 'png']});
console.log(paths);
//=> ['assets/**/*.jpg', 'assets/**/*.png', 'uploads/**/*.jpg', 'uploads/**/*.png']
})();
Other packages similar to dir-glob
glob
The 'glob' package provides pattern matching functionality for file paths. While 'glob' itself focuses on matching patterns against existing file structures, 'dir-glob' specifically converts directory paths into glob patterns. 'glob' can be used in conjunction with 'dir-glob' to first generate glob patterns for directories and then match those patterns against files.
fast-glob
Similar to 'glob', 'fast-glob' is an optimized version that provides a faster and more efficient way to match glob patterns against file systems. It offers a similar functionality to 'dir-glob' in terms of working with glob patterns, but it does not specifically focus on converting directories to globs. Instead, it's more about performing the matching operation itself, potentially using patterns generated by 'dir-glob'.
dir-glob
Convert directories to glob compatible strings
Install
$ npm install dir-glob
Usage
const dirGlob = require('dir-glob');
(async () => {
console.log(await dirGlob(['index.js', 'test.js', 'fixtures']));
console.log(await dirGlob(['index.js', 'inner_folder'], {cwd: 'fixtures'}));
console.log(await dirGlob(['lib/**', 'fixtures'], {
files: ['test', 'unicorn']
extensions: ['js']
}));
console.log(await dirGlob(['lib/**', 'fixtures'], {
files: ['test', 'unicorn', '*.jsx'],
extensions: ['js', 'png']
}));
})();
API
dirGlob(input, options?)
Returns a Promise<string[]>
with globs.
dirGlob.sync(input, options?)
Returns a string[]
with globs.
input
Type: string | string[]
Paths.
options
Type: object
extensions
Type: string[]
Append extensions to the end of your globs.
files
Type: string[]
Only glob for certain files.
cwd
Type: string[]
Test in specific directory.