Skip to content

Commit

Permalink
refactor: move watch out in favour of chokidar
Browse files Browse the repository at this point in the history
Plus jscs clean ups
  • Loading branch information
remy committed Aug 31, 2015
1 parent 2e56d84 commit b0fd56f
Show file tree
Hide file tree
Showing 26 changed files with 272 additions and 465 deletions.
13 changes: 13 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"preset": "node-style-guide",
"requireCapitalizedComments": null,
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningCurlyBrace": true,
"beforeOpeningRoundBrace": true
},
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"excludeFiles": ["node_modules/**"],
"disallowSpacesInFunction": null
}
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
"node": true,
"quotmark": "single",
"undef": true,
"strict": false,
"unused": true
}
}

5 changes: 3 additions & 2 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
var parse = require('./parse');

/**
* Converts a string to command line args, in particular
* groups together quoted values
* groups together quoted values.
* This is a utility function to allow calling nodemon as a required
* library, but with the CLI args passed in (instead of an object).
*
* @param {String} string
* @return {Array}
Expand Down
22 changes: 4 additions & 18 deletions lib/cli/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
/*
nodemon is a utility for node, and replaces the use of the executable
Expand All @@ -15,9 +14,9 @@ nodemon can be run in a number of ways:
*/

var fs = require('fs'),
path = require('path'),
existsSync = fs.existsSync || path.existsSync;
var fs = require('fs');
var path = require('path');
var existsSync = fs.existsSync || path.existsSync;

module.exports = parse;

Expand Down Expand Up @@ -96,19 +95,6 @@ function parse(argv) {
}
}


// FIXME this was commented out on 2014-12-05 due to logic being moved
// to exec.js. I know I have git to revive the code if I need to, but
// I'm going to leave it here for the short term. Next passerby: feel free
// to remove.
// -------------
// allows the user to specify a script and for nodemon to throw an exception
// *instead* of echoing out the usage and ignoring the poor user altogether,
// just because the filename (or argument) specified wasn't found.
// if (!script && args.length) {
// script = args.pop();
// }

nodemonOptions.script = script;
nodemonOptions.args = args;

