Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix port restart issue #156

Merged
merged 6 commits into from
Mar 2, 2016
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.sass-cache
.DS_Store
.start.pid
.port.tmp
public
govuk_modules
node_modules/*
Expand Down
29 changes: 22 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var basicAuth = require('basic-auth'),
prompt = require('prompt'),
portScanner = require('portscanner');
config = require(__dirname + '/../app/config.js'),
fs = require('fs');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be alphabetical.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm, it wasn't before either, so lets just focus on the bug fix

portScanner = require('portscanner'),
prompt = require('prompt');

/**
* Simple basic auth middleware for use with Express 4.x.
Expand Down Expand Up @@ -35,17 +37,26 @@ exports.basicAuth = function(username, password) {

exports.findAvailablePort = function(app){

var port = (process.env.PORT || 3000);
var port = null;

try {
port = Number(fs.readFileSync(__dirname+'/../.port.tmp'));
} catch (e){
port = (process.env.PORT || config.port);
}

console.log('');

// Check that default port is free, else offer to change
portScanner.findAPortNotInUse(port, port+50, '127.0.0.1', function(error, availablePort) {

if (port == availablePort){

app.listen(port);
console.log('Listening on port ' + port + ' url: http://localhost:' + port);
}
else {

} else {

// Default port in use - offer to change to available port
console.error("ERROR: Port " + port + " in use - you may have another prototype running.\n");
// Set up prompt settings
Expand All @@ -63,17 +74,21 @@ exports.findAvailablePort = function(app){
pattern: /y(es)?|no?/i,
message: 'Please enter y or n'
}], function (err, result) {

if (result.answer.match(/y(es)?/i) ) {
// User answers yes
port = availablePort;
fs.writeFileSync(__dirname+'/../.port.tmp', port);
app.listen(port);
console.log('Changed to port ' + port + ' url: http://localhost:' + port);
}
else {

} else {

// User answers no - exit
console.log('\nYou can set a new default port in server.js, or by running the server with PORT=XXXX');
console.log("\nExit by pressing 'ctrl + c'");
process.exit(0);

}
});
}
Expand Down
17 changes: 15 additions & 2 deletions start.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
// Check for `node_modules` folder and warn if missing
var fs = require('fs');

if (!fs.existsSync(__dirname + '/node_modules')) {
console.error('ERROR: Node module folder missing. Try running `npm install`');
process.exit(0);
}

// remove .port.tmp if it exists
try {
fs.unlinkSync(__dirname + '/.port.tmp');
} catch(e){}

var gruntfile = __dirname + '/Gruntfile.js';

require(__dirname + '/node_modules/grunt/lib/grunt.js').cli({
'gruntfile' : gruntfile
});

process.on('SIGINT', function() {
process.kill(process.pid, 'SIGTERM');
process.exit();

// remove .port.tmp if it exists
try {
fs.unlinkSync(__dirname + '/.port.tmp');
} catch(e){}

process.exit(0);

});