Skip to content
/ all-files Public

function to get all files in folder and process them

License

Notifications You must be signed in to change notification settings

asci/all-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

all-files

Build NPM

Helpers to process files recursively in folder with promises

Prereq

  • node 4
  • *nix, macOs

How to install

From NPM

npm install node-all-files or yarn add node-all-files

How to use

getFilenames

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);
});

processFiles

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');
});

How to test

  • npm test