mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-19 20:17:06 +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
51
lib/server/start.js
Normal file
51
lib/server/start.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const program = require('commander');
|
||||
const openBrowser = require('react-dev-utils/openBrowser');
|
||||
const portFinder = require('portfinder');
|
||||
const liveReloadServer = require('./liveReloadServer.js');
|
||||
const server = require('./server.js');
|
||||
|
||||
const CWD = process.cwd();
|
||||
|
||||
function startLiveReloadServer() {
|
||||
const promise = portFinder.getPortPromise({port: 35729}).then(port => {
|
||||
liveReloadServer.start(port);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
function startServer() {
|
||||
const initialServerPort =
|
||||
parseInt(program.port, 10) || process.env.PORT || 3000;
|
||||
const promise = portFinder
|
||||
.getPortPromise({port: initialServerPort})
|
||||
.then(port => {
|
||||
server(port);
|
||||
const {baseUrl} = require(`${CWD}/siteConfig.js`);
|
||||
const serverAddress = `http://localhost:${port}${baseUrl}`;
|
||||
console.log('Docusaurus server started on port %d', port);
|
||||
openBrowser(serverAddress);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
function startDocusaurus() {
|
||||
if (program.watch) {
|
||||
return startLiveReloadServer()
|
||||
.catch(ex => console.warn(`Failed to start live reload server: ${ex}`))
|
||||
.then(() => startServer());
|
||||
}
|
||||
return startServer();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
startDocusaurus,
|
||||
startServer,
|
||||
startLiveReloadServer,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue