Skip to content

Commit ab4e0c6

Browse files
authored
feat: use clack to add more ✨ (#1121)
1 parent 1b26dec commit ab4e0c6

File tree

30 files changed

+542
-312
lines changed

30 files changed

+542
-312
lines changed

knip.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"confbox",
3535
"consola",
3636
"copy-paste",
37+
"debug",
3738
"defu",
3839
"exsolve",
3940
"fuse.js",

packages/create-nuxt/src/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { provider } from 'std-env'
44

55
import init from '../../nuxi/src/commands/init'
66
import { setupInitCompletions } from '../../nuxi/src/completions-init'
7-
import { setupGlobalConsole } from '../../nuxi/src/utils/console'
87
import { checkEngines } from '../../nuxi/src/utils/engines'
98
import { logger } from '../../nuxi/src/utils/logger'
109
import { description, name, version } from '../package.json'
@@ -22,8 +21,6 @@ const _main = defineCommand({
2221
return
2322
}
2423

25-
setupGlobalConsole({ dev: false })
26-
2724
// Check Node.js version and CLI updates in background
2825
if (provider !== 'stackblitz') {
2926
await checkEngines().catch(err => logger.error(err))

packages/nuxi/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@
3333
},
3434
"devDependencies": {
3535
"@bomb.sh/tab": "^0.0.9",
36-
"@clack/prompts": "^1.0.0-alpha.6",
36+
"@clack/prompts": "1.0.0-alpha.6",
3737
"@nuxt/kit": "^4.2.0",
3838
"@nuxt/schema": "^4.2.0",
3939
"@nuxt/test-utils": "^3.20.1",
4040
"@types/copy-paste": "^2.1.0",
41+
"@types/debug": "^4.1.12",
4142
"@types/node": "^24.10.0",
4243
"@types/semver": "^7.7.1",
4344
"c12": "^3.3.1",
4445
"citty": "^0.1.6",
4546
"confbox": "^0.2.2",
4647
"consola": "^3.4.2",
4748
"copy-paste": "^2.2.0",
49+
"debug": "^4.4.3",
4850
"defu": "^6.1.4",
4951
"exsolve": "^1.0.7",
5052
"fuse.js": "^7.1.0",

packages/nuxi/src/commands/add.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { existsSync, promises as fsp } from 'node:fs'
22
import process from 'node:process'
33

4+
import { cancel, intro, outro } from '@clack/prompts'
45
import { defineCommand } from 'citty'
6+
import { colors } from 'consola/utils'
57
import { dirname, extname, resolve } from 'pathe'
68

79
import { loadKit } from '../utils/kit'
810
import { logger } from '../utils/logger'
11+
import { relativeToProcess } from '../utils/paths'
912
import { templates } from '../utils/templates'
1013
import { cwdArgs, logLevelArgs } from './_shared'
1114

@@ -39,15 +42,16 @@ export default defineCommand({
3942
async run(ctx) {
4043
const cwd = resolve(ctx.args.cwd)
4144

45+
intro(colors.cyan('Adding template...'))
46+
4247
const templateName = ctx.args.template
4348

4449
// Validate template name
4550
if (!templateNames.includes(templateName)) {
46-
logger.error(
47-
`Template ${templateName} is not supported. Possible values: ${Object.keys(
48-
templates,
49-
).join(', ')}`,
50-
)
51+
const templateNames = Object.keys(templates).map(name => colors.cyan(name))
52+
const lastTemplateName = templateNames.pop()
53+
logger.error(`Template ${colors.cyan(templateName)} is not supported.`)
54+
logger.info(`Possible values are ${templateNames.join(', ')} or ${lastTemplateName}.`)
5155
process.exit(1)
5256
}
5357

@@ -59,7 +63,7 @@ export default defineCommand({
5963
: ctx.args.name
6064

6165
if (!name) {
62-
logger.error('name argument is missing!')
66+
cancel('name argument is missing!')
6367
process.exit(1)
6468
}
6569

@@ -74,16 +78,15 @@ export default defineCommand({
7478

7579
// Ensure not overriding user code
7680
if (!ctx.args.force && existsSync(res.path)) {
77-
logger.error(
78-
`File exists: ${res.path} . Use --force to override or use a different name.`,
79-
)
81+
logger.error(`File exists at ${colors.cyan(relativeToProcess(res.path))}.`)
82+
logger.info(`Use ${colors.cyan('--force')} to override or use a different name.`)
8083
process.exit(1)
8184
}
8285

8386
// Ensure parent directory exists
8487
const parentDir = dirname(res.path)
8588
if (!existsSync(parentDir)) {
86-
logger.info('Creating directory', parentDir)
89+
logger.step(`Creating directory ${colors.cyan(relativeToProcess(parentDir))}.`)
8790
if (templateName === 'page') {
8891
logger.info('This enables vue-router functionality!')
8992
}
@@ -92,6 +95,7 @@ export default defineCommand({
9295

9396
// Write file
9497
await fsp.writeFile(res.path, `${res.contents.trim()}\n`)
95-
logger.info(`🪄 Generated a new ${templateName} in ${res.path}`)
98+
logger.success(`Created ${colors.cyan(relativeToProcess(res.path))}.`)
99+
outro(`Generated a new ${colors.cyan(templateName)}!`)
96100
},
97101
})

packages/nuxi/src/commands/analyze.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import type { NuxtAnalyzeMeta } from '@nuxt/schema'
33
import { promises as fsp } from 'node:fs'
44
import process from 'node:process'
55

6+
import { intro, note, outro, taskLog } from '@clack/prompts'
67
import { defineCommand } from 'citty'
8+
import { colors } from 'consola/utils'
79
import { defu } from 'defu'
810
import { H3, lazyEventHandler } from 'h3-next'
911
import { join, resolve } from 'pathe'
@@ -13,6 +15,7 @@ import { overrideEnv } from '../utils/env'
1315
import { clearDir } from '../utils/fs'
1416
import { loadKit } from '../utils/kit'
1517
import { logger } from '../utils/logger'
18+
import { relativeToProcess } from '../utils/paths'
1619
import { cwdArgs, dotEnvArgs, extendsArgs, legacyRootDirArgs, logLevelArgs } from './_shared'
1720

1821
const indexHtml = `
@@ -65,6 +68,8 @@ export default defineCommand({
6568
const name = ctx.args.name || 'default'
6669
const slug = name.trim().replace(/[^\w-]/g, '_')
6770

71+
intro(colors.cyan('Analyzing bundle size...'))
72+
6873
const startTime = Date.now()
6974

7075
const { loadNuxt, buildNuxt } = await loadKit(cwd)
@@ -105,8 +110,17 @@ export default defineCommand({
105110
filename: join(analyzeDir, 'client.html'),
106111
})
107112

113+
const tasklog = taskLog({
114+
title: 'Building Nuxt with analysis enabled',
115+
retainLog: false,
116+
limit: 1,
117+
})
118+
119+
tasklog.message('Clearing analyze directory...')
108120
await clearDir(analyzeDir)
121+
tasklog.message('Building Nuxt...')
109122
await buildNuxt(nuxt)
123+
tasklog.success('Build complete')
110124

111125
const endTime = Date.now()
112126

@@ -121,14 +135,9 @@ export default defineCommand({
121135
}
122136

123137
await nuxt.callHook('build:analyze:done', meta)
124-
await fsp.writeFile(
125-
join(analyzeDir, 'meta.json'),
126-
JSON.stringify(meta, null, 2),
127-
'utf-8',
128-
)
138+
await fsp.writeFile(join(analyzeDir, 'meta.json'), JSON.stringify(meta, null, 2), 'utf-8')
129139

130-
logger.info(`Analyze results are available at: \`${analyzeDir}\``)
131-
logger.warn('Do not deploy analyze results! Use `nuxi build` before deploying.')
140+
note(`${relativeToProcess(analyzeDir)}\n\nDo not deploy analyze results! Use ${colors.cyan('nuxt build')} before deploying.`, 'Build location')
132141

133142
if (ctx.args.serve !== false && !process.env.CI) {
134143
const app = new H3()
@@ -139,13 +148,16 @@ export default defineCommand({
139148
return () => new Response(contents, opts)
140149
})
141150

142-
logger.info('Starting stats server...')
151+
logger.step('Starting stats server...')
143152

144153
app.use('/client', serveFile(join(analyzeDir, 'client.html')))
145154
app.use('/nitro', serveFile(join(analyzeDir, 'nitro.html')))
146155
app.use(() => new Response(indexHtml, opts))
147156

148157
await serve(app).serve()
149158
}
159+
else {
160+
outro('✨ Analysis build complete!')
161+
}
150162
},
151163
})

packages/nuxi/src/commands/build.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import type { Nitro } from 'nitropack'
22

33
import process from 'node:process'
44

5+
import { intro, outro } from '@clack/prompts'
56
import { defineCommand } from 'citty'
7+
import { colors } from 'consola/utils'
68
import { relative, resolve } from 'pathe'
79

810
import { showVersions } from '../utils/banner'
@@ -38,6 +40,8 @@ export default defineCommand({
3840

3941
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
4042

43+
intro(colors.cyan('Building Nuxt for production...'))
44+
4145
const kit = await loadKit(cwd)
4246

4347
await showVersions(cwd, kit)
@@ -67,7 +71,7 @@ export default defineCommand({
6771
// Use ? for backward compatibility for Nuxt <= RC.10
6872
nitro = kit.useNitro?.()
6973
if (nitro) {
70-
logger.info(`Building for Nitro preset: \`${nitro.options.preset}\``)
74+
logger.info(`Nitro preset: ${colors.cyan(nitro.options.preset)}`)
7175
}
7276
}
7377
catch {
@@ -79,24 +83,24 @@ export default defineCommand({
7983
await kit.writeTypes(nuxt)
8084

8185
nuxt.hook('build:error', (err) => {
82-
logger.error('Nuxt Build Error:', err)
86+
logger.error(`Nuxt build error: ${err}`)
8387
process.exit(1)
8488
})
8589

8690
await kit.buildNuxt(nuxt)
8791

8892
if (ctx.args.prerender) {
8993
if (!nuxt.options.ssr) {
90-
logger.warn(
91-
'HTML content not prerendered because `ssr: false` was set. You can read more in `https://nuxt.com/docs/getting-started/deployment#static-hosting`.',
92-
)
94+
logger.warn(`HTML content not prerendered because ${colors.cyan('ssr: false')} was set.`)
95+
logger.info(`You can read more in ${colors.cyan('https://nuxt.com/docs/getting-started/deployment#static-hosting')}.`)
9396
}
9497
// TODO: revisit later if/when nuxt build --prerender will output hybrid
9598
const dir = nitro?.options.output.publicDir
9699
const publicDir = dir ? relative(process.cwd(), dir) : '.output/public'
97-
logger.success(
98-
`You can now deploy \`${publicDir}\` to any static hosting!`,
99-
)
100+
outro(`✨ You can now deploy ${colors.cyan(publicDir)} to any static hosting!`)
101+
}
102+
else {
103+
outro('✨ Build complete!')
100104
}
101105
},
102106
})

packages/nuxi/src/commands/cleanup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineCommand } from 'citty'
22
import { resolve } from 'pathe'
33

44
import { loadKit } from '../utils/kit'
5+
import { logger } from '../utils/logger'
56
import { cleanupNuxtDirs } from '../utils/nuxt'
67
import { cwdArgs, legacyRootDirArgs } from './_shared'
78

@@ -19,5 +20,7 @@ export default defineCommand({
1920
const { loadNuxtConfig } = await loadKit(cwd)
2021
const nuxtOptions = await loadNuxtConfig({ cwd, overrides: { dev: true } })
2122
await cleanupNuxtDirs(nuxtOptions.rootDir, nuxtOptions.buildDir)
23+
24+
logger.success('Cleanup complete!')
2225
},
2326
})

packages/nuxi/src/commands/dev.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { NuxtDevContext } from '../dev/utils'
44
import process from 'node:process'
55

66
import { defineCommand } from 'citty'
7+
import { colors } from 'consola/utils'
78
import { getArgs as getListhenArgs } from 'listhen/cli'
89
import { resolve } from 'pathe'
910
import { satisfies } from 'semver'
@@ -12,7 +13,7 @@ import { isBun, isTest } from 'std-env'
1213
import { initialize } from '../dev'
1314
import { ForkPool } from '../dev/pool'
1415
import { overrideEnv } from '../utils/env'
15-
import { logger } from '../utils/logger'
16+
import { debug, logger } from '../utils/logger'
1617
import { cwdArgs, dotEnvArgs, envNameArgs, extendsArgs, legacyRootDirArgs, logLevelArgs } from './_shared'
1718

1819
const startTime: number | undefined = Date.now()
@@ -106,7 +107,7 @@ const command = defineCommand({
106107
onReady((_address) => {
107108
pool.startWarming()
108109
if (startTime) {
109-
logger.debug(`Dev server ready for connections in ${Date.now() - startTime}ms`)
110+
debug(`Dev server ready for connections in ${Date.now() - startTime}ms`)
110111
}
111112
})
112113

@@ -124,15 +125,15 @@ const command = defineCommand({
124125
// Handle IPC messages from the fork
125126
if (message.type === 'nuxt:internal:dev:ready') {
126127
if (startTime) {
127-
logger.debug(`Dev server ready for connections in ${Date.now() - startTime}ms`)
128+
debug(`Dev server ready for connections in ${Date.now() - startTime}ms`)
128129
}
129130
}
130131
else if (message.type === 'nuxt:internal:dev:restart') {
131132
// Fork is requesting another restart
132133
void restartWithFork()
133134
}
134135
else if (message.type === 'nuxt:internal:dev:rejection') {
135-
logger.info(`Restarting Nuxt due to error: \`${message.message}\``)
136+
logger.info(`Restarting Nuxt due to error: ${colors.cyan(message.message)}`)
136137
void restartWithFork()
137138
}
138139
})

packages/nuxi/src/commands/devtools.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import process from 'node:process'
22

33
import { defineCommand } from 'citty'
4+
import { colors } from 'consola/utils'
45
import { resolve } from 'pathe'
56
import { x } from 'tinyexec'
67

@@ -25,7 +26,7 @@ export default defineCommand({
2526
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
2627

2728
if (!['enable', 'disable'].includes(ctx.args.command)) {
28-
logger.error(`Unknown command \`${ctx.args.command}\`.`)
29+
logger.error(`Unknown command ${colors.cyan(ctx.args.command)}.`)
2930
process.exit(1)
3031
}
3132

0 commit comments

Comments
 (0)