Skip to content

Commit

Permalink
Chore: Ignore .yarn from diff --git a/packages/core/package.json b/pa…
Browse files Browse the repository at this point in the history
…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
SBoudrias committed Sep 9, 2023
1 parent 0384a48 commit 15a1085
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
.yarn -diff
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
yarn.lock
.prettierignore
.gitattributes
.gitignore
.eslintignore
.yarn
Expand Down

0 comments on commit 15a1085

Please sign in to comment.