mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
fix: live reload port fallback if port is used (#899)
* Move start reload server into separate module * Find an unused port when starting the live reload server * Move findUnusedPort into module * Add tests for findUnusedPort module * Refactor findUnusedPort * Move starting of servers into separate module and add tests * Remove unused constants.js * Zap extra line breaks * Add tests for liveReloadServer * Rename serverController to start * Move start into lib/server * Add portfinder package * Replace findUnusedPort with portfinder * nits
This commit is contained in:
parent
c4740f7af2
commit
bbef20d345
10 changed files with 291 additions and 84 deletions
|
@ -21,12 +21,12 @@ require('babel-register')({
|
|||
const chalk = require('chalk');
|
||||
const fs = require('fs');
|
||||
const program = require('commander');
|
||||
const openBrowser = require('react-dev-utils/openBrowser');
|
||||
const tcpPortUsed = require('tcp-port-used');
|
||||
|
||||
const CWD = process.cwd();
|
||||
const env = require('./server/env.js');
|
||||
|
||||
const {startDocusaurus} = require('./server/start.js');
|
||||
|
||||
if (!fs.existsSync(`${CWD}/siteConfig.js`)) {
|
||||
console.error(
|
||||
chalk.red('Error: No siteConfig.js file found in website folder!')
|
||||
|
@ -44,41 +44,7 @@ program
|
|||
.option('--no-watch', 'Toggle live reload file watching')
|
||||
.parse(process.argv);
|
||||
|
||||
let port = parseInt(program.port, 10) || process.env.PORT || 3000;
|
||||
let numAttempts = 0;
|
||||
const MAX_ATTEMPTS = 10;
|
||||
|
||||
function checkPort() {
|
||||
tcpPortUsed
|
||||
.check(port, 'localhost')
|
||||
.then(inUse => {
|
||||
if (inUse && numAttempts >= MAX_ATTEMPTS) {
|
||||
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 {
|
||||
// start local server on specified port
|
||||
const server = require('./server/server.js');
|
||||
server(port, program.opts());
|
||||
const {baseUrl} = require(`${CWD}/siteConfig.js`);
|
||||
const host = `http://localhost:${port}${baseUrl}`;
|
||||
console.log('Docusaurus server started on port %d', port);
|
||||
openBrowser(host);
|
||||
}
|
||||
})
|
||||
.catch(ex => {
|
||||
setTimeout(() => {
|
||||
throw ex;
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
|
||||
checkPort();
|
||||
startDocusaurus().catch(ex => {
|
||||
console.error(chalk.red(`Failed to start Docusaurus server: ${ex}`));
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue