From 8890f7dcfc70d62948f9f7f8a3b9a4214c56c750 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Fri, 6 Jun 2025 04:34:37 -0700 Subject: [PATCH] fix(cli): exit cleanly when quitting out of CLI --- cli/bin/index.js | 4 ++++ cli/lib/handlers.js | 11 +++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 cli/lib/handlers.js diff --git a/cli/bin/index.js b/cli/bin/index.js index 5b67db8..f6d4b66 100755 --- a/cli/bin/index.js +++ b/cli/bin/index.js @@ -6,6 +6,10 @@ import figlet from 'figlet'; import { initConfig, loadConfig } from '../lib/config.js'; import { downloadAsset } from '../lib/download.js'; import { runRootScript } from '../lib/runScript.js'; +import { handleExitError } from '../lib/handlers.js'; + +process.on('unhandledRejection', (err) => handleExitError(err, 'Unhandled rejection')); +process.on('uncaughtException', (err) => handleExitError(err, 'Uncaught exception')); program.name('coryd').description('šŸŖ„ Run commands, download things and have fun.').version('1.0.0'); program.command('init').description('Initialize CLI and populate required config.').action(initConfig); diff --git a/cli/lib/handlers.js b/cli/lib/handlers.js new file mode 100644 index 0000000..1744116 --- /dev/null +++ b/cli/lib/handlers.js @@ -0,0 +1,11 @@ +export const handleExitError = (err, type = 'Unhandled error') => { + const isExit = err?.name === 'ExitPromptError' || err?.code === 'ERR_CANCELED' || err?.message?.includes('SIGINT'); + + if (isExit) { + console.log('\nšŸ‘‹ Exiting. Cya!\n'); + process.exit(0); + } + + console.error(`āŒ ${type}:`, err); + process.exit(1); +} diff --git a/package-lock.json b/package-lock.json index 88af963..a7241d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "9.0.3", + "version": "9.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "9.0.3", + "version": "9.0.4", "license": "MIT", "dependencies": { "minisearch": "^7.1.2", diff --git a/package.json b/package.json index 9a45385..ae0b250 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "9.0.3", + "version": "9.0.4", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": {