Skip to content

Commit

Permalink
Use a system assigned port if 0 or ? is specified
Browse files Browse the repository at this point in the history
Fixes #35.
  • Loading branch information
kevva committed Feb 16, 2015
1 parent fc0a3be commit f9ae25d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"bin-version-check": "^2.0.0",
"get-port": "^1.0.0",
"opn": "^1.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -123,7 +123,7 @@ grunt.registerTask('phpwatch', ['php:watch', 'watch']);
Type: `number`
Default: `8000`

The port on which you want to access the webserver. Task will fail if the port is already in use.
The port on which you want to access the webserver. Task will fail if the port is already in use. Use the special value `?` to use a system-assigned port.

### hostname

Expand Down
67 changes: 40 additions & 27 deletions tasks/php.js
Expand Up @@ -3,6 +3,7 @@ var spawn = require('child_process').spawn;
var http = require('http');
var open = require('opn');
var binVersionCheck = require('bin-version-check');
var getPort = require('get-port');

module.exports = function (grunt) {
var checkServerTries = 0;
Expand Down Expand Up @@ -49,45 +50,57 @@ module.exports = function (grunt) {
open: false,
bin: 'php'
});
var host = options.hostname + ':' + options.port;
var args = ['-S', host];

if (options.ini) {
args.push('-c', options.ini);
}

if (options.router) {
args.push(options.router);
}


binVersionCheck(options.bin, '>=5.4', function (err) {
getPort(function (err, port) {
if (err) {
grunt.warn(err);
cb();
return;
}

var cp = spawn(options.bin, args, {
cwd: options.base,
stdio: 'inherit'
});
if (options.port === '?') {
options.port = port;
}

var host = options.hostname + ':' + options.port;
var args = ['-S', host];

if (options.ini) {
args.push('-c', options.ini);
}

// quit PHP when grunt is done
process.on('exit', function () {
cp.kill();
});
if (options.router) {
args.push(options.router);
}

// check when the server is ready. tried doing it by listening
// to the child process `data` event, but it's not triggered...
checkServer(options.hostname, options.port, function () {
if (!this.flags.keepalive && !options.keepalive) {
binVersionCheck(options.bin, '>=5.4', function (err) {
if (err) {
grunt.warn(err);
cb();
return;
}

if (options.open) {
open('http://' + host);
}
var cp = spawn(options.bin, args, {
cwd: options.base,
stdio: 'inherit'
});

// quit PHP when grunt is done
process.on('exit', function () {
cp.kill();
});

// check when the server is ready. tried doing it by listening
// to the child process `data` event, but it's not triggered...
checkServer(options.hostname, options.port, function () {
if (!this.flags.keepalive && !options.keepalive) {
cb();
}

if (options.open) {
open('http://' + host);
}
}.bind(this));
}.bind(this));
}.bind(this));
});
Expand Down

0 comments on commit f9ae25d

Please sign in to comment.