Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Ignore .yarn from diff --git a/packages/core/package.json b/pa…
…ckages/core/package.json index cb11259..38a66da 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -68,6 +68,7 @@ "figures": "^3.2.0", "mute-stream": "^1.0.0", "run-async": "^3.0.0", + "signal-exit": "^4.1.0", "strip-ansi": "^6.0.1", "wrap-ansi": "^6.2.0" }, diff --git a/packages/core/src/lib/create-prompt.mts b/packages/core/src/lib/create-prompt.mts index a1871a7..46c95b3 100644 --- a/packages/core/src/lib/create-prompt.mts +++ b/packages/core/src/lib/create-prompt.mts @@ -1,6 +1,7 @@ import * as readline from 'node:readline'; import { CancelablePromise, type Prompt } from '@inquirer/type'; import MuteStream from 'mute-stream'; +import { onExit as onSignalExit } from 'signal-exit'; import ScreenManager from './screen-manager.mjs'; import type { InquirerReadline } from './read-line.type.mjs'; import { withHooks, effectScheduler } from './hook-engine.mjs'; @@ -53,11 +54,16 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>( let cancel: () => void = () => {}; const answer = new CancelablePromise<Value>((resolve, reject) => { withHooks(rl, (store) => { - const checkCursorPos = () => { + const removeExitListener = onSignalExit((code, signal) => { + onExit(); + reject(new Error(`User force closed the prompt with ${code} ${signal}`)); + }); + + function checkCursorPos() { screen.checkCursorPos(); - }; + } - const onExit = () => { + function onExit() { try { store.hooksCleanup.forEach((cleanFn) => { cleanFn?.(); @@ -73,29 +79,16 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>( } screen.done(); - process.removeListener('SIGINT', onForceExit); + removeExitListener(); store.rl.input.removeListener('keypress', checkCursorPos); - }; + } cancel = () => { onExit(); - reject(new Error('Prompt was canceled')); }; - let shouldHandleExit = true; - const onForceExit = () => { - if (shouldHandleExit) { - shouldHandleExit = false; - onExit(); - reject(new Error('User force closed the prompt with CTRL+C')); - } - }; - - // Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it. - process.on('SIGINT', onForceExit); - - const done = (value: Value) => { + function done(value: Value) { // Delay execution to let time to the hookCleanup functions to registers. setImmediate(() => { onExit(); @@ -103,9 +96,9 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>( // Finally we resolve our promise resolve(value); }); - }; + } - const workLoop = (resolvedConfig: Config & ResolvedPromptConfig) => { + function workLoop(resolvedConfig: Config & ResolvedPromptConfig) { store.index = 0; store.handleChange = () => workLoop(resolvedConfig); @@ -121,7 +114,7 @@ export function createPrompt<Value, Config extends AsyncPromptConfig>( onExit(); reject(err); } - }; + } // TODO: we should display a loader while we get the default options. getPromptConfig(config).then((resolvedConfig) => { diff --git a/yarn.lock b/yarn.lock index 25c7bb1..8b34ffc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -341,6 +341,7 @@ __metadata: figures: ^3.2.0 mute-stream: ^1.0.0 run-async: ^3.0.0 + signal-exit: ^4.1.0 strip-ansi: ^6.0.1 wrap-ansi: ^6.2.0 languageName: unknown @@ -7314,6 +7315,13 @@ __metadata: languageName: node linkType: hard +"signal-exit@npm:^4.1.0": + version: 4.1.0 + resolution: "signal-exit@npm:4.1.0" + checksum: 64c757b498cb8629ffa5f75485340594d2f8189e9b08700e69199069c8e3070fb3e255f7ab873c05dc0b3cec412aea7402e10a5990cb6a050bd33ba062a6c549 + languageName: node + linkType: hard + "sigstore@npm:^1.3.0, sigstore@npm:^1.4.0": version: 1.8.0 resolution: "sigstore@npm:1.8.0" by default
- Loading branch information