Merge pull request #276 from theredcoder/master

Support milliseconds for --delay option
This commit is contained in:
Remy Sharp
2014-01-23 11:44:52 -08:00
3 changed files with 54 additions and 2 deletions

View File

@@ -154,7 +154,15 @@ To add an extra throttle, or delay restarting, use the `--delay` command:
nodemon --delay 10 server.js
The delay figure is number of seconds to delay before restarting. So nodemon will only restart your app the given number of seconds after the *last* file change.
For more precision, milliseconds can be specified. Either as a float:
nodemon --delay 2.5 server.js
Or using the time specifier (ms):
nodemon --delay 2500ms server.js
The delay figure is number of seconds (or milliseconds, if specified) to delay before restarting. So nodemon will only restart your app the given number of seconds after the *last* file change.
## Controlling shutdown of your script

View File

@@ -160,7 +160,7 @@ function nodemonOption(options, arg, eatNext) {
}
else if (arg === '--delay' || arg === '-d') {
options.delay = parseInt(eatNext(), 10) * 1000;
options.delay = parseDelay(eatNext());
}
else if (arg === '--exec' || arg === '-x') {
@@ -219,3 +219,25 @@ function findAppScript() {
return null;
}
/**
* Given an argument (ie. from nodemonOption()), will parse and return the
* equivalent millisecond value or 0 if the argument cannot be parsed
*
* @param {String} argument value given to the --delay option
* @return {Number} millisecond equivalent of the argument
*/
function parseDelay(value) {
var millisPerSecond = 1000;
var millis = 0;
if (value.match(/^\d*ms$/)) {
// Explicitly parse for milliseconds when using ms time specifier
millis = parseInt(value, 10);
} else {
// Otherwise, parse for seconds, with or without time specifier, then convert
millis = parseFloat(value) * millisPerSecond;
}
return isNaN(millis) ? 0 : millis;
}

View File

@@ -218,4 +218,26 @@ describe('nodemon with CoffeeScript', function () {
assert(cmd.indexOf('--nodejs') !== -1, '--nodejs being used');
assert(cmd.indexOf('--debug-brk') !== -1, '--debug-brk being used');
});
});
describe('nodemon --delay argument', function () {
it('should support an integer value', function () {
var settings = cli.parse('node nodemon --delay 5');
assert(settings.delay === 5000, 'delay 5 seconds');
});
it('should support a float value', function () {
var settings = cli.parse('node nodemon --delay 1.2');
assert(settings.delay === 1200, 'delay 1.2 seconds');
});
it('should support a value with a time specifier for seconds (s)', function () {
var settings = cli.parse('node nodemon --delay 5s');
assert(settings.delay === 5000, 'delay 5 seconds');
});
it('should support a value with a time specifier for milliseconds (ms)', function () {
var settings = cli.parse('node nodemon --delay 1200ms');
assert(settings.delay === 1200, 'delay 1.2 seconds');
});
});