Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to show baseURL in showURL #28

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ export interface ListenOptions {
autoCloseSignals: string[]
}

export interface ShowURLOptions {
baseURL: string
name?: string
}

export interface Listener {
url: string,
server: http.Server | https.Server,
close: () => Promise<void>,
open: () => Promise<void>,
showURL: () => void
showURL: (options?: Pick<ListenOptions, 'baseURL'>) => void
}

export async function listen (handle: http.RequestListener, opts: Partial<ListenOptions> = {}): Promise<Listener> {
Expand Down Expand Up @@ -109,13 +114,15 @@ export async function listen (handle: http.RequestListener, opts: Partial<Listen
await clipboardy.write(url).catch(() => { opts.clipboard = false })
}

const showURL = () => {
const showURL = (options?: ShowURLOptions) => {
const add = opts.clipboard ? gray('(copied to clipboard)') : ''
const lines = []
lines.push(` > Local: ${formatURL(url)} ${add}`)
const baseURL = (options?.baseURL || opts.baseURL || '').slice(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use ufo.withoutLeadingSlash instead of slice to handle cases without leading / (or ufo.joinURL to join :)

const name = options?.name ? ` (${options.name})` : ''
lines.push(` > Local${name}: ${formatURL(url + baseURL)} ${add}`)
if (isExternal) {
for (const ip of getExternalIps()) {
lines.push(` > Network: ${formatURL(url.replace('localhost', ip))}`)
lines.push(` > Network${name}: ${formatURL(url.replace('localhost', ip) + baseURL)}`)
}
}
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -165,7 +172,7 @@ function getExternalIps (): string[] {
for (const details of Object.values(networkInterfaces())) {
if (details) {
for (const d of details) {
if ((d.family === 'IPv4' || d.family === 4) && !d.internal) {
if ((d.family === 'IPv4' || +d.family === 4) && !d.internal) {
ips.add(d.address)
}
}
Expand Down