Skip to content

Commit

Permalink
Merge pull request #276 from theredcoder/master
Browse files Browse the repository at this point in the history
Support milliseconds for --delay option
  • Loading branch information
remy committed Jan 23, 2014
2 parents 954d447 + d96f117 commit 66d2bda
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -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

Expand Down
24 changes: 23 additions & 1 deletion lib/cli/parse.js
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
}

22 changes: 22 additions & 0 deletions test/cli/parse.test.js
Expand Up @@ -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');
});
});

0 comments on commit 66d2bda

Please sign in to comment.