Skip to content

Commit

Permalink
Dynamic port switching (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
InternetExplorer7 authored and JoelMarcey committed Apr 10, 2018
1 parent 80ece69 commit bbbe311
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
51 changes: 31 additions & 20 deletions lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,34 @@ const program = require('commander');

program.option('--port <number>', 'Specify port number').parse(process.argv);

const port = parseInt(program.port, 10) || 3000;

tcpPortUsed
.check(port, 'localhost')
.then(function(inUse) {
if (inUse) {
console.error(chalk.red('Port ' + port + ' is in use'));
process.exit(1);
} else {
console.log('Starting Docusaurus server on port ' + port + '...');
// start local server on specified port
const server = require('./server/server.js');
server(port);
}
})
.catch(function(ex) {
setTimeout(function() {
throw ex;
}, 0);
});
var port = process.env.PORT || 3000;
var numAttempts = 0;
var maxAttempts = 10;
checkPort();

function checkPort() {
tcpPortUsed
.check(port, 'localhost')
.then(function(inUse) {
if (inUse && numAttempts >= maxAttempts) {
console.log("Reached max attempts, exiting. Please open up some ports or increase the number of attempts and try again.")
process.exit(1)
} else if (inUse) {
console.error(chalk.red('Port ' + port + ' is in use'));
// Try again but with port + 1
port += 1;
numAttempts += 1;
checkPort();
} else {
console.log('Starting Docusaurus server on port ' + port + '...');
// start local server on specified port
const server = require('./server/server.js');
server(port);
}
})
.catch(function(ex) {
setTimeout(function() {
throw ex;
}, 0);
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "https://github.com/facebook/Docusaurus.git"
},
"scripts": {
"ci-check": "yarn prettier:diff",
"ci-check": "yarn prettier && yarn prettier:diff",
"format:source": "prettier --config .prettierrc --write \"lib/**/*.js\"",
"format:examples": "prettier --config .prettierrc --write \"examples/**/*.js\"",
"nit:source": "prettier --config .prettierrc --list-different \"lib/**/*.js\"",
Expand Down

0 comments on commit bbbe311

Please sign in to comment.