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 24, 2021
1 parent 9bdde16 commit def0c06
Show file tree
Hide file tree
Showing 5 changed files with 596 additions and 210 deletions.
12 changes: 0 additions & 12 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const EventEmitter = require('events').EventEmitter;
const Bluebird = require('bluebird');
const Path = require('path');
const log = require('npmlog');
const StyledString = require('styled_string');
const find = require('lodash.find');

const Server = require('./server');
Expand Down Expand Up @@ -321,17 +320,6 @@ module.exports = class App extends EventEmitter {
});
}
});
this.fileWatcher.on('EMFILE', () => {
let view = this.view;
let text = [
'The file watcher received a EMFILE system error, which means that ',
'it has hit the maximum number of files that can be open at a time. ',
'Luckily, you can increase this limit as a workaround. See the directions below \n \n',
'Linux: http://stackoverflow.com/a/34645/5304\n',
'Mac OS: http://serverfault.com/a/15575/47234'
].join('');
view.setErrorPopupMessage(new StyledString(text + '\n ').foreground('megenta'));
});

return Bluebird.resolve().asCallback(cb);
}
Expand Down
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 def0c06

Please sign in to comment.