Skip to content

Commit

Permalink
Replace fireworm with sane
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 8, 2021
1 parent 9bdde16 commit a113ba4
Show file tree
Hide file tree
Showing 4 changed files with 596 additions and 198 deletions.
64 changes: 36 additions & 28 deletions lib/file_watcher.js
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);
}
}
Loading

0 comments on commit a113ba4

Please sign in to comment.