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

@ -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) {
@ -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 = {

View file

@ -7,7 +7,7 @@
/* eslint-disable no-cond-assign */
function execute(port) {
function execute(port, host) {
const extractTranslations = require('../write-translations');
const metadataUtils = require('./metadataUtils');
const blog = require('./blog');
@ -355,7 +355,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 === '/'
@ -374,7 +374,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);

View file

@ -23,12 +23,13 @@ function startLiveReloadServer() {
function startServer() {
const initialServerPort =
parseInt(program.port, 10) || process.env.PORT || 3000;
const host = program.host || 'localhost';
const promise = portFinder
.getPortPromise({port: initialServerPort})
.then(port => {
server(port);
server(port, host);
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);
openBrowser(serverAddress);
});