An extended fs glob


fs-expand is a standalone module to fetch all file or directory paths that match the given globbing pattern(s), which is much like grunt.file.expand

The difference from glob is that fs-expand

  • could combine the results of several glob patterns.
  • supports negative matching patterns, such as '!*.js'
  • pattern String|Array.<String> accepts either a single glob pattern or an array of globbing patterns. Paths matching patterns that begin with ! will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant.
  • options Object supports all glob library options
  • callback function(err, files) the callback function.
  • err Error
  • files Array.<String> filenames found matching the pattern(s)
  • globOnly Boolean=false only process glob patterns, if a file does not contain globstars, fs-expand will not check the existence of the file.
     |-- a.js
     |-- b.js
], {
  cwd: cwd,
  globOnly: true 
}, function(errfiles){
  // ->  
  // [ 
  //   'a.js', 
  //   'b.js', 
  //   'abc.md' // actually, abc.md doesn't exist. 
  // ] 
   |-- a.js
   |-- b.js
   |-- README.md
var expand = require('fs-expand');
expand(['*.js', '*.md'], {
  cwd: dir
}, function(errfiles){
    console.log(files); // ['a.js', 'b.js', README.md] 

The synchronous version of expand.

Returns the filenames found.

var files = expand.sync(['*.js', '!a.js']);
console.log(files); // ['b.js']