1 | 'use strict';
|
2 |
|
3 | const fireworm = require('fireworm');
|
4 | const EventEmitter = require('events').EventEmitter;
|
5 |
|
6 | module.exports = class FileWatcher extends EventEmitter {
|
7 | constructor(config) {
|
8 | super();
|
9 |
|
10 | this.fileWatcher = fireworm('./', {
|
11 | ignoreInitial: true,
|
12 | skipDirEntryPatterns: []
|
13 | });
|
14 | let onFileChanged = this.onFileChanged.bind(this);
|
15 | this.fileWatcher.on('change', onFileChanged);
|
16 | this.fileWatcher.on('add', onFileChanged);
|
17 | this.fileWatcher.on('remove', onFileChanged);
|
18 | this.fileWatcher.on('emfile', this.onEMFILE.bind(this));
|
19 |
|
20 | let watchFiles = config.get('watch_files');
|
21 | this.fileWatcher.clear();
|
22 | let confFile = config.get('file');
|
23 | if (confFile) {
|
24 | this.fileWatcher.add(confFile);
|
25 | }
|
26 | if (config.isCwdMode()) {
|
27 | this.fileWatcher.add('*.js');
|
28 | }
|
29 | if (watchFiles) {
|
30 | this.fileWatcher.add(watchFiles);
|
31 | }
|
32 | let srcFiles = config.get('src_files') || '*.js';
|
33 | this.fileWatcher.add(srcFiles);
|
34 | let ignoreFiles = config.get('src_files_ignore');
|
35 | if (ignoreFiles) {
|
36 | this.fileWatcher.ignore(ignoreFiles);
|
37 | }
|
38 | }
|
39 |
|
40 | onFileChanged(filePath) {
|
41 | this.emit('fileChanged', filePath);
|
42 | }
|
43 |
|
44 | onEMFILE() {
|
45 | this.emit('EMFILE');
|
46 | }
|
47 |
|
48 | add(file) {
|
49 | this.fileWatcher.add(file);
|
50 | }
|
51 | };
|