const events = require("events"),
util = require("util");
const fs = require("fs"),
watchDir = "./watch",
processedDir = "./done";
class Watcher extends events.EventEmitter {
constructor(watchDir, processedDir) {
super();
this.watchDir = watchDir;
this.processedDir = processedDir;
}
watch() {
const watcher = this;
fs.readdir(this.watchDir, function(err, files) {
if (err) throw err;
for (let index in files) {
watcher.emit("process", files[index]);
}
});
}
start() {
var watcher = this;
fs.watchFile(watchDir, function() {
watcher.watch();
});
}
}
let watcher = new Watcher(watchDir, processedDir);
watcher.on("process", function process(file) {
const watchFile = this.watchDir + "/" + file;
const processedFile = this.processedDir + "/" + file.toLowerCase();
fs.rename(watchFile, processedFile, function(err) {
if (err) throw err;
});
});
watcher.start();