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 = {
getConfigUrls,
getAbsoluteAdminUrl: getAbsoluteUrl('admin'),
getAbsoluteServerUrl: getAbsoluteUrl('server'),
};

View File

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

View File

@ -9,7 +9,7 @@ const Router = require('koa-router');
const _ = require('lodash');
const chalk = require('chalk');
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 utils = require('./utils');
@ -98,14 +98,6 @@ class Strapi {
logFirstStartupMessage() {
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.grey('Create your first administrator 💻 by going to the administration panel at:')
@ -113,13 +105,10 @@ class Strapi {
console.log();
const addressTable = new CLITable();
if (this.config.admin.url.startsWith('http')) {
addressTable.push([chalk.bold(this.config.admin.url)]);
} else {
addressTable.push([
chalk.bold(`http://${hostname}:${strapi.config.port}${this.config.admin.url}`),
]);
}
const adminUrl = getAbsoluteAdminUrl(strapi.config);
addressTable.push([chalk.bold(adminUrl)]);
console.log(`${addressTable.toString()}`);
console.log();
}
@ -127,32 +116,18 @@ class Strapi {
logStartupMessage() {
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!'));
if (this.config.serveAdminPanel === true) {
console.log(chalk.grey('To manage your project 🚀, go to the administration panel at:'));
if (this.config.admin.url.startsWith('http')) {
console.log(chalk.bold(this.config.admin.url));
} else {
console.log(chalk.bold(`http://${hostname}:${strapi.config.port}${this.config.admin.url}`));
}
const adminUrl = getAbsoluteAdminUrl(strapi.config);
console.log(chalk.bold(adminUrl));
console.log();
}
console.log(chalk.grey('To access the server ⚡️, go to:'));
if (this.config.admin.url.startsWith('http')) {
console.log(chalk.bold(this.config.server.url));
} else {
console.log(chalk.bold(`http://${hostname}:${strapi.config.port}${this.config.server.url}`));
}
const serverUrl = getAbsoluteServerUrl(strapi.config);
console.log(chalk.bold(serverUrl));
console.log();
}

View File

@ -12,6 +12,7 @@ var chalk = require('chalk');
var spawn = require('cross-spawn');
var opn = require('opn');
const fetch = require('node-fetch');
const { getAbsoluteAdminUrl } = require('strapi-utils');
// https://github.com/sindresorhus/opn#app
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.
*/
async function openBrowser() {
let url = this.config.admin.url;
if (!url.startsWith('http')) {
url = `http://${strapi.config.host}:${strapi.config.port}${this.config.admin.url}`;
}
const url = getAbsoluteAdminUrl(strapi.config);
// Ping the dashboard to ensure it's available.
await pingDashboard.call(this, url);