Skip to content

Commit

Permalink
feat: print public API to make tunnel experience easier
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Oct 27, 2023
1 parent 47d176a commit 7d69362
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 9 deletions.
24 changes: 21 additions & 3 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ cli.command(
console.log(yellow('\n --remote is required for tunneling, localtunnel is not enabled.\n'))
}

lastRemoteUrl = printInfo(options, port, remote, tunnelUrl)
let publicIp: string | undefined
if (remote)
publicIp = await import('public-ip').then(r => r.publicIpv4())

lastRemoteUrl = printInfo(options, port, remote, tunnelUrl, publicIp)
}

async function openTunnel(port: number) {
Expand Down Expand Up @@ -214,10 +218,13 @@ cli.command(
if (!lastRemoteUrl)
return
await import('uqr')
.then((r) => {
.then(async (r) => {
const code = r.renderUnicodeCompact(lastRemoteUrl!)
console.log(`\n${dim(' QR Code for remote control: ')}\n ${blue(lastRemoteUrl!)}\n`)
console.log(code.split('\n').map(i => ` ${i}`).join('\n'))
const publicIp = await import('public-ip').then(r => r.publicIpv4())
if (publicIp)
console.log(`\n${dim(' Public IP: ')} ${blue(publicIp)}\n`)
})
},
},
Expand Down Expand Up @@ -524,7 +531,13 @@ function exportOptions<T>(args: Argv<T>) {
})
}

function printInfo(options: ResolvedSlidevOptions, port?: number, remote?: string, tunnelUrl?: string) {
function printInfo(
options: ResolvedSlidevOptions,
port?: number,
remote?: string,
tunnelUrl?: string,
publicIp?: string,
) {
console.log()
console.log()
console.log(` ${cyan('●') + blue('■') + yellow('▲')}`)
Expand Down Expand Up @@ -560,6 +573,11 @@ function printInfo(options: ResolvedSlidevOptions, port?: number, remote?: strin
console.log(`${dim(' remote control ')} > ${blue(lastRemoteUrl)}`)
}))

if (publicIp) {
lastRemoteUrl = `http://${publicIp}:${port}${entryPath}`
console.log(`${dim(' remote control ')} > ${blue(lastRemoteUrl)}`)
}

if (tunnelUrl) {
lastRemoteUrl = `${tunnelUrl}${entryPath}`
console.log(`${dim(' remote via tunnel')} > ${yellow(lastRemoteUrl)}`)
Expand Down
1 change: 1 addition & 0 deletions packages/slidev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"postcss-nested": "^6.0.1",
"prismjs": "^1.29.0",
"prompts": "^2.4.2",
"public-ip": "^6.0.1",
"resolve": "^1.22.8",
"resolve-from": "^5.0.0",
"resolve-global": "^1.0.0",
Expand Down
Loading

0 comments on commit 7d69362

Please sign in to comment.