Dynamic port switching (#516)

This commit is contained in:
Kaveh Khorram 2018-04-10 08:48:44 -07:00 committed by Joel Marcey
parent 80ece69a10
commit bbbe311004
2 changed files with 31 additions and 20 deletions

View file

@ -37,23 +37,34 @@ const program = require('commander');
program.option('--port <number>', 'Specify port number').parse(process.argv); program.option('--port <number>', 'Specify port number').parse(process.argv);
const port = parseInt(program.port, 10) || 3000; var port = process.env.PORT || 3000;
var numAttempts = 0;
var maxAttempts = 10;
checkPort();
tcpPortUsed function checkPort() {
.check(port, 'localhost') tcpPortUsed
.then(function(inUse) { .check(port, 'localhost')
if (inUse) { .then(function(inUse) {
console.error(chalk.red('Port ' + port + ' is in use')); if (inUse && numAttempts >= maxAttempts) {
process.exit(1); console.log("Reached max attempts, exiting. Please open up some ports or increase the number of attempts and try again.")
} else { process.exit(1)
console.log('Starting Docusaurus server on port ' + port + '...'); } else if (inUse) {
// start local server on specified port console.error(chalk.red('Port ' + port + ' is in use'));
const server = require('./server/server.js'); // Try again but with port + 1
server(port); port += 1;
} numAttempts += 1;
}) checkPort();
.catch(function(ex) { } else {
setTimeout(function() { console.log('Starting Docusaurus server on port ' + port + '...');
throw ex; // start local server on specified port
}, 0); const server = require('./server/server.js');
}); server(port);
}
})
.catch(function(ex) {
setTimeout(function() {
throw ex;
}, 0);
});
}

View file

@ -14,7 +14,7 @@
"url": "https://github.com/facebook/Docusaurus.git" "url": "https://github.com/facebook/Docusaurus.git"
}, },
"scripts": { "scripts": {
"ci-check": "yarn prettier:diff", "ci-check": "yarn prettier && yarn prettier:diff",
"format:source": "prettier --config .prettierrc --write \"lib/**/*.js\"", "format:source": "prettier --config .prettierrc --write \"lib/**/*.js\"",
"format:examples": "prettier --config .prettierrc --write \"examples/**/*.js\"", "format:examples": "prettier --config .prettierrc --write \"examples/**/*.js\"",
"nit:source": "prettier --config .prettierrc --list-different \"lib/**/*.js\"", "nit:source": "prettier --config .prettierrc --list-different \"lib/**/*.js\"",