Skip to content

Commit

Permalink
If no script, default is null, not -1. Fixes remy#265
Browse files Browse the repository at this point in the history
Includes test and exposing the command run for debugging.
  • Loading branch information
remy committed Jan 21, 2014
1 parent 43fa35a commit 2a2f126
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/cli/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function parse(argv) {

var args = argv.slice(2),
script = null,
nodemonOptions = { scriptPosition: -1 };
nodemonOptions = { scriptPosition: null };

var nodemonOpt = nodemonOption.bind(null, nodemonOptions),
lookForArgs = true;
Expand Down
28 changes: 28 additions & 0 deletions lib/config/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = command;

function command(options) {
var executable = options.execOptions.exec,
args = [];

// after "executable" go the exec args (like --debug, etc)
if (options.execOptions.execArgs) {
[].push.apply(args, options.execOptions.execArgs);
}

// then goes the user's script arguments
if (options.args) {
[].push.apply(args, options.args);
}

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

return {
executable: executable,
args: args
};
}
7 changes: 7 additions & 0 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var load = require('./load'),
rules = require('../rules'),
utils = require('../utils'),
command = require('./command'),
rulesToMonitor = require('../monitor/match').rulesToMonitor,
bus = utils.bus,
checkWatchSupport = require('./checkWatchSupport');
Expand Down Expand Up @@ -57,6 +58,12 @@ config.load = function (settings, ready) {
config.dirs.unshift(cwd);
}

var cmd = command(config.options);
config.command = {
raw: cmd,
string: cmd.executable + (cmd.args.length ? ' ' + cmd.args.join(' ') : '')
};

// now run automatic checks on system adding to the config object
checkWatchSupport(config, function (config) {
if (config.system.useFind === false && config.system.useWatch === false) {
Expand Down
39 changes: 7 additions & 32 deletions lib/monitor/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ var utils = require('../utils'),
killedAfterChange = false,
// timeout = 1000, // check every 1 second
noop = function() {},
restart = null;
restart = null,
nodeMajor = parseInt((process.versions.node.split('.') || [null,null])[1] || 0, 10);

function run(options) {
var command = run.command(options),
nodeMajor = parseInt((process.versions.node.split('.') || [null,null])[1] || 0, 10);
var cmd = config.command.raw;

utils.log.status('starting `' + command.executable + (command.args.length ? ' ' + command.args.join(' ') : '') + '`');
utils.log.status('starting `' + config.command.string + '`');

/*jshint validthis:true*/
restart = run.bind(this, options);
Expand All @@ -25,12 +25,12 @@ function run(options) {
config.lastStarted = Date.now();

if (nodeMajor >= 8) {
child = spawn(command.executable, command.args, {
child = spawn(cmd.executable, cmd.args, {
env: utils.merge(options.execOptions.env, process.env),
stdio: ['pipe', 'pipe', 'pipe']
});
} else {
child = spawn(command.executable, command.args);
child = spawn(cmd.executable, cmd.args);
}

bus.emit('start');
Expand Down Expand Up @@ -59,7 +59,7 @@ function run(options) {

child.on('error', function (error) {
if (error.code === 'ENOENT') {
utils.log.error('unable to run executable: "' + command.executable + '"');
utils.log.error('unable to run executable: "' + cmd.executable + '"');
process.exit(1);
}
});
Expand Down Expand Up @@ -145,31 +145,6 @@ function run(options) {
watch();
}

run.command = function (options) {
var executable = options.execOptions.exec,
args = [];

// after "executable" go the exec args (like --debug, etc)
if (options.execOptions.execArgs) {
[].push.apply(args, options.execOptions.execArgs);
}

// then goes the user's script arguments
if (options.args) {
[].push.apply(args, options.args);
}

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

return {
executable: executable,
args: args
};
};

// stubbed out for now, filled in during run
run.kill = noop;
run.restart = noop;
Expand Down
17 changes: 11 additions & 6 deletions lib/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ var coding = {
function log(type, text) {
var msg = colour(coding[type], '[nodemon] ' + (text||''));

if (required) {
bus.emit('log', { type: type, message: text, colour: msg });
} else if (type === 'error') {
util.error(msg);
} else {
util.log(msg);
// always push the message through our bus
bus.emit('log', { type: type, message: text, colour: msg });

// but if we're running on the command line, also echo out
// question: should we actually just consume our own events?
if (!required) {
if (type === 'error') {
util.error(msg);
} else {
util.log(msg);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/cli/exec.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
/*global describe:true, it: true */
var exec = require('../../lib/config/exec'),
run = require('../../lib/monitor/run'),
command = require('../../lib/config/command'),
assert = require('assert');

describe('nodemon exec', function () {
Expand Down Expand Up @@ -39,9 +39,9 @@ describe('nodemon exec', function () {

it('should replace {{filename}}', function () {
var options = exec({ script: 'app.js', exec: 'node {{filename}}.tmp --somethingElse' });
var command = run.command({ execOptions: options });
var cmd = command({ execOptions: options });

assert(command.executable + ' ' + command.args.join(' ') === 'node app.js.tmp --somethingElse', 'filename is interpolated');
assert(cmd.executable + ' ' + cmd.args.join(' ') === 'node app.js.tmp --somethingElse', 'filename is interpolated');
});

it('should support extension maps', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/cli/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var cli = require('../../lib/cli/'),
exec = require('../../lib/config/exec'),
pkg = require('../../package'),
assert = require('assert'),
command = require('../../lib/monitor/run').command;
command = require('../../lib/config/command');

function asCLI(cmd) {
return ('node nodemon ' + cmd).trim();
Expand Down
5 changes: 2 additions & 3 deletions test/config/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var load = require('../../lib/config/load'),
rules = require('../../lib/rules'),
exec = require('../../lib/config/exec'),
nodemon = require('../../lib/nodemon'),
command = require('../../lib/monitor/run').command,
command = require('../../lib/config/command'),
assert = require('assert');

function commandToString(command) {
Expand Down Expand Up @@ -42,6 +42,7 @@ describe('config load', function () {
utils.home = path.resolve(pwd, ['test', 'fixtures', 'global'].join(path.sep));

rules.reset();
nodemon.config.reset();
});

it('should remove ignore defaults if user provides their own', function (done) {
Expand Down Expand Up @@ -146,6 +147,4 @@ describe('config load', function () {
done();
});
});


});
3 changes: 3 additions & 0 deletions test/fixtures/nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"watch": ["four"],
"env": {
"USER": "remysharp"
},
"execMap": {
"js": "node --harmony"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "app.js"
}
7 changes: 0 additions & 7 deletions test/fork/change-detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ describe('nodemon fork simply running', function () {
});

describe('nodemon fork monitor', function () {
var complete = function (p, done, err) {
p.once('exit', function () {
done(err);
});
p.send('quit');
};

it('should restart on .js file changes with no arguments', function (done) {
var p = run(appjs, {
output: function (data) {
Expand Down
44 changes: 44 additions & 0 deletions test/fork/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
/*global describe:true, it: true, afterEach:true, beforeEach:true */
var assert = require('assert'),
utils = require('../utils'),
path = require('path'),
match = utils.match,
cleanup = utils.cleanup,
run = utils.run;

describe('nodemon full config test', function () {
var pwd = process.cwd();

afterEach(function () {
process.chdir(pwd);
});

beforeEach(function () {
// move to the fixtures directory to allow for config loading
process.chdir(path.resolve(pwd, 'test/fixtures'));
});

it('should allow execMap.js to be overridden', function (done) {
var p = run({ exec: '../../bin/nodemon.js',
args: ['-V']
}, {
error: function (data) {
p.send('quit');
cleanup(p, done, new Error(data));
}
});

p.on('message', function (event) {
if (event.type === 'log') {
if (match(event.data.message, 'starting `')) {
event.data.message.replace(/`(.*)`/, function (all, m) {
assert(m === 'node --harmony app.js', 'Arguments in the correct order: ' + m);
p.send('quit');
cleanup(p, done);
});
}
}
});
});
});
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function match(str, key) {
}

function run(cmd, callbacks) {
var cli = asCLI(cmd);
var cli = typeof cmd === 'string' ? asCLI(cmd) : cmd;
var proc = fork(cli.exec, cli.args, {
env: process.env,
cwd: process.cwd(),
Expand Down

0 comments on commit 2a2f126

Please sign in to comment.