Globber

Wrapper around node-glob with a friendly DSL

View the Project on GitHub benjreinhart/globber

Globber

Build Status

Wrapper around node-glob with a friendly DSL.

Check out the examples below.

Install

npm install globber

API

globber(path[, options], callback)

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

Any options passed to globber not listed above will be passed to node-glob.

callback function takes err and paths arguments.

globber.sync(path[, options])

Synchronous version of globber.

Examples

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'
  ]
*/

License

MIT