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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): improve output aesthetics #6997

Merged
merged 7 commits into from May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
30 changes: 16 additions & 14 deletions packages/vite/src/node/cli.ts
Expand Up @@ -96,24 +96,26 @@ cli

const info = server.config.logger.info

const { version } = require('vite/package.json')

// @ts-ignore
const viteStartTime = global.__vite_start_time ?? false
const startupDurationString = viteStartTime
? colors.dim(
`ready in ${colors.white(
colors.bold(Math.ceil(performance.now() - viteStartTime))
)} ms`
)
: ''

info(
colors.cyan(`\n vite v${require('vite/package.json').version}`) +
colors.green(` dev server running at:\n`),
{
clear: !server.config.logger.hasWarned
}
`\n ${colors.green(
`${colors.bold('VITE')} v${version}`
)} ${startupDurationString}\n`,
{ clear: !server.config.logger.hasWarned }
)

server.printUrls()

// @ts-ignore
if (global.__vite_start_time) {
// @ts-ignore
const startupDuration = performance.now() - global.__vite_start_time
info(
`\n ${colors.cyan(`ready in ${Math.ceil(startupDuration)}ms.`)}\n`
)
}
} catch (e) {
createLogger(options.logLevel).error(
colors.red(`error when starting dev server:\n${e.stack}`),
Expand Down
38 changes: 29 additions & 9 deletions packages/vite/src/node/logger.ts
Expand Up @@ -171,11 +171,21 @@ function printServerUrls(
base: string,
info: Logger['info']
): void {
const urls: Array<{ label: string; url: string }> = []

if (hostname.host === '127.0.0.1') {
const url = `${protocol}://${hostname.name}:${colors.bold(port)}${base}`
info(` > Local: ${colors.cyan(url)}`)
urls.push({
label: 'Local',
url: colors.cyan(
`${protocol}://${hostname.name}:${colors.bold(port)}${base}`
)
})

if (hostname.name !== '127.0.0.1') {
info(` > Network: ${colors.dim('use `--host` to expose')}`)
urls.push({
label: 'Network',
url: colors.dim(`use ${colors.white(colors.bold('--host'))} to expose`)
})
}
} else {
Object.values(os.networkInterfaces())
Expand All @@ -189,14 +199,24 @@ function printServerUrls(
// Node >= v18
(typeof detail.family === 'number' && detail.family === 4))
)
.map((detail) => {
const type = detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: '
.forEach((detail) => {
const host = detail.address.replace('127.0.0.1', hostname.name)
const url = `${protocol}://${host}:${colors.bold(port)}${base}`
return ` > ${type} ${colors.cyan(url)}`
const label = detail.address.includes('127.0.0.1') ? 'Local' : 'Network'

urls.push({ label, url: colors.cyan(url) })
})
.forEach((msg) => info(msg))
}

const length = urls.reduce(
(length, { label }) => Math.max(length, label.length),
0
)
urls.forEach(({ label, url: text }) => {
info(
` ${colors.green('➜')} ${colors.bold(label)}: ${' '.repeat(
Copy link
Contributor

Choose a reason for hiding this comment

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

Love the design. I prefer 2 spaces here like in No.3 design since I think the extra 2 spaces don't provide special meaning here.

Copy link
Member

Choose a reason for hiding this comment

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

I also prefer 2 spaces here. It is just a detail but aligning with the start of VITE feels nicer.

length - label.length
)}${text}`
)
})
}