//walker.js//Copy following code and paste in walker.js file
const fs = require('fs'), path = require('path'), {EventEmitter} = require('events'); module.exports = class FileWalker extends EventEmitter { constructor (entry,debug){ super(); this.isPaused = false; this.queue = []; this.debug = debug ? true : false; this.filter_dir = () => false; this.filter_file = () => false; this.start(entry); } filterDir (fn){ this.filter_dir = fn; } filterFile (fn){ this.filter_file = fn; } start (entry) { fs.lstat(entry, (err,stat) => { if (err){ this.debug&&console.log('Error stat: '+entry); this.emit('error',err,entry,stat); this.next(); return this; } if (stat.isFile()){ if (this.filter_file(entry,stat)){ this.debug&&console.log('filterFile: '+entry); return this.next(); } this.debug&&console.log('File: '+entry); this.emit('file',entry,stat); this.next(); } else if (stat.isDirectory()){ if (this.filter_dir(entry,stat)){ this.debug&&console.log('filterDir: '+entry); return this.next(); } this.debug&&console.log('Dir: '+entry); this.emit('dir',entry,stat); fs.readdir(entry, (err,files) => { if (err){ this.debug&&console.log('Error readdir: '+entry); this.emit('error',err,entry,stat); return this; } Array.prototype.push.apply(this.queue, files.map( file => { return path.join(entry,file); })); this.next(); }); } else { this.debug&&console.log('unknown or inaccessilbe: '+entry); this.emit('unknown',entry,stat); this.next() } }); } next(){ if (this.isPaused) { this.emit('pause'); this.debug&&console.log('isPaused'); return this; } let nextEntry = this.queue.shift(); if (!nextEntry){ this.emit('done'); this.debug&&console.log('Done'); return this; } this.start(nextEntry); } pause(){ if (this.isPaused === true){ debug&&console.log('isPaused failed. App was already paused'); return this; } this.isPaused = true; } resume(){ if (this.isPaused === false){ debug&&console.log('Resume failed. App was already resumed'); return; } this.isPaused = false; this.debug&&console.log('Resume'); this.next(); } }