0

I'm writing a node script that is supposed to take a directory and an extension name as args, and log all of the files in that directory that end with that extension. Sounds simple enough, but my script returns an empty array:

var fs = require('fs');
var ref = process.argv[2];
var extension = process.argv[3];
var files = [];
var dir = fs.readdir(ref, function(err, list){
    if (err) throw err;    
    var filterRx = new RegExp("\." + extension + "$");
    files = list.filter(function(val){
        return filterRx.test(val);
    });


});
console.log(files);

Can anyone help me understand why this isn't working as expected?

Edit: For example, Given a directory with the following files (given by console.log(list) in the callback):

   "[ 'CHANGELOG.md',"                 !=    "LICENCE.md"                       
   "  'LICENCE.md',"                   !=    "README.md"                        
   "  'README.md',"                    !=    ""                                 
   "  'dat',"                          !=                                       
   "  'data.dat',"                     !=                                       
   "  'data.json',"                    !=                                       
   "  'learnyounode.dat',"             !=                                       
   "  'learnyounode.sql',"             !=                                       
   "  'learnyounode.txt',"             !=                                       
   "  'md',"                           !=                                       
   "  'w00t.dat',"                     !=                                       
   "  'w00t.txt',"                     !=                                       
   "  'words.dat',"                    !=                                       
   "  'wrrrrongdat' ]"        

And passing the second argument "md", I should get "CHANGELOG.md", "LICENSE.md", etc

I only get [] :/

David J.
  • 1,753
  • 13
  • 47
  • 96

0 Answers0