//walkerHelper.js
//Go back to tutorial
const walkerClass = require('./walker.js'),
OPT = require ('./options.js');
let includedDirs=[],
excludedDirs=[],
totalProcessedFiles=0,
totalProcessedDirs=0,
walker = new walkerClass();
walker.on('error', cbError);
walker.on('file', cbFile);
walker.on('dir', cbDir);
walker.on('unknown', cbUnknow);
walker.on('pause', cbPause);
walker.on('resume', cbResume);
walker.on('duplicate',cbDuplicate);
walker.on('done', cbDone);
process.on('message',(m)=>{
switch (m.walker){
case OPT.START:
start(m.dirs);
break;
case OPT.PAUSE:
walker.pause();
break;
case OPT.RESUME:
walker.resume();
break;
case OPT.RESET:
reset();
break;
}
})
function start(dirs){
reset();
for (let i in dirs){
let dir = dirs[i];
if (dir.isIncluded)
includedDirs.push(dir.path);
else
excludedDirs.push(dir.path);
}
walker.addToQueue(includedDirs);
walker.filterDir((dir,stat)=>{
return excludedDirs.indexOf(dir) !== -1
});
walker.next();
let obj = {
walker:OPT.STARTED
}
process.send(obj);
}
function reset(){
walker.reset();
includedDirs=[],
excludedDirs=[],
totalProcessedFiles=0,
totalProcessedDirs=0;
}
function cbError(err,entry,stat){
//Log errors
}
function cbUnknow(entry,stat){
//Log errors
}
function cbFile(entry,stat){
totalProcessedFiles++;
}
function cbDir(entry,stat){
totalProcessedDirs++
let obj = {
walker: OPT.DIR,
dir: entry,
stat: stat,
totalDirs:totalProcessedDirs
}
process.send(obj);
}
function cbPause(){
let obj = {
walker:OPT.PAUSED
}
process.send(obj);
}
function cbResume(){
let obj = {
walker: OPT.RESUMED
}
process.send(obj);
}
function cbDuplicate(files,size,ext,hash){
let df = JSON.stringify(
{size:size, id:hash+ext+size, files:files}
);
let obj = {
walker: OPT.WEBVIEW,
addRows: 'addRows('+df+')'
}
process.send(obj);
}
function cbDone(){
let obj = {
walker: OPT.DONE,
totalFiles:totalProcessedFiles,
totalDirs: totalProcessedDirs
}
process.send(obj);
this.reset();
}