-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
38 lines (36 loc) · 996 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const EventEmitter = require('events');
const fs = require('fs');
class Watcher extends EventEmitter {
constructor(watchDir, processedDir){
super();
this.watchDir = watchDir;
this.processedDir = processedDir;
}
watch () {
fs.readdir(this.watchDir, (err, files) => {
if (err) {
throw err;
}
for (const index in files) {
this.emit('process', files[index]);
}
});
}
process () {
this.on('process', (file) => {
let watchFile = this.watchDir + '/' + file;
let processedFile = this.processedDir + '/' + file;
fs.rename(watchFile, processedFile, (err) => {
if(err) throw err;
});
});
}
start () {
this.process();
fs.watchFile (watchDir, () => {
this.watch();
});
}
}
const watcher = new Watcher('./watch', './done');
watcher.start();