@inquirer/core 12.0.2 → 12.0.4

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.
package/README.md CHANGED
@@ -8,21 +8,53 @@ It aims to implements a lightweight API similar to React hooks - but without JSX
8
8
 
9
9
  <table>
10
10
  <tr>
11
- <th>npm</th>
12
11
  <th>yarn</th>
12
+ <th>pnpm</th>
13
13
  </tr>
14
14
  <tr>
15
15
  <td>
16
16
 
17
17
  ```sh
18
- npm install @inquirer/core
18
+ yarn add @inquirer/core
19
19
  ```
20
20
 
21
21
  </td>
22
22
  <td>
23
23
 
24
24
  ```sh
25
- yarn add @inquirer/core
25
+ pnpm add @inquirer/core
26
+ ```
27
+
28
+ </td>
29
+ </tr>
30
+ <tr>
31
+ <th>bun</th>
32
+ <th>deno</th>
33
+ </tr>
34
+ <tr>
35
+ <td>
36
+
37
+ ```sh
38
+ bun add @inquirer/core
39
+ ```
40
+
41
+ </td>
42
+ <td>
43
+
44
+ ```sh
45
+ deno add npm:@inquirer/core
46
+ ```
47
+
48
+ </td>
49
+ </tr>
50
+ <tr>
51
+ <th colSpan="2">npm</th>
52
+ </tr>
53
+ <tr>
54
+ <td colSpan="2">
55
+
56
+ ```sh
57
+ npm install @inquirer/core
26
58
  ```
27
59
 
28
60
  </td>
@@ -0,0 +1,9 @@
1
+ type A = {
2
+ a: string;
3
+ };
4
+ type B = {
5
+ a: string;
6
+ b: number;
7
+ };
8
+ export declare function f(x: A): B;
9
+ export {};
@@ -0,0 +1,4 @@
1
+ export function f(x) {
2
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
3
+ return x;
4
+ }
@@ -0,0 +1,9 @@
1
+ type A = {
2
+ a: string;
3
+ };
4
+ type B = {
5
+ a: string;
6
+ b: number;
7
+ };
8
+ export declare function f(x: A): B;
9
+ export {};
@@ -0,0 +1,3 @@
1
+ export function f(x) {
2
+ return x; // oxlint-disable-line typescript/no-unsafe-type-assertion
3
+ }
@@ -0,0 +1,9 @@
1
+ type A = {
2
+ a: string;
3
+ };
4
+ type B = {
5
+ a: string;
6
+ b: number;
7
+ };
8
+ export declare function f(x: A): B;
9
+ export {};
@@ -0,0 +1,5 @@
1
+ /* oxlint-disable typescript/no-unsafe-type-assertion */
2
+ export function f(x) {
3
+ return x;
4
+ }
5
+ /* oxlint-enable typescript/no-unsafe-type-assertion */
@@ -1,15 +1,27 @@
1
1
  import * as readline from 'node:readline';
2
2
  import { AsyncResource } from 'node:async_hooks';
3
+ import { withResolver } from '@inquirer/type';
3
4
  import MuteStream from 'mute-stream';
4
5
  import { onExit as onSignalExit } from 'signal-exit';
5
6
  import ScreenManager from "./screen-manager.js";
6
- import { PromisePolyfill } from "./promise-polyfill.js";
7
7
  import { withHooks, effectScheduler } from "./hook-engine.js";
8
8
  import { AbortPromptError, CancelPromptError, ExitPromptError } from "./errors.js";
9
9
  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;
@@ -40,54 +52,77 @@ export function createPrompt(view) {
40
52
  // Add mute capabilities to the output
41
53
  const output = new MuteStream();
42
54
  output.pipe(context.output ?? process.stdout);
43
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion
55
+ /* oxlint-disable typescript/no-unsafe-type-assertion */
44
56
  const rl = readline.createInterface({
45
57
  terminal: true,
46
58
  input,
47
59
  output,
48
60
  });
61
+ /* oxlint-enable typescript/no-unsafe-type-assertion */
49
62
  // Mute the output after readline has initialized so readline can perform
50
63
  // any terminal setup writes (e.g. Windows Console API initialization)
51
64
  // before suppressing output. ScreenManager will unmute/mute around each
52
65
  // render call as needed.
53
66
  output.mute();
54
67
  const screen = new ScreenManager(rl);
55
- 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));
68
+ const { promise, resolve, reject } = withResolver();
75
69
  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));
