#!/usr/bin/env node import { Ch5ShellCli } from "./cli"; import { Ch5CliLogger } from "./cli/Ch5CliLogger"; const COMMON_TRANSLATIONS = require("./cli/files/en.json"); // The interactive prompts use the "enquirer" library. Starting with Node.js 24 // (via node PR #58283, which added stricter validation for readline functions // called after close), cancelling a prompt (Ctrl+C / ESC) triggers an // asynchronous "ERR_USE_AFTER_CLOSE: readline was closed" error from enquirer's // internal keypress handler (rl.pause()/rl.close() run after the interface is // already closed). Node.js 20.x and 22.x do not have this check, so the same // code worked there. This throw happens after the prompt promise has // already rejected, so it escapes the command's try/catch and surfaces as an // unhandled rejection / uncaught exception that crashes the process before the // graceful message can be shown. Detect this specific case, print the same // user-friendly message the commands use, and exit cleanly. const logger = new Ch5CliLogger(); let readlineErrorHandled = false; const isReadlineClosedError = (error: any): boolean => !!error && (error.code === "ERR_USE_AFTER_CLOSE" || (typeof error.message === "string" && error.message.includes("readline was closed"))); const handleReadlineClosedError = () => { if (!readlineErrorHandled) { readlineErrorHandled = true; logger.printError(COMMON_TRANSLATIONS.COMMON.SOMETHING_WENT_WRONG); } process.exit(1); }; process.on("unhandledRejection", (reason: any) => { if (isReadlineClosedError(reason)) { handleReadlineClosedError(); } throw reason; }); process.on("uncaughtException", (error: any) => { if (isReadlineClosedError(error)) { handleReadlineClosedError(); } throw error; }); const cli = new Ch5ShellCli(); cli.run();