Skip to content
Closed
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
19 changes: 18 additions & 1 deletion tasks/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function(grunt) {
var portscanner = require('portscanner');
var async = require('async');
var util = require('util');
var serverInstances = {};

var MAX_PORTS = 30; // Maximum available ports to check after the specified port

Expand All @@ -43,6 +44,18 @@ module.exports = function(grunt) {
middlewares.push(serveIndex(directory.path || directory));
return middlewares;
};

grunt.registerMultiTask('disconnect', 'Stop a connect web server.', function() {
var done = this.async();
var taskTarget = this.target;
var stopServer = grunt.config.get('connect.' + taskTarget + '.shutdown');

if (stopServer) {
stopServer(taskTarget, done);
}

grunt.config.set('connect.' + taskTarget + '.shutdown', undefined);
});

grunt.registerMultiTask('connect', 'Start a connect web server.', function() {
var done = this.async();
Expand Down Expand Up @@ -202,7 +215,7 @@ module.exports = function(grunt) {
grunt.fatal('Port ' + options.port + ' is already in use by another process.');
}

server
serverInstances[taskTarget] = server
.listen(foundPort, options.hostname)
.on('listening', function() {
var port = foundPort;
Expand Down Expand Up @@ -240,6 +253,10 @@ module.exports = function(grunt) {
}
});
});
grunt.config.set('connect.' + taskTarget + '.shutdown', function(taskTarget, cb) {
serverInstances[taskTarget].close(cb);
serverInstances[taskTarget] = undefined;
});

// So many people expect this task to keep alive that I'm adding an option
// for it. Running the task explicitly as grunt:keepalive will override any
Expand Down