70
+ // Clear hook effects synchronously before the prompt settles: readline
71
+ // emits keypresses synchronously within a single data event, so a
72
+ // microtask-deferred cleanup would let same-tick input keep driving a
73
+ // settled prompt.
74
+ const clearEffects = AsyncResource.bind(() => effectScheduler.clearAll());
75
+ // The promise resolvers and effectScheduler.clearAll() are both idempotent,
76
+ // so repeated settlement attempts are harmless no-ops. If a hook cleanup
77
+ // throws, the cleanup error supersedes the settlement: the answer (or
78
+ // settlement error, e.g. AbortPromptError) is dropped in favor of
79
+ // rejecting with the cleanup error.
80
+ const settlePrompt = (settle) => {
81
+ try {
82
+ clearEffects();
83
+ settle();
84
+ }
85
+ catch (error) {
86
+ reject(error);
87
+ }
88
+ };
89
+ const resolvePrompt = (value) => settlePrompt(() => resolve(value));
90
+ const rejectPrompt = (error) => settlePrompt(() => reject(error));
91
+ const promptPromise = Object.assign(promise
92
+ .finally(() => {
93
+ cleanups.forEach((cleanup) => cleanup());
94
+ screen.done({ clearContent: Boolean(context.clearPromptOnDone) });
95
+ output.end();
96
+ })
97
+ .then(() => promise), { cancel: () => rejectPrompt(new CancelPromptError()) });
98
+ if (signal) {
99
+ const abort = () => rejectPrompt(new AbortPromptError({ cause: signal.reason }));
100
+ if (signal.aborted) {
101
+ abort();
102
+ return promptPromise;
103
+ }
104
+ cleanups.add(listenTo(signal, 'abort', abort));
105
+ }
106
+ cleanups.add(onSignalExit((code, signal) => {
107
+ rejectPrompt(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
108
+ }));
109
+ // SIGINT must be explicitly handled by the prompt so the ExitPromptError can be handled.
110
+ // Otherwise, the prompt will stop and in some scenarios never resolve.
111
+ // Ref issue #1741
112
+ cleanups.add(listenTo(rl, 'SIGINT', () => rejectPrompt(new ExitPromptError(`User force closed the prompt with SIGINT`))));
113
+ // Fallback for readline closing without the prompt settling (e.g. stdin
114
+ // EOF), so active effect timeouts still get cleared. Every settlement
115
+ // path already clears effects through settlePrompt; this event triggers
116
+ // immediately when the user presses ctrl+c, while signal-exit only
117
+ // triggers after the process is done (after timeouts finish triggering).
118
+ cleanups.add(listenTo(rl, 'close', clearEffects));
82
119
  const startCycle = () => {
83
120
  // Re-renders only happen when the state change; but the readline cursor could
84
121
  // change position and that also requires a re-render (and a manual one because
85
122
  // we mute the streams). We set the listener after the initial workLoop to avoid
86
123
  // a double render if render triggered by a state change sets the cursor to the
87
124
  // right position.
88
- const checkCursorPos = () => screen.checkCursorPos();
89
- rl.input.on('keypress', checkCursorPos);
90
- cleanups.add(() => rl.input.removeListener('keypress', checkCursorPos));
125
+ cleanups.add(listenTo(rl.input, 'keypress', () => screen.checkCursorPos()));
91
126
  let pendingDone = null;
92
127
  cycle(() => {
93
128
  let effectsSettled = false;
@@ -97,7 +132,7 @@ export function createPrompt(view) {
97
132
  // After the cycle completes (async validation path), the "done"
98
133
  // render already flushed via setStatus → handleChange, so resolve
99
134
  // immediately.
100
- resolve(value);
135
+ resolvePrompt(value);
101
136
  }
102
137
  else {
103
138
  pendingDone = { value };
@@ -117,13 +152,13 @@ export function createPrompt(view) {
117
152
  effectScheduler.run();
118
153
  }
119
154
  catch (error) {
120
- reject(error);
155
+ rejectPrompt(error);
121
156
  }
122
157
  effectsSettled = true;
123
158
  if (pendingDone !== null) {
124
159
  const { value } = pendingDone;
125
160
  pendingDone = null;
126
- resolve(value);
161
+ resolvePrompt(value);
127
162
  }
128
163
  });
129
164
  };
@@ -143,22 +178,7 @@ export function createPrompt(view) {
143
178
  else {
144
179
  startCycle();
145
180
  }
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 });
181
+ return promptPromise;
162
182
  });
