Helpers to process files recursively in folder with promises
- node 4
- *nix, macOs
npm install node-all-files
or yarn add node-all-files
Getting all *.js
files in folder ./test-dir
:
const { getFilenames } = require('node-all-files');
const folder = __dirname + '/test-dir';
// Supports RegExp, String and Function for filtering:
getFilenames(folder, /\.js$/i);
//or
getFilenames(folder, '.js');
//or
getFilenames(folder, (f) => ~f.indexOf('.js'))
.then(({ files }) => {
console.log(files);
});
Showing content length of the files
const { processFiles } = require('node-all-files');
const files = [__filename];
processFiles(files, (buff, filename) => {
// process file immediately
console.log(buff.toString().length);
// or return Promise
return new Promise((resolve) => {
setTimeout(resolve, 100);
});
}).then(({results, errors}) => {
// results - array of results
// errors - object (map) with filename as key, and error (or reject reason) as value
console.log('All files were processed');
});
npm test