fix(cli): exit cleanly when quitting out of CLI

This commit is contained in:
Cory Dransfeldt 2025-06-06 04:34:37 -07:00
parent 862c8e1c7f
commit 8890f7dcfc
No known key found for this signature in database
4 changed files with 18 additions and 3 deletions

View file

@ -6,6 +6,10 @@ import figlet from 'figlet';
import { initConfig, loadConfig } from '../lib/config.js'; import { initConfig, loadConfig } from '../lib/config.js';
import { downloadAsset } from '../lib/download.js'; import { downloadAsset } from '../lib/download.js';
import { runRootScript } from '../lib/runScript.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.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); program.command('init').description('Initialize CLI and populate required config.').action(initConfig);

11
cli/lib/handlers.js Normal file
View file

@ -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);
}

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "9.0.3", "version": "9.0.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "9.0.3", "version": "9.0.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"minisearch": "^7.1.2", "minisearch": "^7.1.2",

View file

@ -1,6 +1,6 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "9.0.3", "version": "9.0.4",
"description": "The source for my personal site. Built using 11ty (and other tools).", "description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module", "type": "module",
"engines": { "engines": {