163
183
  };
164
184
  return prompt;
@@ -16,16 +16,14 @@ function createStore(rl) {
16
16
  // Run callback in with the hook engine setup.
17
17
  export function withHooks(rl, cb) {
18
18
  const store = createStore(rl);
19
- return hookStorage.run(store, () => {
20
- function cycle(render) {
21
- store.handleChange = () => {
22
- store.index = 0;
23
- render();
24
- };
25
- store.handleChange();
26
- }
27
- return cb(cycle);
28
- });
19
+ function cycle(render) {
20
+ store.handleChange = () => {
21
+ store.index = 0;
22
+ render();
23
+ };
24
+ store.handleChange();
25
+ }
26
+ return hookStorage.run(store, () => cb(cycle));
29
27
  }
30
28
  // Safe getStore utility that'll return the store or throw if undefined.
31
29
  function getStore() {
@@ -23,11 +23,11 @@ function deepMerge(...objects) {
23
23
  return output;
24
24
  }
25
25
  export function makeTheme(...themes) {
26
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion
26
+ /* oxlint-disable typescript/no-unsafe-type-assertion */
27
27
  const themesToMerge = [
28
28
  getDefaultTheme(),
29
29
  ...themes.filter((theme) => theme != null),
30
30
  ];
31
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion
32
31
  return deepMerge(...themesToMerge);
32
+ /* oxlint-enable typescript/no-unsafe-type-assertion */
33
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "12.0.2",
3
+ "version": "12.0.4",
4
4
  "description": "Core Inquirer prompt API",
5
5
  "keywords": [
6
6
  "answer",
@@ -67,9 +67,9 @@
67
67
  "tsc": "tsc"
68
68
  },
69
69
  "dependencies": {
70
- "@inquirer/ansi": "^2.0.8",
70
+ "@inquirer/ansi": "^2.0.9",
71
71
  "@inquirer/figures": "^2.0.9",
72
- "@inquirer/type": "4.1.1",
72
+ "@inquirer/type": "4.2.0",
73
73
  "cli-width": "^4.1.0",
74
74
  "fast-wrap-ansi": "^0.2.0",
75
75
  "mute-stream": "^3.0.0",
@@ -94,5 +94,5 @@
94
94
  },
95
95
  "main": "./dist/index.js",
96
96
  "types": "./dist/index.d.ts",
97
- "gitHead": "5748bd9966b5e175ddf5995ee4cdce6f60c4272e"
97
+ "gitHead": "a9436bd94d55a439df308f50e77a7cd0b2078e98"
98
98
  }
@@ -1,7 +0,0 @@
1
- export declare class PromisePolyfill<T> extends Promise<T> {
2
- static withResolver<T>(): {
3
- promise: Promise<T>;
4
- resolve: (value: T) => void;
5
- reject: (error: unknown) => void;
6
- };
7
- }
@@ -1,14 +0,0 @@
1
- // TODO: Remove this class once Node 22 becomes the minimum supported version.
2
- export class PromisePolyfill extends Promise {
3
- // Available starting from Node 22
4
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
5
- static withResolver() {
6
- let resolve;
7
- let reject;
8
- const promise = new Promise((res, rej) => {
9
- resolve = res;
10
- reject = rej;
11
- });
12
- return { promise, resolve: resolve, reject: reject };
13
- }
14
- }