-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWatcher.js
43 lines (37 loc) · 1.09 KB
/
Watcher.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
39
40
41
42
43
// create Class constructor
// inherit EventEmitter
// extend Functions
function Watcher(watchDir, processedDir) {
this.watchDir = watchDir
this.processedDir = processedDir
}
var events = require('events')
var util = require('util')
util.inherits(Watcher, events.EventEmitter)
var fs = require('fs')
var watchDir = './watch'
var processedDir = './done'
Watcher.prototype.watch = function () {
var watcher = this
fs.readdir(this.watchDir, function (err, files) {
if (err) throw err
for (var index in files) {
watcher.emit('process', files[index])
}
})
}
Watcher.prototype.start = function () {
var watcher = this
fs.watchFile(watchDir, function () {
watcher.watch()
})
}
var watcher = new Watcher(watchDir, processedDir)
watcher.on('process', function process(file) {
var watchFile = this.watchDir + '/' + file
// file.toLowerCase().replace('[阳光电影www.ygdy8.com].','')
var processedFile = this.processedDir + '/' + file.toLowerCase().replace('[阳光电影www.ygdy8.com].','')
fs.rename(watchFile, processedFile, function (err) {
if (err) throw err
})
})