Fix: --host flag logs when no network IPs are found (#3547)

* feat: add fallback log if no network interfaces found

* fix: extra newline on missing network log

* chore: changeset
This commit is contained in:
Ben Holmes 2022-06-07 17:12:35 -04:00 committed by GitHub
parent 68caa71b0f
commit a83d581714
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 23 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix: show "unable to find network to expose" with local network log when using --host without suitable networks

View file

@ -76,31 +76,29 @@ export function devStart({
const networkLogging = getNetworkLogging(config.server.host); const networkLogging = getNetworkLogging(config.server.host);
const toDisplayUrl = (hostname: string) => const toDisplayUrl = (hostname: string) =>
`${https ? 'https' : 'http'}://${hostname}:${port}${rootPath}`; `${https ? 'https' : 'http'}://${hostname}:${port}${rootPath}`;
let addresses = [];
if (networkLogging === 'none') { let local = `${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`;
addresses = [`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`]; let network = null;
} else if (networkLogging === 'host-to-expose') {
addresses = [ if (networkLogging === 'host-to-expose') {
`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`, network = `${networkPrefix}${dim('use --host to expose')}`;
`${networkPrefix}${dim('use --host to expose')}`, } else if (networkLogging === 'visible') {
]; const ipv4Networks = Object.values(os.networkInterfaces())
} else {
addresses = Object.values(os.networkInterfaces())
.flatMap((networkInterface) => networkInterface ?? []) .flatMap((networkInterface) => networkInterface ?? [])
.filter( .filter(
(networkInterface) => networkInterface?.address && networkInterface?.family === 'IPv4' (networkInterface) => networkInterface?.address && networkInterface?.family === 'IPv4'
) );
.map(({ address }) => { for (let { address } of ipv4Networks) {
if (address.includes('127.0.0.1')) { if (address.includes('127.0.0.1')) {
const displayAddress = address.replace('127.0.0.1', localAddress); const displayAddress = address.replace('127.0.0.1', localAddress);
return `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`; local = `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`;
} else { } else {
return `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`; network = `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`;
}
}
if (!network) {
network = `${networkPrefix}${dim('unable to find network to expose')}`;
} }
})
// ensure Local logs come before Network
.sort((msg) => (msg.startsWith(localPrefix) ? -1 : 1));
} }
const messages = [ const messages = [
@ -108,10 +106,11 @@ export function devStart({
`started in ${Math.round(startupTime)}ms` `started in ${Math.round(startupTime)}ms`
)}`, )}`,
'', '',
...addresses, local,
network,
'', '',
]; ];
return messages.map((msg) => ` ${msg}`).join('\n'); return messages.filter((msg) => typeof msg === 'string').map((msg) => ` ${msg}`).join('\n');
} }
export function telemetryNotice() { export function telemetryNotice() {