@inquirer/core 12.0.2 → 12.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,18 @@ import path from 'node:path';
10
10
  // Capture the real setImmediate at module load time so it works even when test
11
11
  // frameworks mock timers with vi.useFakeTimers() or similar.
12
12
  const nativeSetImmediate = globalThis.setImmediate;
13
+ /**
14
+ * Register an event listener and return its disposer, so registrations collapse
15
+ * into a single statement: `cleanups.add(listenTo(rl, 'close', handler))`.
16
+ */
17
+ function listenTo(target, event, listener) {
18
+ if ('on' in target) {
19
+ target.on(event, listener);
20
+ return () => target.removeListener(event, listener);
21
+ }
22
+ target.addEventListener(event, listener);
23
+ return () => target.removeEventListener(event, listener);
24
+ }
13
25
  function getCallSites() {
14
26
  // oxlint-disable-next-line typescript/unbound-method
15
27
  const savedPrepareStackTrace = Error.prepareStackTrace;
@@ -53,41 +65,63 @@ export function createPrompt(view) {
53
65
  output.mute();
54
66
  const screen = new ScreenManager(rl);
55
67
  const { promise, resolve, reject } = PromisePolyfill.withResolver();
56
- const cancel = () => reject(new CancelPromptError());
57
- if (signal) {
58
- const abort = () => reject(new AbortPromptError({ cause: signal.reason }));
59
- if (signal.aborted) {
60
- abort();
61
- return Object.assign(promise, { cancel });
62
- }
63
- signal.addEventListener('abort', abort);
64
- cleanups.add(() => signal.removeEventListener('abort', abort));
65
- }
66
- cleanups.add(onSignalExit((code, signal) => {
67
- reject(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
68
- }));
69
- // SIGINT must be explicitly handled by the prompt so the ExitPromptError can be handled.
70
- // Otherwise, the prompt will stop and in some scenarios never resolve.
71
- // Ref issue #1741
72
- const sigint = () => reject(new ExitPromptError(`User force closed the prompt with SIGINT`));
73
- rl.on('SIGINT', sigint);
74
- cleanups.add(() => rl.removeListener('SIGINT', sigint));
75
68
  return withHooks(rl, (cycle) => {
76
- // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
77
- // triggers after the process is done (which happens after timeouts are done triggering.)
78
- // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
79
- const hooksCleanup = AsyncResource.bind(() => effectScheduler.clearAll());
80
- rl.on('close', hooksCleanup);
81
- cleanups.add(() => rl.removeListener('close', hooksCleanup));
69
+ // Clear hook effects synchronously before the prompt settles: readline
70
+ // emits keypresses synchronously within a single data event, so a
71
+ // microtask-deferred cleanup would let same-tick input keep driving a
72
+ // settled prompt.
73
+ const clearEffects = AsyncResource.bind(() => effectScheduler.clearAll());
74
+ // The promise resolvers and effectScheduler.clearAll() are both idempotent,
75
+ // so repeated settlement attempts are harmless no-ops. If a hook cleanup
76
+ // throws, the cleanup error supersedes the settlement: the answer (or
77
+ // settlement error, e.g. AbortPromptError) is dropped in favor of
78
+ // rejecting with the cleanup error.
79
+ const settlePrompt = (settle) => {
80
+ try {
81
+ clearEffects();
82
+ settle();
83
+ }
84
+ catch (error) {
85
+ reject(error);
86
+ }
87
+ };
88
+ const resolvePrompt = (value) => settlePrompt(() => resolve(value));
89
+ const rejectPrompt = (error) => settlePrompt(() => reject(error));
90
+ const promptPromise = Object.assign(promise
91
+ .finally(() => {
92
+ cleanups.forEach((cleanup) => cleanup());
93
+ screen.done({ clearContent: Boolean(context.clearPromptOnDone) });
94
+ output.end();
95
+ })
96
+ .then(() => promise), { cancel: () => rejectPrompt(new CancelPromptError()) });
97
+ if (signal) {
98
+ const abort = () => rejectPrompt(new AbortPromptError({ cause: signal.reason }));
99
+ if (signal.aborted) {
100
+ abort();
101
+ return promptPromise;
102
+ }
103
+ cleanups.add(listenTo(signal, 'abort', abort));
104
+ }
105
+ cleanups.add(onSignalExit((code, signal) => {
106
+ rejectPrompt(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
107
+ }));
108
+ // SIGINT must be explicitly handled by the prompt so the ExitPromptError can be handled.
109
+ // Otherwise, the prompt will stop and in some scenarios never resolve.
110
+ // Ref issue #1741
111
+ cleanups.add(listenTo(rl, 'SIGINT', () => rejectPrompt(new ExitPromptError(`User force closed the prompt with SIGINT`))));
112
+ // Fallback for readline closing without the prompt settling (e.g. stdin
113
+ // EOF), so active effect timeouts still get cleared. Every settlement
114
+ // path already clears effects through settlePrompt; this event triggers
115
+ // immediately when the user presses ctrl+c, while signal-exit only
116
+ // triggers after the process is done (after timeouts finish triggering).
117
+ cleanups.add(listenTo(rl, 'close', clearEffects));
82
118
  const startCycle = () => {
83
119
  // Re-renders only happen when the state change; but the readline cursor could
84
120
  // change position and that also requires a re-render (and a manual one because
85
121
  // we mute the streams). We set the listener after the initial workLoop to avoid
86
122
  // a double render if render triggered by a state change sets the cursor to the
87
123
  // right position.
88
- const checkCursorPos = () => screen.checkCursorPos();
89
- rl.input.on('keypress', checkCursorPos);
90
- cleanups.add(() => rl.input.removeListener('keypress', checkCursorPos));
124
+ cleanups.add(listenTo(rl.input, 'keypress', () => screen.checkCursorPos()));
91
125
  let pendingDone = null;
92
126
  cycle(() => {
93
127
  let effectsSettled = false;
@@ -97,7 +131,7 @@ export function createPrompt(view) {
97
131
  // After the cycle completes (async validation path), the "done"
98
132
  // render already flushed via setStatus → handleChange, so resolve
99
133
  // immediately.
100
- resolve(value);
134
+ resolvePrompt(value);
101
135
  }
102
136
  else {
103
137
  pendingDone = { value };
@@ -117,13 +151,13 @@ export function createPrompt(view) {
117
151
  effectScheduler.run();
118
152
  }
119
153
  catch (error) {
120
- reject(error);
154
+ rejectPrompt(error);
121
155
  }
122
156
  effectsSettled = true;
123
157
  if (pendingDone !== null) {
124
158
  const { value } = pendingDone;
125
159
  pendingDone = null;
126
- resolve(value);
160
+ resolvePrompt(value);
127
161
  }
128
162
  });
129
163
  };
@@ -143,22 +177,7 @@ export function createPrompt(view) {
143
177
  else {
144
178
  startCycle();
145
179
  }
146
- return Object.assign(promise
147
- .then((answer) => {
148
- effectScheduler.clearAll();
149
- return answer;
150
- }, (error) => {
151
- effectScheduler.clearAll();
152
- throw error;
153
- })
154
- // Wait for the promise to settle, then cleanup.
155
- .finally(() => {
156
- cleanups.forEach((cleanup) => cleanup());
157
- screen.done({ clearContent: Boolean(context.clearPromptOnDone) });
158
- output.end();
159
- })
160
- // Once cleanup is done, let the expose promise resolve/reject to the internal one.
161
- .then(() => promise), { cancel });
180
+ return promptPromise;
162
181
  });
163
182
  };
164
183
  return prompt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "12.0.2",
3
+ "version": "12.0.3",
4
4
  "description": "Core Inquirer prompt API",
5
5
  "keywords": [
6
6
  "answer",
@@ -94,5 +94,5 @@
94
94
  },
95
95
  "main": "./dist/index.js",
96
96
  "types": "./dist/index.d.ts",
97
- "gitHead": "5748bd9966b5e175ddf5995ee4cdce6f60c4272e"
97
+ "gitHead": "cbdb34bfc6c245941d80ef88493c50b3d6dbfce6"
98
98
  }