Skip to content

Commit

Permalink
feat: allow recalling showURL
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 13, 2021
1 parent 9f321ab commit cd00e79
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Expand Up @@ -40,7 +40,8 @@ export interface Listener {
url: string,
server: http.Server | https.Server,
close: () => Promise<void>,
open: () => Promise<void>
open: () => Promise<void>,
showURL: () => void
}

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

if (opts.showURL) {
const showURL = () => {
const add = opts.clipboard ? gray('(copied to clipboard)') : ''
const lines = []
lines.push(` > Local: ${formatURL(url)} ${add}`)
Expand All @@ -116,6 +117,10 @@ export async function listen (handle: http.RequestListener, opts: Partial<Listen
console.log('\n' + lines.join('\n') + '\n')
}

if (opts.showURL) {
showURL()
}

const _open = async () => {
const { default: open } = await import('open')
await open(url).catch(() => { })
Expand All @@ -132,6 +137,7 @@ export async function listen (handle: http.RequestListener, opts: Partial<Listen
url,
server,
open: _open,
showURL,
close
}
}
Expand Down

0 comments on commit cd00e79

Please sign in to comment.