Skip to content

Commit 6958d93

Browse files
committed
fix: handle Ctrl+C gracefully during prompts
## Changed - Switch `program.parse()` to `program.parseAsync()` to propagate async errors ## New - Catch `ExitPromptError` globally to exit cleanly with "Operation cancelled." message - Add `operationCancelled` message to centralized messages
1 parent 36bab60 commit 6958d93

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import { createRequire } from 'node:module';
44
import { Command, Option } from 'commander';
5+
import { ExitPromptError } from '@inquirer/core';
56
import dotenv from 'dotenv';
67

78
dotenv.config({ debug: false, quiet: true });
89

10+
import { Messages } from './messages/messages.js';
911
import initCommand from './cli/cmd/init/init.js';
1012
import translateCommand from './cli/cmd/translate/translate.js';
1113
import memoryCommand from './cli/cmd/memory/memory.js';
@@ -27,6 +29,12 @@ const program = new Command()
2729
.addCommand(glossaryCommand);
2830

2931
// Parse command line arguments
30-
program.parse();
32+
program.parseAsync().catch((error: unknown) => {
33+
if (error instanceof ExitPromptError) {
34+
console.log(`\n${Messages.info.operationCancelled}`);
35+
process.exit(0);
36+
}
37+
throw error;
38+
});
3139

3240
export default program;

src/messages/messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export const Messages = {
9999
`${count} ${count === 1 ? 'locale' : 'locales'} already added`,
100100
autoDetected: (count: number) => `${count} auto-detected`,
101101
manuallyAdded: (count: number) => `${count} manually added`,
102+
operationCancelled: 'Operation cancelled.',
102103
},
103104

104105
warnings: {

0 commit comments

Comments
 (0)