Wrapper around node-glob with a friendly DSL
Wrapper around node-glob with a friendly DSL.
Check out the examples below.
npm install globber
path
can be a base path to begin searching from or a glob string or an array of base paths and/or glob strings.
options
can be the following
absolute
{Boolean} - If true
, all paths returned will be absolute, regardless of the initial pathextension
{String} - Only search for files with extension extension
exclude
{String|Array} - Either a path or array of paths to be excluded from the result setrecursive
{Boolean} - If false
, will only search for files one level deepincludeDirectories
{Boolean} - if false
, the resulting paths
array will only include paths to files, not paths to directoriesAny options passed to globber not listed above will be passed to node-glob.
callback
function takes err
and paths
arguments.
Synchronous version of globber
.
For a more elaborate set of examples, check out the tests.
Give the current directory structure:
/Users/ben
└ project
└ templates
└ sessions
└ info.txt
login.mustache
users
└ index.mustache
info.txt
show.mustache
and a current working directory of /Users/ben
:
Perform a recursive search for all files in the project/templates
directory relative to the current working directory:
globber('project/templates', function(err, paths){
console.log(paths);
});
/*
[
'project/templates/sessions',
'project/templates/sessions/info.txt',
'project/templates/sessions/login.mustache',
'project/templates/users',
'project/templates/users/index.mustache',
'project/templates/users/info.txt',
'project/templates/users/show.mustache'
]
*/
Do the same as the above but exclude certain paths and return all paths as absolute paths:
var pathsToExclude = ['project/templates/users/info.txt', 'project/templates/sessions/info.txt'];
globber('project/templates', {absolute: true, exclude: pathsToExclude}, function(err, paths){
console.log(paths);
});
/*
[
'/Users/ben/project/templates/sessions',
'/Users/ben/project/templates/sessions/login.mustache',
'/Users/ben/project/templates/users',
'/Users/ben/project/templates/users/index.mustache',
'/Users/ben/project/templates/users/show.mustache'
]
*/
Scope the recursive search only to files with a "mustache" extension in the project/templates
directory relative to the current working directory:
globber('project/templates', {extension: 'mustache'}, function(err, paths){
console.log(paths);
});
/*
[
'project/templates/sessions',
'project/templates/sessions/login.mustache',
'project/templates/users',
'project/templates/users/index.mustache',
'project/templates/users/show.mustache'
]
*/
Search multiple directories:
globber(['project/templates/sessions', 'project/templates/users'], function(err, paths){
console.log(paths);
});
/*
[
'project/templates/sessions',
'project/templates/sessions/info.txt',
'project/templates/sessions/login.mustache',
'project/templates/users',
'project/templates/users/index.mustache',
'project/templates/users/info.txt',
'project/templates/users/show.mustache'
]
*/
Only search in the directory provided, not in any sub directories:
globber('project/templates', {recursive: false}, function(err, paths){
console.log(paths);
});
/*
[
'project/templates/sessions',
'project/templates/users'
]
*/
Only return paths that are files (will remove paths of directories from the result set):
globber('project/templates', {includeDirectories: false}, function(err, paths){
console.log(paths);
});
/*
[
'project/templates/sessions/info.txt',
'project/templates/sessions/login.mustache',
'project/templates/users/index.mustache',
'project/templates/users/info.txt',
'project/templates/users/show.mustache'
]
*/
Pass a glob string directly:
globber('project/templates/**/*.mustache', function(err, paths){
console.log(paths);
});
/*
[
'project/templates/sessions',
'project/templates/sessions/login.mustache',
'project/templates/users',
'project/templates/users/index.mustache',
'project/templates/users/show.mustache'
]
*/