From 34defa0a445c88deb3c5c4272cd1655f0dcb004b Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Mon, 29 May 2017 18:38:48 -0700 Subject: [PATCH] Update tooling (eslint + prettier) --- .eslintrc | 17 +++ .jshintrc | 76 ----------- package.json | 8 +- src/cli.js | 33 +++-- src/common.js | 22 ++-- src/node_watcher.js | 188 +++++++++++++++------------- src/poll_watcher.js | 18 +-- src/utils/recrawl-warning-dedupe.js | 10 +- src/watchman_watcher.js | 52 ++++---- 9 files changed, 207 insertions(+), 217 deletions(-) create mode 100644 .eslintrc delete mode 100644 .jshintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..fde5ec2 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,17 @@ +{ + "extends": "eslint:recommended", + "env": { + "browser": true, + "commonjs": true, + "node": true, + "es6": true + }, + "parserOptions": { + "ecmaVersion": 6 + }, + "rules": { + "no-console": "off", + "strict": ["error", "global"], + "curly": "warn" + } +} \ No newline at end of file diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index cfcdb89..0000000 --- a/.jshintrc +++ /dev/null @@ -1,76 +0,0 @@ -{ - "-W093": true, - "asi": false, - "bitwise": true, - "boss": false, - "browser": false, - "couch": false, - "curly": true, - "debug": false, - "devel": true, - "dojo": false, - "eqeqeq": true, - "eqnull": true, - "esnext": true, - "evil": false, - "expr": true, - "forin": false, - "freeze": true, - "funcscope": true, - "gcl": false, - "globalstrict": true, - "immed": false, - "indent": 2, - "iterator": false, - "jquery": false, - "lastsemic": false, - "latedef": false, - "laxbreak": true, - "laxcomma": false, - "loopfunc": false, - "maxcomplexity": false, - "maxdepth": false, - "maxerr": 50, - "maxlen": 80, - "maxparams": false, - "maxstatements": false, - "mootools": false, - "moz": false, - "multistr": false, - "newcap": true, - "noarg": true, - "node": true, - "noempty": true, - "nonbsp": true, - "nonew": true, - "nonstandard": false, - "notypeof": false, - "noyield": false, - "phantom": false, - "plusplus": false, - "predef": [ - "describe", - "beforeEach", - "it", - "jest", - "pit", - "expect" - ], - "proto": true, - "prototypejs": false, - "quotmark": true, - "rhino": false, - "scripturl": false, - "shadow": false, - "smarttabs": false, - "strict": true, - "sub": false, - "supernew": false, - "trailing": true, - "undef": true, - "unused": true, - "validthis": false, - "worker": false, - "wsh": false, - "yui": false -} diff --git a/package.json b/package.json index 9c81620..6e8c08f 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,9 @@ ], "scripts": { "prepublish": "jshint --config=.jshintrc src/ index.js && mocha --bail", - "test": "jshint --config=.jshintrc src/ index.js && mocha --bail test/test.js && mocha --bail test/utils-test.js", - "test:debug": "mocha debug --bail" + "test": "npm run format && eslint src/ index.js && mocha --bail test/test.js && mocha --bail test/utils-test.js", + "test:debug": "mocha debug --bail", + "format": "prettier --trailing-comma es5 --single-quote --write index.js 'src/**/*.js'" }, "bin": "./src/cli.js", "keywords": [ @@ -37,8 +38,9 @@ "watch": "~0.10.0" }, "devDependencies": { - "jshint": "^2.5.10", + "eslint": "^3.19.0", "mocha": "~1.17.1", + "prettier": "^1.3.1", "rimraf": "~2.2.6", "tmp": "0.0.27" }, diff --git a/src/cli.js b/src/cli.js index f3749ed..f06e9f9 100755 --- a/src/cli.js +++ b/src/cli.js @@ -5,9 +5,10 @@ var sane = require('../'); var argv = require('minimist')(process.argv.slice(2)); var execshell = require('exec-sh'); -if(argv._.length === 0) { - var msg = 'Usage: sane [...directory] [--glob=] ' + - '[--poll] [--watchman] [--dot] [--wait=]'; +if (argv._.length === 0) { + var msg = + 'Usage: sane [...directory] [--glob=] ' + + '[--poll] [--watchman] [--dot] [--wait=]'; console.error(msg); process.exit(); } @@ -21,27 +22,37 @@ var glob = argv.glob || argv.g; var poll = argv.poll || argv.p; var watchman = argv.watchman || argv.w; -if (dot) { opts.dot = true; } -if (glob) { opts.glob = glob; } -if (poll) { opts.poll = true; } -if (watchman) { opts.watchman = true; } +if (dot) { + opts.dot = true; +} +if (glob) { + opts.glob = glob; +} +if (poll) { + opts.poll = true; +} +if (watchman) { + opts.watchman = true; +} var wait = false; var watcher = sane(dir, opts); -watcher.on('ready', function () { +watcher.on('ready', function() { console.log('Watching: ', dir + '/' + (opts.glob || '')); execshell(command); }); -watcher.on('change', function (filepath) { - if (wait) { return; } +watcher.on('change', function(filepath) { + if (wait) { + return; + } console.log('Change detected in:', filepath); execshell(command); if (waitTime > 0) { wait = true; - setTimeout(function () { + setTimeout(function() { wait = false; }, waitTime * 1000); } diff --git a/src/common.js b/src/common.js index b23795f..1c8cc0c 100644 --- a/src/common.js +++ b/src/common.js @@ -31,11 +31,13 @@ exports.assignOptions = function(watcher, opts) { if (!Array.isArray(watcher.globs)) { watcher.globs = [watcher.globs]; } - watcher.hasIgnore = Boolean(opts.ignored) && - !(Array.isArray(opts) && opts.length > 0); - watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : function () { - return false; - }; + watcher.hasIgnore = + Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0); + watcher.doIgnore = opts.ignored + ? anymatch(opts.ignored) + : function() { + return false; + }; return opts; }; @@ -52,16 +54,18 @@ exports.isFileIncluded = function(globs, dot, doIgnore, relativePath) { var matched; if (globs.length) { for (var i = 0; i < globs.length; i++) { - if (minimatch(relativePath, globs[i], {dot: dot}) && - !doIgnore(relativePath)) { + if ( + minimatch(relativePath, globs[i], { dot: dot }) && + !doIgnore(relativePath) + ) { matched = true; break; } } } else { // Make sure we honor the dot option if even we're not using globs. - matched = (dot || minimatch(relativePath, '**/*')) && - !doIgnore(relativePath); + matched = + (dot || minimatch(relativePath, '**/*')) && !doIgnore(relativePath); } return matched; }; diff --git a/src/node_watcher.js b/src/node_watcher.js index 9921522..b379add 100644 --- a/src/node_watcher.js +++ b/src/node_watcher.js @@ -34,7 +34,7 @@ module.exports = NodeWatcher; */ function NodeWatcher(dir, opts) { - opts = common.assignOptions(this, opts); + common.assignOptions(this, opts); this.watched = Object.create(null); this.changeTimers = Object.create(null); @@ -74,11 +74,9 @@ NodeWatcher.prototype.__proto__ = EventEmitter.prototype; NodeWatcher.prototype.register = function(filepath) { var relativePath = path.relative(this.root, filepath); - if (!common.isFileIncluded( - this.globs, - this.dot, - this.doIgnore, - relativePath)) { + if ( + !common.isFileIncluded(this.globs, this.dot, this.doIgnore, relativePath) + ) { return false; } @@ -131,8 +129,10 @@ NodeWatcher.prototype.unregisterDir = function(dirpath) { NodeWatcher.prototype.registered = function(fullpath) { var dir = path.dirname(fullpath); - return this.dirRegistery[fullpath] || - this.dirRegistery[dir] && this.dirRegistery[dir][path.basename(fullpath)]; + return ( + this.dirRegistery[fullpath] || + (this.dirRegistery[dir] && this.dirRegistery[dir][path.basename(fullpath)]) + ); }; /** @@ -213,32 +213,37 @@ NodeWatcher.prototype.detectChangedFile = function(dir, event, callback) { } var found = false; - var closest = {mtime: 0}; + var closest = { mtime: 0 }; var c = 0; Object.keys(this.dirRegistery[dir]).forEach(function(file, i, arr) { - fs.lstat(path.join(dir, file), function(error, stat) { - if (found) { - return; - } + fs.lstat( + path.join(dir, file), + function(error, stat) { + if (found) { + return; + } - if (error) { - if (error.code === 'ENOENT' || - (platform === 'win32' && error.code === 'EPERM')) { - found = true; - callback(file); + if (error) { + if ( + error.code === 'ENOENT' || + (platform === 'win32' && error.code === 'EPERM') + ) { + found = true; + callback(file); + } else { + this.emit('error', error); + } } else { - this.emit('error', error); - } - } else { - if (stat.mtime > closest.mtime) { - stat.file = file; - closest = stat; - } - if (arr.length === ++c) { - callback(closest.file); + if (stat.mtime > closest.mtime) { + stat.file = file; + closest = stat; + } + if (arr.length === ++c) { + callback(closest.file); + } } - } - }.bind(this)); + }.bind(this) + ); }, this); }; @@ -253,11 +258,15 @@ NodeWatcher.prototype.detectChangedFile = function(dir, event, callback) { NodeWatcher.prototype.normalizeChange = function(dir, event, file) { if (!file) { - this.detectChangedFile(dir, event, function(actualFile) { - if (actualFile) { - this.processChange(dir, event, actualFile); - } - }.bind(this)); + this.detectChangedFile( + dir, + event, + function(actualFile) { + if (actualFile) { + this.processChange(dir, event, actualFile); + } + }.bind(this) + ); } else { this.processChange(dir, event, path.normalize(file)); } @@ -275,39 +284,45 @@ NodeWatcher.prototype.normalizeChange = function(dir, event, file) { NodeWatcher.prototype.processChange = function(dir, event, file) { var fullPath = path.join(dir, file); var relativePath = path.join(path.relative(this.root, dir), file); - fs.lstat(fullPath, function(error, stat) { - if (error && error.code !== 'ENOENT') { - this.emit('error', error); - } else if (!error && stat.isDirectory()) { - // win32 emits usless change events on dirs. - if (event !== 'change') { - this.watchdir(fullPath); - if (common.isFileIncluded( - this.globs, - this.dot, - this.doIgnore, - relativePath)) { - this.emitEvent(ADD_EVENT, relativePath, stat); - } - } - } else { - var registered = this.registered(fullPath); - if (error && error.code === 'ENOENT') { - this.unregister(fullPath); - this.stopWatching(fullPath); - this.unregisterDir(fullPath); - if (registered) { - this.emitEvent(DELETE_EVENT, relativePath); + fs.lstat( + fullPath, + function(error, stat) { + if (error && error.code !== 'ENOENT') { + this.emit('error', error); + } else if (!error && stat.isDirectory()) { + // win32 emits usless change events on dirs. + if (event !== 'change') { + this.watchdir(fullPath); + if ( + common.isFileIncluded( + this.globs, + this.dot, + this.doIgnore, + relativePath + ) + ) { + this.emitEvent(ADD_EVENT, relativePath, stat); + } } - } else if (registered) { - this.emitEvent(CHANGE_EVENT, relativePath, stat); } else { - if (this.register(fullPath)) { - this.emitEvent(ADD_EVENT, relativePath, stat); + var registered = this.registered(fullPath); + if (error && error.code === 'ENOENT') { + this.unregister(fullPath); + this.stopWatching(fullPath); + this.unregisterDir(fullPath); + if (registered) { + this.emitEvent(DELETE_EVENT, relativePath); + } + } else if (registered) { + this.emitEvent(CHANGE_EVENT, relativePath, stat); + } else { + if (this.register(fullPath)) { + this.emitEvent(ADD_EVENT, relativePath, stat); + } } } - } - }.bind(this)); + }.bind(this) + ); }; /** @@ -326,33 +341,36 @@ NodeWatcher.prototype.emitEvent = function(type, file, stat) { return; } clearTimeout(this.changeTimers[key]); - this.changeTimers[key] = setTimeout(function() { - delete this.changeTimers[key]; - if (type === ADD_EVENT && stat.isDirectory()) { - // Recursively emit add events and watch for sub-files/folders - recReaddir( - path.resolve(this.root, file), - function emitAddDir (dir, stats) { - this.watchdir(dir); - this.rawEmitEvent(ADD_EVENT, path.relative(this.root, dir), stats); - }.bind(this), - function emitAddFile (file, stats) { - this.register(file); - this.rawEmitEvent(ADD_EVENT, path.relative(this.root, file), stats); - }.bind(this), - function endCallback () {}, - this.ignored - ); - } else { - this.rawEmitEvent(type, file, stat); - } - }.bind(this), DEFAULT_DELAY); + this.changeTimers[key] = setTimeout( + function() { + delete this.changeTimers[key]; + if (type === ADD_EVENT && stat.isDirectory()) { + // Recursively emit add events and watch for sub-files/folders + recReaddir( + path.resolve(this.root, file), + function emitAddDir(dir, stats) { + this.watchdir(dir); + this.rawEmitEvent(ADD_EVENT, path.relative(this.root, dir), stats); + }.bind(this), + function emitAddFile(file, stats) { + this.register(file); + this.rawEmitEvent(ADD_EVENT, path.relative(this.root, file), stats); + }.bind(this), + function endCallback() {}, + this.ignored + ); + } else { + this.rawEmitEvent(type, file, stat); + } + }.bind(this), + DEFAULT_DELAY + ); }; /** * Actually emit the events */ -NodeWatcher.prototype.rawEmitEvent = function (type, file, stat) { +NodeWatcher.prototype.rawEmitEvent = function(type, file, stat) { this.emit(type, file, this.root, stat); this.emit(ALL_EVENT, type, file, this.root, stat); }; diff --git a/src/poll_watcher.js b/src/poll_watcher.js index cdd08dc..ff4d467 100644 --- a/src/poll_watcher.js +++ b/src/poll_watcher.js @@ -39,8 +39,9 @@ function PollWatcher(dir, opts) { watch.createMonitor( this.root, - { interval: opts.interval || DEFAULT_DELAY, - filter: this.filter.bind(this) + { + interval: opts.interval || DEFAULT_DELAY, + filter: this.filter.bind(this), }, this.init.bind(this) ); @@ -57,11 +58,14 @@ PollWatcher.prototype.__proto__ = EventEmitter.prototype; */ PollWatcher.prototype.filter = function(filepath, stat) { - return stat.isDirectory() || common.isFileIncluded( - this.globs, - this.dot, - this.doIgnore, - path.relative(this.root, filepath) + return ( + stat.isDirectory() || + common.isFileIncluded( + this.globs, + this.dot, + this.doIgnore, + path.relative(this.root, filepath) + ) ); }; diff --git a/src/utils/recrawl-warning-dedupe.js b/src/utils/recrawl-warning-dedupe.js index 8f750aa..a016561 100644 --- a/src/utils/recrawl-warning-dedupe.js +++ b/src/utils/recrawl-warning-dedupe.js @@ -22,9 +22,13 @@ RecrawlWarning.findByRoot = function(root) { }; RecrawlWarning.isRecrawlWarningDupe = function(warningMessage) { - if (typeof warningMessage !== 'string') { return false; } + if (typeof warningMessage !== 'string') { + return false; + } var match = warningMessage.match(REG); - if (!match) { return false; } + if (!match) { + return false; + } var count = Number(match[1]); var root = match[2]; @@ -33,7 +37,7 @@ RecrawlWarning.isRecrawlWarningDupe = function(warningMessage) { if (warning) { // only keep the highest count, assume count to either stay the same or // increase. - if (warning.count >= count ) { + if (warning.count >= count) { return true; } else { // update the existing warning to the latest (highest) count diff --git a/src/watchman_watcher.js b/src/watchman_watcher.js index ce98d07..75842bf 100644 --- a/src/watchman_watcher.js +++ b/src/watchman_watcher.js @@ -24,7 +24,6 @@ var SUB_NAME = 'sane-sub'; module.exports = WatchmanWatcher; - /** * Watches `dir`. * @@ -35,7 +34,7 @@ module.exports = WatchmanWatcher; */ function WatchmanWatcher(dir, opts) { - opts = common.assignOptions(this, opts); + common.assignOptions(this, opts); this.root = path.resolve(dir); this.init(); } @@ -81,9 +80,7 @@ WatchmanWatcher.prototype.init = function() { self.capabilities = resp.capabilities; if (self.capabilities.relative_root) { - self.client.command( - ['watch-project', getWatchRoot()], onWatchProject - ); + self.client.command(['watch-project', getWatchRoot()], onWatchProject); } else { self.client.command(['watch', getWatchRoot()], onWatch); } @@ -98,13 +95,12 @@ WatchmanWatcher.prototype.init = function() { self.watchProjectInfo = { root: resp.watch, - relativePath: resp.relative_path ? resp.relative_path : '' + relativePath: resp.relative_path ? resp.relative_path : '', }; self.client.command(['clock', getWatchRoot()], onClock); } - function onWatch(error, resp) { if (handleError(self, error)) { return; @@ -124,7 +120,7 @@ WatchmanWatcher.prototype.init = function() { var options = { fields: ['name', 'exists', 'new'], - since: resp.clock + since: resp.clock, }; // If the server has the wildmatch capability available it supports @@ -136,16 +132,26 @@ WatchmanWatcher.prototype.init = function() { if (self.globs.length === 0) { if (!self.dot) { // Make sure we honor the dot option if even we're not using globs. - options.expression = ['match', '**', 'wholename', { - includedotfiles: false - }]; + options.expression = [ + 'match', + '**', + 'wholename', + { + includedotfiles: false, + }, + ]; } } else { options.expression = ['anyof']; for (var i in self.globs) { - options.expression.push(['match', self.globs[i], 'wholename', { - includedotfiles: self.dot - }]); + options.expression.push([ + 'match', + self.globs[i], + 'wholename', + { + includedotfiles: self.dot, + }, + ]); } } } @@ -170,10 +176,12 @@ WatchmanWatcher.prototype.init = function() { self.emit('ready'); } - self.client.capabilityCheck({ - optional:['wildmatch', 'relative_root'] + self.client.capabilityCheck( + { + optional: ['wildmatch', 'relative_root'], }, - onCapability); + onCapability + ); }; /** @@ -214,12 +222,10 @@ WatchmanWatcher.prototype.handleFileChange = function(changeDescriptor) { relativePath = changeDescriptor.name; } - if (!(self.capabilities.wildmatch && !this.hasIgnore) && - !common.isFileIncluded( - this.globs, - this.dot, - this.doIgnore, - relativePath)) { + if ( + !(self.capabilities.wildmatch && !this.hasIgnore) && + !common.isFileIncluded(this.globs, this.dot, this.doIgnore, relativePath) + ) { return; }