Skip to content

Commit

Permalink
Merge pull request #578 from khoomeister/ck-disable-startup
Browse files Browse the repository at this point in the history
add option --no-startup to not run command on startup
  • Loading branch information
remy committed Jul 27, 2015
2 parents cfd5c8e + 50f5994 commit e177dd7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/cli/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-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.
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ function nodemonOption(options, arg, eatNext) {
options.stdin = false;
}

else if (arg === '--on-change-only' || arg === '-C') {
options.runOnChangeOnly = true;
}

else if (arg === '--ext' || arg === '-e') {
options.ext = eatNext();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ module.exports = {
ignore: ['.git', 'node_modules', 'bower_components', '.sass-cache'],
watch: ['*.*'],
stdin: 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
// nodemon.on('stdout/stderr')
stdout: true

};
};
10 changes: 6 additions & 4 deletions lib/monitor/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = !options.runOnChangeOnly || config.lastStarted !== 0;
if (runCmd) {
utils.log.status('starting `' + config.command.string + '`');
}

/*jshint validthis:true*/
restart = run.bind(this, options);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions test/cli/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 -C -e jade');
assert(settings.version, 'version');
assert(settings.verbose, 'verbose');
assert(settings.exec === 'java', 'exec');
Expand All @@ -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.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 --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');
Expand All @@ -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.runOnChangeOnly, 'run on change only');
assert(settings.ext === 'jade', 'extension is jade');
});
});
Expand Down Expand Up @@ -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');
});
});
});
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--reporter spec
--ui bdd
--ui bdd
18 changes: 18 additions & 0 deletions test/monitor/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ describe('when nodemon runs (2)', function () {
});
});

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,
runOnChangeOnly: true,
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)');

Expand Down

0 comments on commit e177dd7

Please sign in to comment.