Expand Down Expand Up @@ -216,7 +202,7 @@ function nodemonOption(options, arg, eatNext) {
function findAppScript() {
// nodemon has been run alone, so try to read the package file
// or try to read the index.js file
if (existsSync('./index.js')) { // FIXME is ./ the right location?
if (existsSync('./index.js')) {
return { exec: null, script: 'index.js' };
}

Expand Down
29 changes: 16 additions & 13 deletions lib/config/checkWatchSupport.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
var utils = require('../utils'),
watchable = require('./watchable'),
fs = require('fs'),
exec = require('child_process').exec,
checkComplete = false;
var utils = require('../utils');
var watchable = require('./watchable');
var fs = require('fs');
var exec = require('child_process').exec;
var checkComplete = false;

module.exports = checkWatchSupport;

Expand All @@ -21,16 +20,14 @@ function checkWatchSupport(config, callback) {
}

var ready = function () {
if (checkComplete) {
//utils.bus.emit('config:update');
} else {
if (!checkComplete) {
checkComplete = true;
callback(config);
}
};

var alternativeCheck = function () {
watchable.check(function(success) {
watchable.check(function (success) {
// whether or not fs.watch actually works on this platform, tested and set
// later before starting
config.system.useWatch = success;
Expand All @@ -42,15 +39,21 @@ function checkWatchSupport(config, callback) {
// this is because it has a default ulimit of 256 - which is WAY LOW,
// and without asking the user to `unlimit -n <BIG-ASS-NUMBER>` it'll throw
// up all over your screen like this: http://d.pr/i/R6B8+
// even with higher ulimit -n Mac has another problem: https://github.com/joyent/node/issues/5463
// even with higher ulimit -n Mac has another problem:
// https://github.com/joyent/node/issues/5463
// This will be fixed in 0.12, before then we default to find
config.system.useFind = utils.isMac || utils.isLinux || !fs.watch;
var mtime = utils.isMac ? '-mtime -1s' : '-mmin -0.01';
if (config.system.useFind) {
exec('find -L /dev/null -type f ' + mtime + ' -print', function(error) {
exec('find -L /dev/null -type f ' + mtime + ' -print', function (error) {
if (error) {
if (!fs.watch) {
utils.log.error('The version of node you are using combined with the version of find being used does not support watching files. Upgrade to a newer version of node, or install a version of find that supports search by seconds.');
var notice = 'The version of node you are using combined with the ' +
'version of find being used does not support watching files. ' +
'Upgrade to a newer version of node, or install a version of ' +
'find that supports search by seconds.';

utils.log.error(notice);
process.exit(1);
} else {
config.system.useFind = false;
Expand Down
26 changes: 20 additions & 6 deletions lib/config/command.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
'use strict';

module.exports = command;

/**
* command constructs the executable command to run in a shell including the
* user script, the command arguments.
*
* @param {Object} settings Object as:
* { execOptions: {
* exec: String,
* [script: String],
* [scriptPosition: Number],
* [execArgs: Array<string>]
* }
* }
* @return {Object} an object with the node executable and the
* arguments to the command
*/
function command(settings) {
var options = settings.execOptions;
var executable = options.exec,
args = [];
var executable = options.exec;
var args = [];

// after "executable" go the exec args (like --debug, etc)
if (options.execArgs) {
Expand All @@ -19,11 +32,12 @@ function command(settings) {

// after the "executable" goes the user's script
if (options.script) {
args.splice((options.scriptPosition || 0) + options.execArgs.length, 0, options.script);
args.splice((options.scriptPosition || 0) +
options.execArgs.length, 0, options.script);
}

return {
executable: executable,
args: args
args: args,
};
}
6 changes: 3 additions & 3 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = {
py: 'python',
rb: 'ruby',
// more can be added here such as ls: lsc - but please ensure it's cross
// compatible with linux, mac and windows, or make the default.js dynamically
// append the `.cmd` for node based utilities
// compatible with linux, mac and windows, or make the default.js
// dynamically append the `.cmd` for node based utilities
},
ignore: ['.git', 'node_modules', 'bower_components', '.sass-cache'],
watch: ['*.*'],
Expand All @@ -16,6 +16,6 @@ module.exports = {
// 'stdout' refers to the default behaviour of a required nodemon's child,
// but also includes stderr. If this is false, data is still dispatched via
// nodemon.on('stdout/stderr')
stdout: true
stdout: true,

};
44 changes: 26 additions & 18 deletions lib/config/exec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var path = require('path'),
utils = require('../utils');
var path = require('path');
var utils = require('../utils');

module.exports = exec;

Expand Down Expand Up @@ -30,7 +29,8 @@ function execFromPackage() {
}

function replace(map, str) {
return str.replace(new RegExp('\{\{(' + Object.keys(map).join('|') + ')\}\}'), function (all, m) {
var re = new RegExp('{{(' + Object.keys(map).join('|') + ')}}');
return str.replace(re, function (all, m) {
return map[m] || all || '';
});
}
Expand All @@ -51,10 +51,10 @@ function exec(nodemonOptions, execMap) {
execMap = {};
}

// if there's no exec found yet, then try to read it from the local package.json
// this logic used to sit in the cli/parse, but actually the cli should be parsed
// first, then the user options (via nodemon.json) then finally default down to
// pot shots at the directory via package.json
// if there's no exec found yet, then try to read it from the local
// package.json this logic used to sit in the cli/parse, but actually the cli
// should be parsed first, then the user options (via nodemon.json) then
// finally default down to pot shots at the directory via package.json
if (!nodemonOptions.exec && !nodemonOptions.script) {
var found = execFromPackage();
if (found !== null) {
Expand All @@ -64,24 +64,26 @@ function exec(nodemonOptions, execMap) {
if (!nodemonOptions.script) {
nodemonOptions.script = found.script;
}
if (Array.isArray(nodemonOptions.args) && nodemonOptions.scriptPosition === null) {
if (Array.isArray(nodemonOptions.args) &&
nodemonOptions.scriptPosition === null) {
nodemonOptions.scriptPosition = nodemonOptions.args.length;
}
}
}

var options = utils.clone(nodemonOptions || {}),
script = path.basename(options.script || ''),
scriptExt = path.extname(script).slice(1),
extension = options.ext || scriptExt || 'js',
execDefined = !!options.exec;
var options = utils.clone(nodemonOptions || {});
var script = path.basename(options.script || '');
var scriptExt = path.extname(script).slice(1);
var extension = options.ext || scriptExt || 'js';
var execDefined = !!options.exec;

// strip any leading periods int he extension
if (extension.indexOf('.') === 0) {
extension = extension.slice(1);
}

// allows the user to simplify cli usage: https://github.com/remy/nodemon/issues/195
// allows the user to simplify cli usage:
// https://github.com/remy/nodemon/issues/195
// but always give preference to the user defined argument
if (!options.exec && execMap[scriptExt] !== undefined) {
options.exec = execMap[scriptExt];
Expand All @@ -99,9 +101,14 @@ function exec(nodemonOptions, execMap) {
options.exec = 'node';
} else {
// allow variable substitution for {{filename}} and {{pwd}}
var substitution = replace.bind(null, { filename: options.script, pwd: process.cwd() });
var substitution = replace.bind(null, {
filename: options.script,
pwd: process.cwd(),
});

var newExec = substitution(options.exec);
if (newExec !== options.exec && options.exec.indexOf('{{filename}}') !== -1) {
if (newExec !== options.exec &&
options.exec.indexOf('{{filename}}') !== -1) {
options.script = null;
}
options.exec = newExec;
Expand All @@ -119,7 +126,8 @@ function exec(nodemonOptions, execMap) {
}

// note: indexOf('coffee') handles both .coffee and .litcoffee
if (!execDefined && options.exec === 'node' && scriptExt.indexOf('coffee') !== -1) {
if (!execDefined && options.exec === 'node' &&
scriptExt.indexOf('coffee') !== -1) {
options.exec = 'coffee';

// we need to get execArgs set before the script
Expand Down
29 changes: 13 additions & 16 deletions lib/config/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict';
/**
* Manages the internal config of nodemon, checking for the state of support
* with fs.watch, how nodemon can watch files (using find or fs methods).
*
* This is *not* the user's config.
*/
var load = require('./load'),
rules = require('../rules'),
utils = require('../utils'),
command = require('./command'),
path = require('path'),
rulesToMonitor = require('../monitor/match').rulesToMonitor,
bus = utils.bus,
checkWatchSupport = require('./checkWatchSupport');
var load = require('./load');
var rules = require('../rules');
var utils = require('../utils');
var command = require('./command');
var rulesToMonitor = require('../monitor/match').rulesToMonitor;
var bus = utils.bus;
var checkWatchSupport = require('./checkWatchSupport');

function reset() {
rules.reset();
Expand All @@ -34,7 +32,7 @@ var config = {
required: false,
dirs: [],
timeout: 1000,
options: {}
options: {},
};

/**
Expand All @@ -57,14 +55,14 @@ config.load = function (settings, ready) {
options.watch.push('*.*');
}

if (options.watch_interval) {
config.watch_interval = options.watch_interval;
}
config.watchInterval = options.watchInterval ||
options.watch_interval || // jshint ignore:line
null;

var cmd = command(config.options);
config.command = {
raw: cmd,
string: utils.stringify(cmd.executable, cmd.args)
string: utils.stringify(cmd.executable, cmd.args),
};

// now run automatic checks on system adding to the config object
Expand All @@ -87,5 +85,4 @@ config.load = function (settings, ready) {

config.reset = reset;

module.exports = config;

module.exports = config;
Loading

0 comments on commit b0fd56f

Please sign in to comment.