Skip to content

Commit

Permalink
Fix: --host flag logs when no network IPs are found (#3547)
Browse files Browse the repository at this point in the history
* feat: add fallback log if no network interfaces found

* fix: extra newline on missing network log

* chore: changeset
  • Loading branch information
bholmesdev authored Jun 7, 2022
1 parent 68caa71 commit a83d581
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-papayas-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix: show "unable to find network to expose" with local network log when using --host without suitable networks
47 changes: 23 additions & 24 deletions packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,42 +76,41 @@ export function devStart({
const networkLogging = getNetworkLogging(config.server.host);
const toDisplayUrl = (hostname: string) =>
`${https ? 'https' : 'http'}://${hostname}:${port}${rootPath}`;
let addresses = [];

if (networkLogging === 'none') {
addresses = [`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`];
} else if (networkLogging === 'host-to-expose') {
addresses = [
`${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`,
`${networkPrefix}${dim('use --host to expose')}`,
];
} else {
addresses = Object.values(os.networkInterfaces())

let local = `${localPrefix}${bold(cyan(toDisplayUrl(localAddress)))}`;
let network = null;

if (networkLogging === 'host-to-expose') {
network = `${networkPrefix}${dim('use --host to expose')}`;
} else if (networkLogging === 'visible') {
const ipv4Networks = Object.values(os.networkInterfaces())
.flatMap((networkInterface) => networkInterface ?? [])
.filter(
(networkInterface) => networkInterface?.address && networkInterface?.family === 'IPv4'
)
.map(({ address }) => {
if (address.includes('127.0.0.1')) {
const displayAddress = address.replace('127.0.0.1', localAddress);
return `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`;
} else {
return `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`;
}
})
// ensure Local logs come before Network
.sort((msg) => (msg.startsWith(localPrefix) ? -1 : 1));
);
for (let { address } of ipv4Networks) {
if (address.includes('127.0.0.1')) {
const displayAddress = address.replace('127.0.0.1', localAddress);
local = `${localPrefix}${bold(cyan(toDisplayUrl(displayAddress)))}`;
} else {
network = `${networkPrefix}${bold(cyan(toDisplayUrl(address)))}`;
}
}
if (!network) {
network = `${networkPrefix}${dim('unable to find network to expose')}`;
}
}

const messages = [
`${emoji('🚀 ', '')}${bgGreen(black(` astro `))} ${green(`v${version}`)} ${dim(
`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() {
Expand Down

0 comments on commit a83d581

Please sign in to comment.