Skip to content

Commit fb98de6

Browse files
committed
fix: handle Ctrl-C more consistently
1 parent 2a66eb7 commit fb98de6

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

packages/nuxi/src/commands/init.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ export default defineCommand({
9797
placeholder: './nuxt-app',
9898
type: 'text',
9999
default: 'nuxt-app',
100-
})
100+
cancel: 'reject',
101+
}).catch(() => process.exit(1))
101102
}
102103

103104
const cwd = resolve(ctx.args.cwd)
@@ -132,18 +133,15 @@ export default defineCommand({
132133
break
133134

134135
case 'Select different directory': {
135-
const dir = await logger.prompt('Please specify a different directory:', {
136+
templateDownloadPath = resolve(cwd, await logger.prompt('Please specify a different directory:', {
136137
type: 'text',
137-
})
138-
if (dir && typeof dir === 'string') {
139-
templateDownloadPath = resolve(cwd, dir)
140-
}
138+
cancel: 'reject',
139+
}).catch(() => process.exit(1)))
141140
break
142141
}
143142

144143
// 'Abort' or Ctrl+C
145144
default:
146-
logger.info('Initialization aborted.')
147145
process.exit(1)
148146
}
149147
}
@@ -175,12 +173,8 @@ export default defineCommand({
175173
: await logger.prompt('Which package manager would you like to use?', {
176174
type: 'select',
177175
options: packageManagerOptions,
178-
})
179-
180-
if (!packageManagerOptions.includes(selectedPackageManager)) {
181-
logger.error('Invalid package manager selected.')
182-
process.exit(1)
183-
}
176+
cancel: 'reject',
177+
}).catch(() => process.exit(1))
184178

185179
// Install project dependencies
186180
// or skip installation based on the '--no-install' flag
@@ -213,7 +207,8 @@ export default defineCommand({
213207
if (ctx.args.gitInit === undefined) {
214208
ctx.args.gitInit = await logger.prompt('Initialize git repository?', {
215209
type: 'confirm',
216-
}) === true
210+
cancel: 'reject',
211+
}).catch(() => process.exit(1))
217212
}
218213
if (ctx.args.gitInit) {
219214
logger.info('Initializing git repository...\n')

packages/nuxi/src/commands/module/add.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export default defineCommand({
7373
{
7474
type: 'confirm',
7575
initial: false,
76+
cancel: 'default',
7677
},
7778
)
7879
if (shouldContinue !== true) {
@@ -141,6 +142,7 @@ async function addModules(modules: ResolvedModule[], { skipInstall, skipConfig,
141142
return logger.prompt(`Install failed for \`${failedModulesList}\`. Do you want to continue adding the module${s} to ${colors.cyan('nuxt.config')}?`, {
142143
type: 'confirm',
143144
initial: false,
145+
cancel: 'default',
144146
})
145147
},
146148
)
@@ -246,9 +248,10 @@ async function resolveModule(moduleName: string, cwd: string): Promise<ModuleRes
246248
{
247249
type: 'confirm',
248250
initial: false,
251+
cancel: 'default',
249252
},
250253
)
251-
if (shouldContinue !== true) {
254+
if (!shouldContinue) {
252255
return false
253256
}
254257
}
@@ -268,8 +271,9 @@ async function resolveModule(moduleName: string, cwd: string): Promise<ModuleRes
268271
pkgVersion = await logger.prompt('Choose a version:', {
269272
type: 'select',
270273
options: [_moduleVersion, pkgVersion],
274+
cancel: 'undefined',
271275
})
272-
if (typeof pkgVersion !== 'string') {
276+
if (!pkgVersion) {
273277
return false
274278
}
275279
}
@@ -313,9 +317,10 @@ async function resolveModule(moduleName: string, cwd: string): Promise<ModuleRes
313317
{
314318
type: 'confirm',
315319
initial: false,
320+
cancel: 'default',
316321
},
317322
)
318-
if (shouldContinue !== true) {
323+
if (!shouldContinue) {
319324
return false
320325
}
321326
}

packages/nuxi/src/commands/upgrade.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ const nuxtVersionTags = {
4444
}
4545

4646
async function getNightlyVersion(packageNames: string[]): Promise<{ npmPackages: string[], nuxtVersion: string }> {
47-
const result = await logger.prompt(
47+
const nuxtVersion = await logger.prompt(
4848
'Which nightly Nuxt release channel do you want to install? (3.x or 4.x)',
4949
{
5050
type: 'select',
5151
options: ['3.x', '4.x'] as const,
5252
default: '3.x',
53+
cancel: 'reject',
5354
},
54-
)
55-
56-
const nuxtVersion = typeof result === 'string' ? result : '3.x'
55+
).catch(() => process.exit(1))
5756

5857
const npmPackages = packageNames.map(p => `${p}@npm:${p}-nightly@${nuxtVersionTags[nuxtVersion]}`)
5958

@@ -142,6 +141,7 @@ export default defineCommand({
142141
{
143142
type: 'select',
144143
initial: 'dedupe',
144+
cancel: 'reject',
145145
options: [
146146
{
147147
label: 'dedupe lockfile',
@@ -158,12 +158,7 @@ export default defineCommand({
158158
},
159159
],
160160
},
161-
)
162-
163-
// user bails on the question with Ctrl+C
164-
if (typeof method !== 'string') {
165-
process.exit(1)
166-
}
161+
).catch(() => process.exit(1))
167162

168163
if (method === 'force') {
169164
logger.info(

0 commit comments

Comments
 (0)