openbrowser on localhost instead of 0.0.0.0 (#6391)

Signed-off-by: Pierre Noël <petersg83@gmail.com>

Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
This commit is contained in:
Pierre Noël 2020-05-28 11:17:59 +02:00 committed by GitHub
parent 344a50944e
commit 4a6ef41201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 39 deletions

View File

@ -57,6 +57,21 @@ const getConfigUrls = (serverConfig, forAdminBuild = false) => {
}; };
}; };
const getAbsoluteUrl = adminOrServer => config => {
if (config[adminOrServer].url.startsWith('http')) {
return config[adminOrServer].url;
}
let hostname =
config.environment === 'development' && ['127.0.0.1', '0.0.0.0'].includes(config.host)
? 'localhost'
: config.host;
return `http://${hostname}:${config.port}${config[adminOrServer].url}`;
};
module.exports = { module.exports = {
getConfigUrls, getConfigUrls,
getAbsoluteAdminUrl: getAbsoluteUrl('admin'),
getAbsoluteServerUrl: getAbsoluteUrl('server'),
}; };

View File

@ -22,7 +22,7 @@ const {
escapeQuery, escapeQuery,
} = require('./stringFormatting'); } = require('./stringFormatting');
const { removeUndefined } = require('./objectFormatting'); const { removeUndefined } = require('./objectFormatting');
const { getConfigUrls } = require('./config'); const { getConfigUrls, getAbsoluteAdminUrl, getAbsoluteServerUrl } = require('./config');
module.exports = { module.exports = {
yup, yup,
@ -43,4 +43,6 @@ module.exports = {
getConfigUrls, getConfigUrls,
escapeQuery, escapeQuery,
removeUndefined, removeUndefined,
getAbsoluteAdminUrl,
getAbsoluteServerUrl,
}; };

View File

@ -9,7 +9,7 @@ const Router = require('koa-router');
const _ = require('lodash'); const _ = require('lodash');
const chalk = require('chalk'); const chalk = require('chalk');
const CLITable = require('cli-table3'); const CLITable = require('cli-table3');
const { logger, models } = require('strapi-utils'); const { logger, models, getAbsoluteAdminUrl, getAbsoluteServerUrl } = require('strapi-utils');
const { createDatabaseManager } = require('strapi-database'); const { createDatabaseManager } = require('strapi-database');
const utils = require('./utils'); const utils = require('./utils');
@ -98,14 +98,6 @@ class Strapi {
logFirstStartupMessage() { logFirstStartupMessage() {
this.logStats(); this.logStats();
let hostname = strapi.config.host;
if (
strapi.config.environment === 'development' &&
['127.0.0.1', '0.0.0.0'].includes(strapi.config.host)
) {
hostname = 'localhost';
}
console.log(chalk.bold('One more thing...')); console.log(chalk.bold('One more thing...'));
console.log( console.log(
chalk.grey('Create your first administrator 💻 by going to the administration panel at:') chalk.grey('Create your first administrator 💻 by going to the administration panel at:')
@ -113,13 +105,10 @@ class Strapi {
console.log(); console.log();
const addressTable = new CLITable(); const addressTable = new CLITable();
if (this.config.admin.url.startsWith('http')) {
addressTable.push([chalk.bold(this.config.admin.url)]); const adminUrl = getAbsoluteAdminUrl(strapi.config);
} else { addressTable.push([chalk.bold(adminUrl)]);
addressTable.push([
chalk.bold(`http://${hostname}:${strapi.config.port}${this.config.admin.url}`),
]);
}
console.log(`${addressTable.toString()}`); console.log(`${addressTable.toString()}`);
console.log(); console.log();
} }
@ -127,32 +116,18 @@ class Strapi {
logStartupMessage() { logStartupMessage() {
this.logStats(); this.logStats();
let hostname = strapi.config.host;
if (
strapi.config.environment === 'development' &&
['127.0.0.1', '0.0.0.0'].includes(strapi.config.host)
) {
hostname = 'localhost';
}
console.log(chalk.bold('Welcome back!')); console.log(chalk.bold('Welcome back!'));
if (this.config.serveAdminPanel === true) { if (this.config.serveAdminPanel === true) {
console.log(chalk.grey('To manage your project 🚀, go to the administration panel at:')); console.log(chalk.grey('To manage your project 🚀, go to the administration panel at:'));
if (this.config.admin.url.startsWith('http')) { const adminUrl = getAbsoluteAdminUrl(strapi.config);
console.log(chalk.bold(this.config.admin.url)); console.log(chalk.bold(adminUrl));
} else {
console.log(chalk.bold(`http://${hostname}:${strapi.config.port}${this.config.admin.url}`));
}
console.log(); console.log();
} }
console.log(chalk.grey('To access the server ⚡️, go to:')); console.log(chalk.grey('To access the server ⚡️, go to:'));
if (this.config.admin.url.startsWith('http')) { const serverUrl = getAbsoluteServerUrl(strapi.config);
console.log(chalk.bold(this.config.server.url)); console.log(chalk.bold(serverUrl));
} else {
console.log(chalk.bold(`http://${hostname}:${strapi.config.port}${this.config.server.url}`));
}
console.log(); console.log();
} }

View File

@ -12,6 +12,7 @@ var chalk = require('chalk');
var spawn = require('cross-spawn'); var spawn = require('cross-spawn');
var opn = require('opn'); var opn = require('opn');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const { getAbsoluteAdminUrl } = require('strapi-utils');
// https://github.com/sindresorhus/opn#app // https://github.com/sindresorhus/opn#app
var OSX_CHROME = 'google chrome'; var OSX_CHROME = 'google chrome';
@ -122,10 +123,7 @@ async function pingDashboard(url, multipleTime = false) {
* true if it opened a browser or ran a node.js script, otherwise false. * true if it opened a browser or ran a node.js script, otherwise false.
*/ */
async function openBrowser() { async function openBrowser() {
let url = this.config.admin.url; const url = getAbsoluteAdminUrl(strapi.config);
if (!url.startsWith('http')) {
url = `http://${strapi.config.host}:${strapi.config.port}${this.config.admin.url}`;
}
// Ping the dashboard to ensure it's available. // Ping the dashboard to ensure it's available.
await pingDashboard.call(this, url); await pingDashboard.call(this, url);