Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow different host through CLI flag #1475

Merged
merged 7 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/docusaurus-1.x/lib/server/liveReloadServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const gaze = require('gaze');
const tinylr = require('tiny-lr');
const program = require('commander');
const readMetadata = require('./readMetadata.js');

function start(port) {
Expand All @@ -26,10 +27,11 @@ function start(port) {
},
);
}

const getReloadScriptUrl = () => {
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 = {
Expand Down
6 changes: 4 additions & 2 deletions packages/docusaurus-1.x/lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function execute(port) {
const blog = require('./blog');
const docs = require('./docs');
const env = require('./env.js');
const program = require('commander');
const express = require('express');
const React = require('react');
const request = require('request');
Expand All @@ -31,6 +32,7 @@ function execute(port) {
const CWD = process.cwd();
const join = path.join;
const sep = path.sep;
const host = program.host || 'localhost';

function removeModulePathFromCache(moduleName) {
/* eslint-disable no-underscore-dangle */
Expand Down Expand Up @@ -355,7 +357,7 @@ function execute(port) {
// for example, request to "blog" returns "blog/index.html" or "blog.html"
app.get(routing.noExtension(), (req, res, next) => {
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(
slash === '/'
Expand All @@ -374,7 +376,7 @@ function execute(port) {
next();
return;
}
requestFile(`http://localhost:${port}${req.path}.html`, res, next);
requestFile(`http://${host}:${port}${req.path}.html`, res, next);
});

app.listen(port);
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-1.x/lib/server/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ function startLiveReloadServer() {
function startServer() {
const initialServerPort =
parseInt(program.port, 10) || process.env.PORT || 3000;
const initialHost = program.host || 'localhost';
const promise = portFinder
.getPortPromise({port: initialServerPort})
.then(port => {
server(port);
const {baseUrl} = require(`${CWD}/siteConfig.js`);
const serverAddress = `http://localhost:${port}${baseUrl}`;
const serverAddress = `http://${initialHost}:${port}${baseUrl}`;
console.log('Docusaurus server started on port %d', port);
openBrowser(serverAddress);
});
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-1.x/lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) {
program
.option('--port <number>', 'Specify port number')
.option('--no-watch', 'Toggle live reload file watching')
.option('--host <host>', 'use specified host (default: localhost)')
.parse(process.argv);

startDocusaurus().catch(ex => {
Expand Down
1 change: 1 addition & 0 deletions website-1.x/versioned_docs/version-1.8.0/api-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ This command will build the static website, apply translations if necessary, and
| 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. |
|`--host <number>`|`localhost`|Use a custom host with localhost as the default.|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it host ?

Copy link
Contributor Author

@palmer-cl palmer-cl May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops my bad. I mean why host <number>.

GitHub swallowed the <number>

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha. All good. Ya, I figured number as in 127.0.0.1 but maybe 'value' is better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| `--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.:
Expand Down