feat: allow different host through CLI flag (#1475)

* feat(core): use 0.0.0.0 for livereload address

* test(core): update server tests

* feat(cli): add host by command line flag

* test: revert test changes

* fix(core): update server to use custom host if supplied

* refactor: nits
This commit is contained in:
Cole 2019-05-20 09:31:41 -07:00 committed by Endi
parent 382b88bb49
commit 0568ad4992
6 changed files with 13 additions and 8 deletions

View file

@ -146,6 +146,7 @@ This command will build the static website, apply translations if necessary, and
| Options | Default | Description | | Options | Default | Description |
| ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ | | ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `--port <number>` | `3000` | The website will be served from port 3000 by default, but if the port is taken up, Docusaurus will attempt to find an available one. | | `--port <number>` | `3000` | The website will be served from port 3000 by default, but if the port is taken up, Docusaurus will attempt to find an available one. |
|`--host <host>`|`localhost`|Specify a host to use. E.g., if you want your server to be accessible externally, you can use --host 0.0.0.0.|
| `--watch` | - | Whether to watch the files and live reload the page when files are changed. Defaults to true. Disable this by using `--no-watch`. | | `--watch` | - | Whether to watch the files and live reload the page when files are changed. Defaults to true. Disable this by using `--no-watch`. |
You can specify the browser application to be opened by setting the `BROWSER` environment variable before the command, e.g.: You can specify the browser application to be opened by setting the `BROWSER` environment variable before the command, e.g.:

View file

@ -7,6 +7,7 @@
const gaze = require('gaze'); const gaze = require('gaze');
const tinylr = require('tiny-lr'); const tinylr = require('tiny-lr');
const program = require('commander');
const readMetadata = require('./readMetadata.js'); const readMetadata = require('./readMetadata.js');
function start(port) { function start(port) {
@ -26,10 +27,11 @@ function start(port) {
}, },
); );
} }
const getReloadScriptUrl = () => { const getReloadScriptUrl = () => {
const port = process.env.LIVERELOAD_PORT; const port = process.env.LIVERELOAD_PORT;
return `http://localhost:${port}/livereload.js`; const host = program.host || 'localhost';
return `http://${host}:${port}/livereload.js`;
}; };
module.exports = { module.exports = {

View file

@ -7,7 +7,7 @@
/* eslint-disable no-cond-assign */ /* eslint-disable no-cond-assign */
function execute(port) { function execute(port, host) {
const extractTranslations = require('../write-translations'); const extractTranslations = require('../write-translations');
const metadataUtils = require('./metadataUtils'); const metadataUtils = require('./metadataUtils');
const blog = require('./blog'); const blog = require('./blog');
@ -355,7 +355,7 @@ function execute(port) {
// for example, request to "blog" returns "blog/index.html" or "blog.html" // for example, request to "blog" returns "blog/index.html" or "blog.html"
app.get(routing.noExtension(), (req, res, next) => { app.get(routing.noExtension(), (req, res, next) => {
const slash = req.path.toString().endsWith('/') ? '' : '/'; const slash = req.path.toString().endsWith('/') ? '' : '/';
const requestUrl = `http://localhost:${port}${req.path}`; const requestUrl = `http://${host}:${port}${req.path}`;
requestFile(`${requestUrl + slash}index.html`, res, () => { requestFile(`${requestUrl + slash}index.html`, res, () => {
requestFile( requestFile(
slash === '/' slash === '/'
@ -374,7 +374,7 @@ function execute(port) {
next(); next();
return; return;
} }
requestFile(`http://localhost:${port}${req.path}.html`, res, next); requestFile(`http://${host}:${port}${req.path}.html`, res, next);
}); });
app.listen(port); app.listen(port);

View file

@ -23,12 +23,13 @@ function startLiveReloadServer() {
function startServer() { function startServer() {
const initialServerPort = const initialServerPort =
parseInt(program.port, 10) || process.env.PORT || 3000; parseInt(program.port, 10) || process.env.PORT || 3000;
const host = program.host || 'localhost';
const promise = portFinder const promise = portFinder
.getPortPromise({port: initialServerPort}) .getPortPromise({port: initialServerPort})
.then(port => { .then(port => {
server(port); server(port, host);
const {baseUrl} = require(`${CWD}/siteConfig.js`); const {baseUrl} = require(`${CWD}/siteConfig.js`);
const serverAddress = `http://localhost:${port}${baseUrl}`; const serverAddress = `http://${host}:${port}${baseUrl}`;
console.log('Docusaurus server started on port %d', port); console.log('Docusaurus server started on port %d', port);
openBrowser(serverAddress); openBrowser(serverAddress);
}); });

View file

@ -42,6 +42,7 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) {
program program
.option('--port <number>', 'Specify port number') .option('--port <number>', 'Specify port number')
.option('--no-watch', 'Toggle live reload file watching') .option('--no-watch', 'Toggle live reload file watching')
.option('--host <host>', 'use specified host (default: localhost)')
.parse(process.argv); .parse(process.argv);
startDocusaurus().catch(ex => { startDocusaurus().catch(ex => {

View file

@ -46,7 +46,7 @@ Builds and serves the static site with [Webpack Dev Server](https://webpack.js.o
|Options|Default|Description| |Options|Default|Description|
|-|-|-| |-|-|-|
|`--port`|`3000`|Specifies the port of the dev server| |`--port`|`3000`|Specifies the port of the dev server|
|`--host`|`localhost`|Specifie a host to use. E.g., if you want your server to be accessible externally, you can use `--host 0.0.0.0`| |`--host`|`localhost`|Specify a host to use. E.g., if you want your server to be accessible externally, you can use `--host 0.0.0.0`|
|`--hot-only`|`false`|Enables Hot Module Replacement without page refresh as fallback in case of build failures. More information [here](https://webpack.js.org/configuration/dev-server/#devserverhotonly).| |`--hot-only`|`false`|Enables Hot Module Replacement without page refresh as fallback in case of build failures. More information [here](https://webpack.js.org/configuration/dev-server/#devserverhotonly).|
### `docusaurus build` ### `docusaurus build`