Skip to content

Commit 9ff68d9

Browse files
committed
fix(init): print cd step for single-character project directories
1 parent f6e9235 commit 9ff68d9

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

packages/nuxt-cli/src/commands/init.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ async function reportMissingNonInteractiveArgs<T extends ArgsDef>(
8585
.join(', ')}`)
8686
}
8787

88+
/**
89+
* Commands to print in the closing 'Next steps' box, in the order to run them.
90+
*
91+
* `dir` is relative to the current working directory, so `.` means the project
92+
* was created in place and there is nowhere to `cd` to.
93+
*/
94+
export function getNextSteps(options: {
95+
dir: string
96+
shell: boolean
97+
installFailure?: unknown
98+
recoveryCommands: string[]
99+
packageManager: PackageManagerName
100+
}): string[] {
101+
const { dir, shell, installFailure, recoveryCommands, packageManager } = options
102+
const runCmd = packageManager === 'deno' ? 'task' : 'run'
103+
return [
104+
!shell && dir !== '.' && `cd ${dir}`,
105+
installFailure && `${packageManager} install`,
106+
...recoveryCommands,
107+
`${packageManager} ${runCmd} dev`,
108+
].filter((step): step is string => typeof step === 'string')
109+
}
110+
88111
export default defineCommand({
89112
meta: {
90113
name: 'init',
@@ -678,16 +701,13 @@ export default defineCommand({
678701
}
679702

680703
// Display next steps
681-
const relativeTemplateDir = relative(process.cwd(), template.dir) || '.'
682-
const runCmd = selectedPackageManager === 'deno' ? 'task' : 'run'
683-
const nextSteps = [
684-
!ctx.args.shell
685-
&& relativeTemplateDir.length > 1
686-
&& colors.cyan(`cd ${relativeTemplateDir}`),
687-
installFailure && colors.cyan(`${selectedPackageManager} install`),
688-
...recoveryCommands.map(command => colors.cyan(command)),
689-
colors.cyan(`${selectedPackageManager} ${runCmd} dev`),
690-
].filter(Boolean)
704+
const nextSteps = getNextSteps({
705+
dir: relative(process.cwd(), template.dir) || '.',
706+
shell: !!ctx.args.shell,
707+
installFailure,
708+
recoveryCommands,
709+
packageManager: selectedPackageManager,
710+
}).map(step => colors.cyan(step))
691711

692712
logger.message()
693713
writeWithGuide(output => box(`\n${nextSteps.map(step => ` › ${step}`).join('\n')}\n`, ` 👉 Next steps `, {

packages/nuxt-cli/test/unit/commands/init.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,41 @@ import { tmpdir } from 'node:os'
33
import { join } from 'node:path'
44
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
55

6-
import { detectTemplatePackageManager } from '../../../src/commands/init'
6+
import { detectTemplatePackageManager, getNextSteps } from '../../../src/commands/init'
7+
8+
describe('getNextSteps', () => {
9+
const base = { shell: false, recoveryCommands: [] as string[], packageManager: 'npm' as const }
10+
11+
it('should tell the user to change into the project directory', () => {
12+
expect(getNextSteps({ ...base, dir: 'my-app' })).toEqual(['cd my-app', 'npm run dev'])
13+
})
14+
15+
it('should tell the user to change into a single-character directory', () => {
16+
expect(getNextSteps({ ...base, dir: 'a' })).toEqual(['cd a', 'npm run dev'])
17+
})
18+
19+
it('should omit the change of directory when the project was created in place', () => {
20+
expect(getNextSteps({ ...base, dir: '.' })).toEqual(['npm run dev'])
21+
})
22+
23+
it('should omit the change of directory when a shell is launched', () => {
24+
expect(getNextSteps({ ...base, dir: 'my-app', shell: true })).toEqual(['npm run dev'])
25+
})
26+
27+
it('should ask for a retried install before recovery commands', () => {
28+
expect(getNextSteps({
29+
...base,
30+
dir: 'my-app',
31+
installFailure: { error: new Error('offline') },
32+
recoveryCommands: ['pnpm approve-builds'],
33+
packageManager: 'pnpm',
34+
})).toEqual(['cd my-app', 'pnpm install', 'pnpm approve-builds', 'pnpm run dev'])
35+
})
36+
37+
it('should use `task` for deno', () => {
38+
expect(getNextSteps({ ...base, dir: 'my-app', packageManager: 'deno' })).toEqual(['cd my-app', 'deno task dev'])
39+
})
40+
})
741

842
describe('detectTemplatePackageManager', () => {
943
let dir: string

0 commit comments

Comments
 (0)