-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
37 lines (30 loc) · 843 Bytes
/
index.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
"use strict";
const fs = require('fs');
const debounce = require('debounce');
class FSNotifier {
constructor(options) {
this.ui = options.ui;
this.targetDir = options.targetDir;
}
subscribe(notify) {
// Debounce Notify so it's only called once, filesystem change will shoot
// off multiple notifiers otherwise.
this.notify = debounce(notify, 200);
return this.initWatcher();
}
initWatcher() {
return new Promise((resolve, reject) => {
fs.watch(this.targetDir, {}, (event, filename) => {
this.hasWatcher = true;
if (event === 'error') {
this.ui.writeError('error while watching target directory');
reject(err);
} else if (event === 'change') {
this.notify();
}
});
resolve();
});
}
}
module.exports = FSNotifier;