1.1.2 • Published 9 years ago

fs-expand v1.1.2

Weekly downloads
315
License
MIT
Repository
github
Last release
9 years ago

Build Status

fs-expand

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'

expand(pattern, options, callback);

  • 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)

options.globOnly

Type Boolean=false

Only process glob patterns, if a file does not contain globstars, fs-expand will not check the existence of the file.

<cwd>/
     |-- a.js
     |-- b.js
expand([
  '*.js',
  'abc.md'
], {
  cwd: cwd,
  globOnly: true

}, function(err, files){
  files;
  // ->
  // [
  //   'a.js',
  //   'b.js',
  //   'abc.md' // actually, abc.md doesn't exist.
  // ]
});

options.filter

Type String|Function

Either a valid fs.Stats method name or a function that is passed the matched src filepath and returns true or false.

fs.Stats method name
{
  filter: 'isDirectory'
}
Filter function
{
  filter: function (src) {
    return fs.statSync(src).isFile()
  }
}

It can also be asynchoronous function by using the common this.async() style, see wrap-as-async for details

{
  filter: function (src) {
    // Turns the filter function into an async one by calling `this.async()`
    var done = this.async()
    fs.stat(src, function (err, stat) {
      if (err) {
        return done(err)
      }

      done(null, stat.isFile())
    })
  }
}

Example

dir/
   |-- a.js
   |-- b.js
   |-- README.md
var expand = require('fs-expand');

expand(['*.js', '*.md'], {
  cwd: dir
}, function(err, files){
	console.log(files); // ['a.js', 'b.js', README.md]
});

expand.sync(pattern, options);

The synchronous version of expand.

Returns the filenames found.

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

console.log(files); // ['b.js']
1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.2.2

10 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago