From cfe571ec8b60f5df7af039ff65dd5bcd2cad5707 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Thu, 18 Feb 2016 19:04:41 +0000 Subject: [PATCH 1/6] fix port restart issue #148 --- lib/utils.js | 27 +++++++++++++++++++++------ start.js | 13 +++++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index d2f19c3b70..0b1d3b055d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,7 @@ var basicAuth = require('basic-auth'), prompt = require('prompt'), - portScanner = require('portscanner'); + portScanner = require('portscanner'), + fs = require('fs'); /** * Simple basic auth middleware for use with Express 4.x. @@ -35,17 +36,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 || 3000); + } 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 @@ -63,17 +73,22 @@ 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); + } }); } diff --git a/start.js b/start.js index 76175a706b..06a16dbac9 100644 --- a/start.js +++ b/start.js @@ -1,16 +1,25 @@ // 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); } 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.kill(process.pid, 'SIGTERM'); + process.exit(0); + }); From 623e36b1e35cc31c13822b13dd2907e1eb34e760 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Thu, 25 Feb 2016 16:19:38 +0000 Subject: [PATCH 2/6] remove port.tmp on startup --- start.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/start.js b/start.js index 06a16dbac9..96ed02f9d9 100644 --- a/start.js +++ b/start.js @@ -6,6 +6,11 @@ if (!fs.existsSync(__dirname + '/node_modules')) { 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({ From 14b2046084456a1f4f5f01e4f6b062583f6217b3 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Tue, 1 Mar 2016 18:01:15 +0000 Subject: [PATCH 3/6] get port from config, remove comment --- lib/utils.js | 7 ++++--- start.js | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 0b1d3b055d..7932af27bd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,7 +1,8 @@ var basicAuth = require('basic-auth'), - prompt = require('prompt'), - portScanner = require('portscanner'), + config = require(__dirname + '/../app/config.js'), fs = require('fs'); + portScanner = require('portscanner'), + prompt = require('prompt'); /** * Simple basic auth middleware for use with Express 4.x. @@ -41,7 +42,7 @@ exports.findAvailablePort = function(app){ try { port = Number(fs.readFileSync(__dirname+'/../port.tmp')); } catch (e){ - port = (process.env.PORT || 3000); + port = (process.env.PORT || config.port); } console.log(''); diff --git a/start.js b/start.js index 96ed02f9d9..6ef96c7a67 100644 --- a/start.js +++ b/start.js @@ -24,7 +24,6 @@ process.on('SIGINT', function() { fs.unlinkSync(__dirname + '/port.tmp'); } catch(e){} - // process.kill(process.pid, 'SIGTERM'); process.exit(0); }); From fe93bfa9cdd91e409cc00804f827df88539e7ca7 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Tue, 1 Mar 2016 18:02:36 +0000 Subject: [PATCH 4/6] add port.tmp to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6c2ce5d982..cdd209d659 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .sass-cache .DS_Store .start.pid +port.tmp public govuk_modules node_modules/* From 8697afa2a5ebf38b38b8f241ec318065aad75761 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Tue, 1 Mar 2016 18:12:34 +0000 Subject: [PATCH 5/6] use .port instead of port.tmp --- .gitignore | 2 +- lib/utils.js | 7 +++---- start.js | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index cdd209d659..2943e2df00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .sass-cache .DS_Store .start.pid -port.tmp +.port.tmp public govuk_modules node_modules/* diff --git a/lib/utils.js b/lib/utils.js index 7932af27bd..2894c3ceb9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -40,9 +40,9 @@ exports.findAvailablePort = function(app){ var port = null; try { - port = Number(fs.readFileSync(__dirname+'/../port.tmp')); + port = Number(fs.readFileSync(__dirname+'/../.port.tmp')); } catch (e){ - port = (process.env.PORT || config.port); + port = (process.env.PORT || config.port.tmp); } console.log(''); @@ -76,10 +76,9 @@ exports.findAvailablePort = function(app){ }], function (err, result) { if (result.answer.match(/y(es)?/i) ) { - // User answers yes port = availablePort; - fs.writeFileSync(__dirname+'/../port.tmp', port); + fs.writeFileSync(__dirname+'/../.port.tmp', port); app.listen(port); console.log('Changed to port ' + port + ' url: http://localhost:' + port); diff --git a/start.js b/start.js index 6ef96c7a67..d91ad66283 100644 --- a/start.js +++ b/start.js @@ -6,9 +6,9 @@ if (!fs.existsSync(__dirname + '/node_modules')) { process.exit(0); } -// remove port.tmp if it exists +// remove .port.tmp if it exists try { - fs.unlinkSync(__dirname + '/port.tmp'); + fs.unlinkSync(__dirname + '/.port.tmp'); } catch(e){} var gruntfile = __dirname + '/Gruntfile.js'; @@ -19,9 +19,9 @@ require(__dirname + '/node_modules/grunt/lib/grunt.js').cli({ process.on('SIGINT', function() { - // remove port.tmp if it exists + // remove .port.tmp if it exists try { - fs.unlinkSync(__dirname + '/port.tmp'); + fs.unlinkSync(__dirname + '/.port.tmp'); } catch(e){} process.exit(0); From e5f57b71b0fbfb7e2d0e1109ab4d1b640ce6fa31 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Wed, 2 Mar 2016 11:24:10 +0000 Subject: [PATCH 6/6] fix config.port bug --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 2894c3ceb9..b0771490dd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -42,7 +42,7 @@ exports.findAvailablePort = function(app){ try { port = Number(fs.readFileSync(__dirname+'/../.port.tmp')); } catch (e){ - port = (process.env.PORT || config.port.tmp); + port = (process.env.PORT || config.port); } console.log('');