Skip to content

Commit

Permalink
Merge pull request remy#436 from remy/fix/435
Browse files Browse the repository at this point in the history
Allow piping commands in the exec
  • Loading branch information
remy committed Dec 9, 2014
2 parents bcd75d6 + bab695d commit ffa59fc
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 9 deletions.
11 changes: 10 additions & 1 deletion lib/monitor/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,17 @@ function match(files, monitor, ext) {
});
} // else assume *.*

var result = good.concat(whitelist);

if (utils.isWindows) {
// fix for windows testing - I *think* this is okay to do
result = result.map(function (file) {
return file.slice(0, 1).toLowerCase() + file.slice(1);
});
}

return {
result: good.concat(whitelist),
result: result,
ignored: ignored,
watched: watched,
total: files.length
Expand Down
20 changes: 18 additions & 2 deletions lib/monitor/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,29 @@ function run(options) {
stdio = ['pipe', process.stdout, process.stderr];
}

var sh = 'sh';
var shFlag = '-c';

if (utils.isWindows) {
sh = 'cmd';
shFlag = '/c';
}

var args = '';
[cmd.executable].concat(cmd.args).forEach(function (arg) {
if (arg.match(/[ '"]/)) {
arg = '"' + arg.replace(/"/g, '\\"') + '"';
}
args += ' ' + arg;
});

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

if (config.required) {
Expand Down
4 changes: 3 additions & 1 deletion lib/monitor/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function reset() {
while (watchers.length) {
watcher = watchers.pop();
watcher.removeAllListeners('change');
watcher.close();
if (watcher.close) { // watchFile doesn't have this
watcher.close();
}
}

// reset the watched directory too
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ util.inherits(Bus, events.EventEmitter);

var bus = new Bus();

/*
var collected = {};
bus.on('newListener', function (event) {
if (!collected[event]) {
collected[event] = true;
bus.on(event, function (e) {
console.log('>> ' + event);
});
}
});
//*/

// proxy process messages (if forked) to the bus
process.on('message', function (event) {
bus.emit(event);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"reload",
"terminal"
],
"version": "1.3.0-1",
"version": "1.3.0-5",
"preferGlobal": "true",
"licenses": [
{
Expand All @@ -35,7 +35,7 @@
"main": "./lib/nodemon",
"scripts": {
"coverage": "istanbul cover _mocha -- --timeout 30000 --ui bdd --reporter list test/**/*.test.js",
"test": "node_modules/mocha/bin/_mocha --timeout 30000 --ui bdd test/**/*.test.js",
"test": "node_modules/.bin/mocha --timeout 30000 --ui bdd test/**/*.test.js",
":test": "for FILE in test/**/*.test.js; do echo $FILE; ./node_modules/.bin/mocha --timeout 30000 $FILE; if [ $? -ne 0 ]; then exit 1; fi; done",
"web": "node web"
},
Expand Down
93 changes: 93 additions & 0 deletions test/events/complete.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';
/*global describe:true, it: true, afterEach: true */
var nodemon = require('../../lib/'),
assert = require('assert'),
path = require('path'),
touch = require('touch'),
utils = require('../utils'),
appjs = path.resolve(__dirname, '..', 'fixtures', 'env.js');

describe('events should follow normal flow on user triggered change', function () {
function conf() {
utils.port++;
return {
script: appjs,
verbose: true,
stdout: false,
noReset: true,
env: { PORT: utils.port, USER: 'nodemon' },
};
}

beforeEach(function (done) {
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
});

after(function (done) {
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
});

it('start', function (done) {
nodemon(conf()).once('start', function () {
assert(true, '"start" event');
done();
});
});

it('config:update', function (done) {
nodemon(conf()).on('config:update', function () {
assert(true, '"config:update" event');
done();
});
});

it('exit', function (done) {
var plan = new utils.Plan(4, function () {
nodemon.reset(done);
});
var run = 0;
nodemon(conf()).on('exit', function () {
plan.assert(true, '"exit" event');
if (run === 1) {
setTimeout(function () {
plan.assert(true, 'restarting');
touch.sync(appjs);
}, 1000);
} else if (run === 2) {
plan.assert(true, 'finished');
} else {
plan.assert(false, 'quit too many times: ' + run);
}
}).on('start', function () {
run++;
});
})

it('stdout', function (done) {
nodemon(conf()).on('stdout', function (data) {
assert(true, '"stdout" event with: ' + data);
done();
});
});

it('restart', function (done) {
var plan = new utils.Plan(4, function () {
nodemon.reset(done);
});

nodemon(conf()).on('restart', function (files) {
assert(true, '"restart" event with ' + files);
assert(files[0] === appjs, 'restart due to ' + files.join(' ') + ' changing');
}).on('exit', function () {
plan.assert(true, '"exit" event');
setTimeout(function () {
plan.assert(true, 'restarting');
touch.sync(appjs);
}, 1000);
});
});
});
4 changes: 2 additions & 2 deletions test/fixtures/hello.ls
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
http = require 'http'
server = http.createServer (req, res) -> res.end 'hi\n'
server.listen 8000
console.log "Listening on port 8000"
server.listen process.env.PORT
console.log "Listening..."
23 changes: 22 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var fork = require('child_process').fork,
path = require('path'),
appjs = path.resolve(__dirname, 'fixtures', 'app.js'),
assert = require('assert'),
port = 8000,
appcoffee = path.resolve(__dirname, 'fixtures', 'app.coffee');

Expand Down Expand Up @@ -69,7 +70,7 @@ function run(cmd, callbacks) {

function cleanup(p, done, err) {
if (p) {
p.on('exit', function () {
p.once('exit', function () {
p = null;
done(err);
});
Expand All @@ -79,7 +80,27 @@ function cleanup(p, done, err) {
}
}

function Plan(count, done) {
this.done = done;
this.count = count;
}

Plan.prototype.assert = function() {
assert.apply(null, arguments);

if (this.count === 0) {
assert(false, 'Too many assertions called');
} else {
this.count--;
}

if (this.count === 0) {
this.done();
}
};

module.exports = {
Plan: Plan,
asCLI: asCLI,
match: match,
run: run,
Expand Down

0 comments on commit ffa59fc

Please sign in to comment.