Skip to content

Instantly share code, notes, and snippets.

@myndzi
Created April 25, 2014 18:40
Show Gist options
  • Save myndzi/11299120 to your computer and use it in GitHub Desktop.
Save myndzi/11299120 to your computer and use it in GitHub Desktop.
Get list of files in directory matching mask, excluding directories, with Bluebird
return Promise.promisify(fs.readdir)(config.path).filter(function (fileName) {
return config.mask.test(fileName);
}).map(function (fileName) {
var fullPath = path.join(config.path, fileName);
return Promise.promisify(fs.stat)(fullPath).tap(function (stat) {
stat.fullPath = fullPath;
stat.fileName = fileName;
});
}).filter(function (stat) {
return stat.isFile();
}).map(function (stat) {
return stat.fullPath;
});
@myndzi
Copy link
Author

myndzi commented Apr 25, 2014

fs.readdir(function (err, res) {
  if (err) { throw err; }
  res = res.filter(function (fileName) {
    return config.mask.test(fileName);
  });
  var stats = [], count = res.length;
  res.forEach(function (fileName, idx) {
    var fullPath = path.join(config.path, fileName);
    fs.stat(fullPath, function (err, res) {
      if (err) { throw err; }
      stats[idx] = res;
      count--;
      if (count === 0) {
        res = res.filter(function (fileName, idx) {
          return stats[idx].isFile();
        });
      }
    });
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment