Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
npm-debug.log
tmp
*.sublime-project
*.sublime-workspace
50 changes: 38 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function(grunt) {
'Gruntfile.js',
'tasks/**/*.js',
'test/*.js',
'!test/server_malformed.js'
],
options: {
jshintrc: '.jshintrc',
Expand Down Expand Up @@ -80,10 +81,15 @@ module.exports = function(grunt) {
custom_cmd: {
options: {
script: './test/server.coffee',
cmd: "coffee",
opts: ["node_modules/coffee-script/bin/coffee"],
output: "Express server listening on port .+"
}
},
malformed: {
options: {
script: './test/server_malformed.js'
}
},
custom_args: {
options: {
args: [1, 2],
Expand Down Expand Up @@ -146,19 +152,39 @@ module.exports = function(grunt) {
// plugin's task(s), then test the result.
grunt.registerTask('test', [
'clean',
'express:defaults', 'nodeunit:defaults', 'express:defaults:stop',
'express:custom_cmd', 'nodeunit:custom_cmd', 'express:custom_cmd:stop',
'express:custom_args', 'nodeunit:custom_args', 'express:custom_args:stop',
'express:custom_port', 'nodeunit:custom_port', 'express:custom_port:stop',
'express:custom_node_env', 'nodeunit:custom_node_env', 'express:custom_node_env:stop',
'express:custom_delay', 'nodeunit:custom_delay', 'express:custom_delay:stop',
'express:custom_output', 'nodeunit:custom_output', 'express:custom_output:stop',
'express:stoppable', 'express:stoppable:stop', 'nodeunit:stoppable',
'nodeunit:defaults',
'express:defaults',
'express:defaults:stop',
'express:malformed',
'express:custom_cmd',
'nodeunit:custom_cmd',
'express:custom_cmd:stop',
'express:custom_args',
'nodeunit:custom_args',
'express:custom_args:stop',
'express:custom_port',
'nodeunit:custom_port',
'express:custom_port:stop',
'express:custom_node_env',
'nodeunit:custom_node_env',
'express:custom_node_env:stop',
'express:custom_delay',
'nodeunit:custom_delay',
'express:custom_delay:stop',
'express:custom_output',
'nodeunit:custom_output',
'express:custom_output:stop',
'express:stoppable',
'express:stoppable:stop',
'nodeunit:stoppable',

// Multiple servers
'express:custom_port', 'express:defaults',
'nodeunit:defaults', 'nodeunit:custom_port',
'express:custom_port:stop', 'express:defaults:stop',
'express:custom_port',
'nodeunit:defaults',
'express:defaults',
'nodeunit:custom_port',
'express:custom_port:stop',
'express:defaults:stop',
]);

// By default, lint and run all tests.
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ or within each individual server task.
output: ".+",

// Set --debug
debug: false
debug: false,

// Set --debug-brk (requires debug to be true)
breakOnFirstLine: false,

// Object with properties `out` and `err` both will take a path to a log file and
// append the output of the server. Make sure the folders exist.
logs: undefined

}
}
```
Expand Down
22 changes: 12 additions & 10 deletions tasks/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ module.exports = function(grunt) {
var server = servers[this.target];
var action = this.args.shift() || 'start';
var options = this.options({
cmd: process.argv[0],
opts: [ ],
args: [ ],
node_env: undefined,
background: true,
fallback: function() { /* Prevent EADDRINUSE from breaking Grunt */ },
port: process.env.PORT || 3000,
delay: 0,
output: ".+",
debug: false
cmd: process.argv[0],
opts: [ ],
args: [ ],
node_env: undefined,
background: true,
fallback: function() { /* Prevent EADDRINUSE from breaking Grunt */ },
port: process.env.PORT || 3000,
delay: 0,
output: ".+",
debug: false,
breakOnFirstLine: false,
logs: undefined
});

options.script = path.resolve(options.script);
Expand Down
56 changes: 31 additions & 25 deletions tasks/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

'use strict';
var spawn = require('child_process').spawn;

module.exports = function(grunt, target) {
if (!process._servers) {
Expand All @@ -24,7 +25,6 @@ module.exports = function(grunt, target) {
done = null;
}
};

return {
start: function start(options) {
if (server) {
Expand Down Expand Up @@ -68,20 +68,29 @@ module.exports = function(grunt, target) {

// Set debug mode for node-inspector
if (options.debug) {
options.opts.unshift('--debug');
if (options.breakOnFirstLine) {
options.opts.unshift('--debug-brk');
} else {
options.opts.unshift('--debug');
}
if (options.cmd === 'coffee') {
options.opts.unshift('--nodejs');
}
}

if (options.background) {
var donefunc = (options.delay || options.output) ? function() {} : finished;
server = process._servers[target] = grunt.util.spawn({
cmd: options.cmd,
args: options.opts.concat(options.args),
env: process.env,
fallback: options.fallback
}, donefunc);
var errtype = process.stderr;
if(options.logs && options.logs.err) {
errtype = 'pipe';
}
server = process._servers[target] = spawn(
options.cmd,
options.opts.concat(options.args),
{
env: process.env,
stdio: ['ignore', 'pipe', errtype]
}
);

if (options.delay) {
setTimeout(finished, options.delay);
Expand All @@ -96,32 +105,29 @@ module.exports = function(grunt, target) {
}
});
}
server.stderr.on('data', function(data) {
if (!options.debug) {
finished();
} else {
var message = "" + data;
var regex = new RegExp('debugger listening', "gi");
if (!message.match(regex)) {
finished();
}
}
});
server.stdout.pipe(process.stdout);
server.stderr.pipe(process.stderr);
var out = process.stdout;
if(options.logs) {
var fs = require('fs'), path = require('path');
if(options.logs.out) {
out = fs.createWriteStream(path.resolve(options.logs.out), {flags: 'a'});
}
if(options.logs.err && errtype === 'pipe') {
server.stderr.pipe(fs.createWriteStream(path.resolve(options.logs.err), {flags: 'a'}));
}
}
server.stdout.pipe(out);
server.on('close',this.stop);
} else {
// Server is ran in current process
server = process._servers[target] = require(options.script);
}

process.on('exit', finished);
process.on('exit', this.stop);
},

stop: function stop() {
if (server && server.kill) {
grunt.log.writeln('Stopping'.red + ' Express server');

server.removeAllListeners('close');
server.kill('SIGTERM');
process.removeListener('exit', finished);
process.removeListener('exit', stop);
Expand Down
2 changes: 1 addition & 1 deletion test/custom_delay_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports.custom_delay = {

get('http://localhost:3000/hello.txt', function(res, body) {
test.equal(res.statusCode, 200, 'should return 200');
test.equal(body, 'Howdy!\n', 'should return static page');
test.equal(body.trim(), 'Howdy!', 'should return static page');
test.done();
}, function(err) {
test.done();
Expand Down
2 changes: 1 addition & 1 deletion test/custom_output_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports.custom_output = {

get('http://localhost:3000/hello.txt', function(res, body) {
test.equal(res.statusCode, 200, 'should return 200');
test.equal(body, 'Howdy!\n', 'should return static page');
test.equal(body.trim(), 'Howdy!', 'should return static page');
test.done();
}, function(err) {
test.done();
Expand Down
2 changes: 1 addition & 1 deletion test/custom_port_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports.custom_port = {

get('http://localhost:8080/hello.txt', function(res, body) {
test.equal(res.statusCode, 200, 'should return 200');
test.equal(body, 'Howdy!\n', 'should return static page');
test.equal(body.trim(), 'Howdy!', 'should return static page');
test.done();
}, function(err) {
test.done();
Expand Down
25 changes: 25 additions & 0 deletions test/server_malformed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

/**
* Test Server
*/

var app = require('./app');
var start = Date.now();
var log = function(message) {
console.log("[" + (Date.now() - start) + "] " + message);
};

log"Begin server.js");

setTimeout(function() {
module.exports = app.listen(app.get('port'), function() {
log("Express server listening on port " + app.get('port'));
});
}, 50);

setTimeout(function() {
log("250ms timeout");
}, 250);

log("End server.js");