Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js glob pattern for excluding multiple files

Tags:

node.js

glob

I'm using the npm module node-glob.

This snippet returns recursively all files in the current working directory.

var glob = require('glob');
glob('**/*', function(err, files) {
    console.log(files);
});

sample output:

[ 'index.html', 'js', 'js/app.js', 'js/lib.js' ]

I want to exclude index.html and js/lib.js. I tried to exclude these files with negative pattern '!' but without luck. Is there a way to achieve this only by using a pattern?

like image 970
ke_wa Avatar asked May 22 '14 14:05

ke_wa


3 Answers

I suppose it's not actual anymore but I got stuck with the same question and found an answer.

This can be done using only glob module. We need to use options as a second parameter to glob function

glob('pattern', {options}, cb)

There is an options.ignore pattern for your needs.

var glob = require('glob');

glob("**/*",{"ignore":['index.html', 'js', 'js/app.js', 'js/lib.js']}, function (err, files) {
  console.log(files);
})
like image 194
Sergei Panfilov Avatar answered Oct 17 '22 21:10

Sergei Panfilov


Check out globby, which is pretty much glob with support for multiple patterns and a Promise API:

const globby = require('globby');

globby(['**/*', '!index.html', '!js/lib.js']).then(paths => {
    console.log(paths);
});
like image 47
Sindre Sorhus Avatar answered Oct 17 '22 21:10

Sindre Sorhus


You can use node-globule for that:

var globule = require('globule');
var result = globule.find(['**/*', '!index.html', '!js/lib.js']);
console.log(result);
like image 18
stefanbuck Avatar answered Oct 17 '22 21:10

stefanbuck