mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 03:12:35 +02:00
Dynamic port switching (#516)
This commit is contained in:
parent
80ece69a10
commit
bbbe311004
2 changed files with 31 additions and 20 deletions
|
@ -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;
|
||||
var port = process.env.PORT || 3000;
|
||||
var numAttempts = 0;
|
||||
var maxAttempts = 10;
|
||||
checkPort();
|
||||
|
||||
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);
|
||||
});
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue