-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.js
39 lines (34 loc) · 1016 Bytes
/
watch.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
var co = require('co');
var chokidar = require('chokidar');
var fs = require('fs');
var path = require('path');
var pathMap = require('./pathMap');
var thunkify = require('thunkify');
/**
* 监控指定目录,生成URL路径 map.json
*/
var fsWrite = thunkify(fs.writeFile);
var createMap = function (splitFile){
return co(function *(){
var pathList = yield pathMap.createMap(path.join(__filename, '../' + splitFile));
var s = yield pathMap.traFileName(pathList, splitFile);
yield fsWrite('map.json', JSON.stringify(s));
}).catch(function(err){
console.log(err)
});
}
module.exports = function *(splitFile){
var watcher = chokidar.watch('./' + splitFile, { persistent: true });
watcher.on('add', function (filePath) {
createMap(splitFile);
})
.on('change',function(filePath){
createMap(splitFile);
})
.on('error',function(filePath){
console.log(filePath)
})
.on('unlink', function(filePath){
createMap(splitFile);
})
}