-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
596 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,59 @@ | ||
'use strict'; | ||
|
||
const fireworm = require('fireworm'); | ||
const sane = require('sane'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
module.exports = class FileWatcher extends EventEmitter { | ||
constructor(config) { | ||
super(); | ||
|
||
this.fileWatcher = fireworm('./', { | ||
ignoreInitial: true, | ||
skipDirEntryPatterns: [] | ||
}); | ||
let onFileChanged = this.onFileChanged.bind(this); | ||
this.fileWatcher.on('change', onFileChanged); | ||
this.fileWatcher.on('add', onFileChanged); | ||
this.fileWatcher.on('remove', onFileChanged); | ||
this.fileWatcher.on('emfile', this.onEMFILE.bind(this)); | ||
let globPatterns = []; | ||
|
||
let watchFiles = config.get('watch_files'); | ||
this.fileWatcher.clear(); | ||
let confFile = config.get('file'); | ||
if (confFile) { | ||
this.fileWatcher.add(confFile); | ||
} | ||
addPatternToGlobList(globPatterns, confFile); | ||
|
||
if (config.isCwdMode()) { | ||
this.fileWatcher.add('*.js'); | ||
} | ||
if (watchFiles) { | ||
this.fileWatcher.add(watchFiles); | ||
addPatternToGlobList(globPatterns, '*.js'); | ||
} | ||
|
||
let watchFiles = config.get('watch_files'); | ||
addPatternToGlobList(globPatterns, watchFiles); | ||
|
||
let srcFiles = config.get('src_files') || '*.js'; | ||
this.fileWatcher.add(srcFiles); | ||
addPatternToGlobList(globPatterns, srcFiles); | ||
|
||
let ignoreFiles = config.get('src_files_ignore'); | ||
if (ignoreFiles) { | ||
this.fileWatcher.ignore(ignoreFiles); | ||
} | ||
|
||
let cwd = config.cwd(); | ||
|
||
this.fileWatcher = sane(cwd, { | ||
glob: globPatterns, | ||
ignored: ignoreFiles | ||
}); | ||
|
||
let onFileChanged = this.onFileChanged.bind(this); | ||
this.fileWatcher.on('change', onFileChanged); | ||
this.fileWatcher.on('add', onFileChanged); | ||
this.fileWatcher.on('delete', onFileChanged); | ||
} | ||
|
||
onFileChanged(filePath) { | ||
this.emit('fileChanged', filePath); | ||
} | ||
|
||
onEMFILE() { | ||
this.emit('EMFILE'); | ||
} | ||
|
||
add(file) { | ||
this.fileWatcher.add(file); | ||
this.fileWatcher.register(file); | ||
} | ||
}; | ||
|
||
function addPatternToGlobList(list, globPattern) { | ||
if (!globPattern) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(globPattern)) { | ||
globPattern.forEach((globPattern) => list.push(globPattern)); | ||
} else { | ||
list.push(globPattern); | ||
} | ||
} |
Oops, something went wrong.