From db196733b7e045e77719ad80ec8345715522671b Mon Sep 17 00:00:00 2001 From: Chris Khoo Date: Sun, 5 Jul 2015 15:40:36 -0700 Subject: [PATCH 1/2] add option --no-startup to not run command on startup --- doc/cli/help.txt | 1 + lib/cli/parse.js | 4 ++++ lib/config/defaults.js | 3 ++- lib/monitor/run.js | 10 ++++++---- test/cli/parse.test.js | 8 +++++--- test/mocha.opts | 2 +- test/monitor/run.test.js | 18 ++++++++++++++++++ 7 files changed, 37 insertions(+), 9 deletions(-) diff --git a/doc/cli/help.txt b/doc/cli/help.txt index f848e977..19600ea5 100644 --- a/doc/cli/help.txt +++ b/doc/cli/help.txt @@ -10,6 +10,7 @@ -q, --quiet .............. minimise nodemon messages to start/stop only. -V, --verbose ............ show detail on what is causing restarts. -I, --no-stdin ........... don't try to read from stdin. + -S, --no-startup ......... disable running script on startup. -d, --delay n ............ debounce restart for "n" seconds. -L, --legacy-watch ....... Forces node to use the most compatible version for watching file changes. diff --git a/lib/cli/parse.js b/lib/cli/parse.js index 4c24ce99..07436884 100644 --- a/lib/cli/parse.js +++ b/lib/cli/parse.js @@ -191,6 +191,10 @@ function nodemonOption(options, arg, eatNext) { options.stdin = false; } + else if (arg === '--no-startup' || arg === '-S') { + options.runOnStartup = false; + } + else if (arg === '--ext' || arg === '-e') { options.ext = eatNext(); } diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 4c9ec801..5a91ce49 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -11,10 +11,11 @@ module.exports = { ignore: ['.git', 'node_modules', 'bower_components', '.sass-cache'], watch: ['*.*'], stdin: true, + runOnStartup: true, verbose: false, // '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 -}; \ No newline at end of file +}; diff --git a/lib/monitor/run.js b/lib/monitor/run.js index 9798e5e5..34ae9ed9 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -24,7 +24,10 @@ exec('ps', function(error) { function run(options) { var cmd = config.command.raw; - utils.log.status('starting `' + config.command.string + '`'); + var runCmd = config.lastStarted !== 0 || options.runOnStartup; + if (runCmd) { + utils.log.status('starting `' + config.command.string + '`'); + } /*jshint validthis:true*/ restart = run.bind(this, options); @@ -54,8 +57,7 @@ function run(options) { executable = executable.replace(/\\((\w+\s+)+\w+)(?=([\\\.]))(?=([^"]*"[^"]*")*[^"]*$)/g, '\\"$1"'); } - var args = utils.stringify(executable, cmd.args); - + var args = runCmd ? utils.stringify(executable, cmd.args) : ':'; var spawnArgs = [sh, [shFlag, args]]; if (utils.version.major === 0 && utils.version.minor < 8) { @@ -139,7 +141,7 @@ function run(options) { // restart restart(); } else if (code === 0) { // clean exit - wait until file change to restart - utils.log.status('clean exit - waiting for changes before restart'); + if (runCmd) utils.log.status('clean exit - waiting for changes before restart'); child = null; } } else { diff --git a/test/cli/parse.test.js b/test/cli/parse.test.js index cbf24aa3..02a35146 100644 --- a/test/cli/parse.test.js +++ b/test/cli/parse.test.js @@ -237,7 +237,7 @@ describe('nodemon argument parser', function () { }); it('should support short versions of flags', function () { - var settings = cli.parse('node nodemon -v -x java -I -V -q -w fixtures -i fixtures -d 5 -L -e jade'); + var settings = cli.parse('node nodemon -v -x java -I -V -q -w fixtures -i fixtures -d 5 -L -S -e jade'); assert(settings.version, 'version'); assert(settings.verbose, 'verbose'); assert(settings.exec === 'java', 'exec'); @@ -247,12 +247,13 @@ describe('nodemon argument parser', function () { assert(settings.ignore[0] === 'fixtures', 'ignore'); assert(settings.delay === 5000, 'delay 5 seconds'); assert(settings.legacyWatch, 'legacy watch method'); + assert(settings.runOnStartup === false, 'run on startup'); assert(settings.ext === 'jade', 'extension is jade'); }); it('should support long versions of flags', function () { - var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --ext jade'); + var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --no-startup --ext jade'); assert(settings.version, 'version'); assert(settings.verbose, 'verbose'); assert(settings.exec === 'java', 'exec'); @@ -263,6 +264,7 @@ describe('nodemon argument parser', function () { assert(settings.ignore[0] === 'fixtures', 'ignore'); assert(settings.delay === 5000, 'delay 5 seconds'); assert(settings.legacyWatch, 'legacy watch method'); + assert(settings.runOnStartup === false, 'run on startup'); assert(settings.ext === 'jade', 'extension is jade'); }); }); @@ -335,4 +337,4 @@ describe('nodemon --delay argument', function () { var settings = cli.parse('node nodemon --delay 1200ms'); assert(settings.delay === 1200, 'delay 1.2 seconds'); }); -}); \ No newline at end of file +}); diff --git a/test/mocha.opts b/test/mocha.opts index 7c7a9d7a..3fbc2697 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,2 +1,2 @@ --reporter spec ---ui bdd \ No newline at end of file +--ui bdd diff --git a/test/monitor/run.test.js b/test/monitor/run.test.js index b724d191..9864a63a 100644 --- a/test/monitor/run.test.js +++ b/test/monitor/run.test.js @@ -106,6 +106,24 @@ describe('when nodemon runs (2)', function () { }); }); + it('should not run command if runOnStartup is false', function(done) { + fs.writeFileSync(tmp, 'console.log("testing 1 2 3")'); + + nodemon({ + script: tmp, + runOnStartup: false, + stdout: false + }).on('stdout', function() { + assert(false, 'there should not be any stdout'); + }).on('stderr', function() { + assert(false, 'there should not be any stderr'); + }).on('crash', function () { + assert(false, 'detected crashed state'); + }).once('exit', function () { + done(); + }); + }); + // it('should kill child on SIGINT', function (done) { // fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)'); From 50f5994ee620cfffdcf0d4cf17d27a219994d675 Mon Sep 17 00:00:00 2001 From: Chris Khoo Date: Mon, 20 Jul 2015 17:39:49 -0700 Subject: [PATCH 2/2] change --no-startup to --on-change-only --- doc/cli/help.txt | 2 +- lib/cli/parse.js | 4 ++-- lib/config/defaults.js | 2 +- lib/monitor/run.js | 2 +- test/cli/parse.test.js | 8 ++++---- test/monitor/run.test.js | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/cli/help.txt b/doc/cli/help.txt index 19600ea5..e358c4e0 100644 --- a/doc/cli/help.txt +++ b/doc/cli/help.txt @@ -10,7 +10,7 @@ -q, --quiet .............. minimise nodemon messages to start/stop only. -V, --verbose ............ show detail on what is causing restarts. -I, --no-stdin ........... don't try to read from stdin. - -S, --no-startup ......... disable running script on startup. + -C, --on-change-only ..... execute script on change only, not startup -d, --delay n ............ debounce restart for "n" seconds. -L, --legacy-watch ....... Forces node to use the most compatible version for watching file changes. diff --git a/lib/cli/parse.js b/lib/cli/parse.js index 07436884..c6b699df 100644 --- a/lib/cli/parse.js +++ b/lib/cli/parse.js @@ -191,8 +191,8 @@ function nodemonOption(options, arg, eatNext) { options.stdin = false; } - else if (arg === '--no-startup' || arg === '-S') { - options.runOnStartup = false; + else if (arg === '--on-change-only' || arg === '-C') { + options.runOnChangeOnly = true; } else if (arg === '--ext' || arg === '-e') { diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 5a91ce49..95d4e91a 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -11,7 +11,7 @@ module.exports = { ignore: ['.git', 'node_modules', 'bower_components', '.sass-cache'], watch: ['*.*'], stdin: true, - runOnStartup: true, + runOnChangeOnly: false, verbose: false, // '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 diff --git a/lib/monitor/run.js b/lib/monitor/run.js index 34ae9ed9..ad5dc52c 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -24,7 +24,7 @@ exec('ps', function(error) { function run(options) { var cmd = config.command.raw; - var runCmd = config.lastStarted !== 0 || options.runOnStartup; + var runCmd = !options.runOnChangeOnly || config.lastStarted !== 0; if (runCmd) { utils.log.status('starting `' + config.command.string + '`'); } diff --git a/test/cli/parse.test.js b/test/cli/parse.test.js index 02a35146..58e1cf21 100644 --- a/test/cli/parse.test.js +++ b/test/cli/parse.test.js @@ -237,7 +237,7 @@ describe('nodemon argument parser', function () { }); it('should support short versions of flags', function () { - var settings = cli.parse('node nodemon -v -x java -I -V -q -w fixtures -i fixtures -d 5 -L -S -e jade'); + var settings = cli.parse('node nodemon -v -x java -I -V -q -w fixtures -i fixtures -d 5 -L -C -e jade'); assert(settings.version, 'version'); assert(settings.verbose, 'verbose'); assert(settings.exec === 'java', 'exec'); @@ -247,13 +247,13 @@ describe('nodemon argument parser', function () { assert(settings.ignore[0] === 'fixtures', 'ignore'); assert(settings.delay === 5000, 'delay 5 seconds'); assert(settings.legacyWatch, 'legacy watch method'); - assert(settings.runOnStartup === false, 'run on startup'); + assert(settings.runOnChangeOnly, 'run on change only'); assert(settings.ext === 'jade', 'extension is jade'); }); it('should support long versions of flags', function () { - var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --no-startup --ext jade'); + var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --on-change-only --ext jade'); assert(settings.version, 'version'); assert(settings.verbose, 'verbose'); assert(settings.exec === 'java', 'exec'); @@ -264,7 +264,7 @@ describe('nodemon argument parser', function () { assert(settings.ignore[0] === 'fixtures', 'ignore'); assert(settings.delay === 5000, 'delay 5 seconds'); assert(settings.legacyWatch, 'legacy watch method'); - assert(settings.runOnStartup === false, 'run on startup'); + assert(settings.runOnChangeOnly, 'run on change only'); assert(settings.ext === 'jade', 'extension is jade'); }); }); diff --git a/test/monitor/run.test.js b/test/monitor/run.test.js index 9864a63a..c5fb9543 100644 --- a/test/monitor/run.test.js +++ b/test/monitor/run.test.js @@ -106,12 +106,12 @@ describe('when nodemon runs (2)', function () { }); }); - it('should not run command if runOnStartup is false', function(done) { + it('should not run command on startup if runOnChangeOnly is true', function(done) { fs.writeFileSync(tmp, 'console.log("testing 1 2 3")'); nodemon({ script: tmp, - runOnStartup: false, + runOnChangeOnly: true, stdout: false }).on('stdout', function() { assert(false, 'there should not be any stdout');