@inquirer/core 9.0.10 → 9.1.0

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.
@@ -29,7 +29,10 @@ class Separator {
29
29
  }
30
30
  }
31
31
  static isSeparator(choice) {
32
- return Boolean(choice && choice.type === 'separator');
32
+ return Boolean(choice &&
33
+ typeof choice === 'object' &&
34
+ 'type' in choice &&
35
+ choice.type === 'separator');
33
36
  }
34
37
  }
35
38
  exports.Separator = Separator;
@@ -29,89 +29,95 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.createPrompt = createPrompt;
30
30
  const readline = __importStar(require("node:readline"));
31
31
  const node_async_hooks_1 = require("node:async_hooks");
32
- const type_1 = require("@inquirer/type");
33
32
  const mute_stream_1 = __importDefault(require("mute-stream"));
34
33
  const signal_exit_1 = require("signal-exit");
35
34
  const screen_manager_mjs_1 = __importDefault(require('./screen-manager.js'));
35
+ const type_1 = require("@inquirer/type");
36
36
  const hook_engine_mjs_1 = require('./hook-engine.js');
37
37
  const errors_mjs_1 = require('./errors.js');
38
38
  function createPrompt(view) {
39
- const prompt = (config, context) => {
40
- var _a, _b;
39
+ const prompt = (config, context = {}) => {
40
+ var _a;
41
41
  // Default `input` to stdin
42
- const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
42
+ const { input = process.stdin, signal } = context;
43
43
  // Add mute capabilities to the output
44
44
  const output = new mute_stream_1.default();
45
- output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
45
+ output.pipe((_a = context.output) !== null && _a !== void 0 ? _a : process.stdout);
46
46
  const rl = readline.createInterface({
47
47
  terminal: true,
48
48
  input,
49
49
  output,
50
50
  });
51
51
  const screen = new screen_manager_mjs_1.default(rl);
52
- let cancel = () => { };
53
- const answer = new type_1.CancelablePromise((resolve, reject) => {
54
- (0, hook_engine_mjs_1.withHooks)(rl, (cycle) => {
55
- function checkCursorPos() {
56
- screen.checkCursorPos();
52
+ const cleanups = new Set();
53
+ const { promise, resolve, reject } = type_1.CancelablePromise.withResolver();
54
+ function onExit() {
55
+ cleanups.forEach((cleanup) => cleanup());
56
+ screen.done({ clearContent: Boolean(context === null || context === void 0 ? void 0 : context.clearPromptOnDone) });
57
+ output.end();
58
+ }
59
+ function fail(error) {
60
+ onExit();
61
+ reject(error);
62
+ }
63
+ if (signal) {
64
+ const abort = () => fail(new errors_mjs_1.AbortPromptError({ cause: signal.reason }));
65
+ if (signal.aborted) {
66
+ abort();
67
+ return promise;
68
+ }
69
+ signal.addEventListener('abort', abort);
70
+ cleanups.add(() => signal.removeEventListener('abort', abort));
71
+ }
72
+ (0, hook_engine_mjs_1.withHooks)(rl, (cycle) => {
73
+ cleanups.add((0, signal_exit_1.onExit)((code, signal) => {
74
+ fail(new errors_mjs_1.ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
75
+ }));
76
+ const hooksCleanup = node_async_hooks_1.AsyncResource.bind(() => {
77
+ try {
78
+ hook_engine_mjs_1.effectScheduler.clearAll();
57
79
  }
58
- const removeExitListener = (0, signal_exit_1.onExit)((code, signal) => {
80
+ catch (error) {
81
+ reject(error);
82
+ }
83
+ });
84
+ cleanups.add(hooksCleanup);
85
+ // Re-renders only happen when the state change; but the readline cursor could change position
86
+ // and that also requires a re-render (and a manual one because we mute the streams).
87
+ // We set the listener after the initial workLoop to avoid a double render if render triggered
88
+ // by a state change sets the cursor to the right position.
89
+ const checkCursorPos = () => screen.checkCursorPos();
90
+ rl.input.on('keypress', checkCursorPos);
91
+ cleanups.add(() => rl.input.removeListener('keypress', checkCursorPos));
92
+ // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
93
+ // triggers after the process is done (which happens after timeouts are done triggering.)
94
+ // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
95
+ rl.on('close', hooksCleanup);
96
+ cleanups.add(() => rl.removeListener('close', hooksCleanup));
97
+ function done(value) {
98
+ // Delay execution to let time to the hookCleanup functions to registers.
99
+ setImmediate(() => {
59
100
  onExit();
60
- reject(new errors_mjs_1.ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
101
+ // Finally we resolve our promise
102
+ resolve(value);
61
103
  });
62
- const hooksCleanup = node_async_hooks_1.AsyncResource.bind(() => {
63
- try {
64
- hook_engine_mjs_1.effectScheduler.clearAll();
65
- }
66
- catch (error) {
67
- reject(error);
68
- }
69
- });
70
- function onExit() {
71
- hooksCleanup();
72
- screen.done({ clearContent: Boolean(context === null || context === void 0 ? void 0 : context.clearPromptOnDone) });
73
- removeExitListener();
74
- rl.input.removeListener('keypress', checkCursorPos);
75
- rl.removeListener('close', hooksCleanup);
76
- output.end();
104
+ }
105
+ cycle(() => {
106
+ try {
107
+ const nextView = view(config, done);
108
+ const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
109
+ screen.render(content, bottomContent);
110
+ hook_engine_mjs_1.effectScheduler.run();
77
111
  }
78
- cancel = () => {
79
- onExit();
80
- reject(new errors_mjs_1.CancelPromptError());
81
- };
82
- function done(value) {
83
- // Delay execution to let time to the hookCleanup functions to registers.
84
- setImmediate(() => {
85
- onExit();
86
- // Finally we resolve our promise
87
- resolve(value);
88
- });
112
+ catch (error) {
113
+ fail(error);
89
114
  }
90
- cycle(() => {
91
- try {
92
- const nextView = view(config, done);
93
- const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
94
- screen.render(content, bottomContent);
95
- hook_engine_mjs_1.effectScheduler.run();
96
- }
97
- catch (error) {
98
- onExit();
99
- reject(error);
100
- }
101
- });
102
- // Re-renders only happen when the state change; but the readline cursor could change position
103
- // and that also requires a re-render (and a manual one because we mute the streams).
104
- // We set the listener after the initial workLoop to avoid a double render if render triggered
105
- // by a state change sets the cursor to the right position.
106
- rl.input.on('keypress', checkCursorPos);
107
- // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
108
- // triggers after the process is done (which happens after timeouts are done triggering.)
109
- // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
110
- rl.on('close', hooksCleanup);
111
115
  });
112
116
  });
113
- answer.cancel = cancel;
114
- return answer;
117
+ promise.cancel = () => {
118
+ fail(new errors_mjs_1.CancelPromptError());
119
+ };
120
+ return promise;
115
121
  };
116
122
  return prompt;
117
123
  }
@@ -1,6 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ValidationError = exports.HookError = exports.ExitPromptError = exports.CancelPromptError = void 0;
3
+ exports.ValidationError = exports.HookError = exports.ExitPromptError = exports.CancelPromptError = exports.AbortPromptError = void 0;
4
+ class AbortPromptError extends Error {
5
+ constructor(options) {
6
+ super();
7
+ Object.defineProperty(this, "name", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: 'AbortPromptError'
12
+ });
13
+ Object.defineProperty(this, "message", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: 'Prompt was aborted'
18
+ });
19
+ this.cause = options === null || options === void 0 ? void 0 : options.cause;
20
+ }
21
+ }
22
+ exports.AbortPromptError = AbortPromptError;
4
23
  class CancelPromptError extends Error {
5
24
  constructor() {
6
25
  super(...arguments);
@@ -6,5 +6,5 @@ export declare class Separator {
6
6
  readonly separator: string;
7
7
  readonly type = "separator";
8
8
  constructor(separator?: string);
9
- static isSeparator(choice: undefined | Separator | Record<string, unknown>): choice is Separator;
9
+ static isSeparator(choice: unknown): choice is Separator;
10
10
  }
@@ -1,3 +1,10 @@
1
+ export declare class AbortPromptError extends Error {
2
+ name: string;
3
+ message: string;
4
+ constructor(options?: {
5
+ cause?: unknown;
6
+ });
7
+ }
1
8
  export declare class CancelPromptError extends Error {
2
9
  name: string;
3
10
  message: string;
@@ -13,6 +13,9 @@ export class Separator {
13
13
  }
14
14
  }
15
15
  static isSeparator(choice) {
16
- return Boolean(choice && choice.type === 'separator');
16
+ return Boolean(choice &&
17
+ typeof choice === 'object' &&
18
+ 'type' in choice &&
19
+ choice.type === 'separator');
17
20
  }
18
21
  }
@@ -1,87 +1,93 @@
1
1
  import * as readline from 'node:readline';
2
2
  import { AsyncResource } from 'node:async_hooks';
3
- import { CancelablePromise } from '@inquirer/type';
4
3
  import MuteStream from 'mute-stream';
5
4
  import { onExit as onSignalExit } from 'signal-exit';
6
5
  import ScreenManager from './screen-manager.mjs';
6
+ import { CancelablePromise } from '@inquirer/type';
7
7
  import { withHooks, effectScheduler } from './hook-engine.mjs';
8
- import { CancelPromptError, ExitPromptError } from './errors.mjs';
8
+ import { AbortPromptError, CancelPromptError, ExitPromptError } from './errors.mjs';
9
9
  export function createPrompt(view) {
10
- const prompt = (config, context) => {
10
+ const prompt = (config, context = {}) => {
11
11
  // Default `input` to stdin
12
- const input = context?.input ?? process.stdin;
12
+ const { input = process.stdin, signal } = context;
13
13
  // Add mute capabilities to the output
14
14
  const output = new MuteStream();
15
- output.pipe(context?.output ?? process.stdout);
15
+ output.pipe(context.output ?? process.stdout);
16
16
  const rl = readline.createInterface({
17
17
  terminal: true,
18
18
  input,
19
19
  output,
20
20
  });
21
21
  const screen = new ScreenManager(rl);
22
- let cancel = () => { };
23
- const answer = new CancelablePromise((resolve, reject) => {
24
- withHooks(rl, (cycle) => {
25
- function checkCursorPos() {
26
- screen.checkCursorPos();
22
+ const cleanups = new Set();
23
+ const { promise, resolve, reject } = CancelablePromise.withResolver();
24
+ function onExit() {
25
+ cleanups.forEach((cleanup) => cleanup());
26
+ screen.done({ clearContent: Boolean(context?.clearPromptOnDone) });
27
+ output.end();
28
+ }
29
+ function fail(error) {
30
+ onExit();
31
+ reject(error);
32
+ }
33
+ if (signal) {
34
+ const abort = () => fail(new AbortPromptError({ cause: signal.reason }));
35
+ if (signal.aborted) {
36
+ abort();
37
+ return promise;
38
+ }
39
+ signal.addEventListener('abort', abort);
40
+ cleanups.add(() => signal.removeEventListener('abort', abort));
41
+ }
42
+ withHooks(rl, (cycle) => {
43
+ cleanups.add(onSignalExit((code, signal) => {
44
+ fail(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
45
+ }));
46
+ const hooksCleanup = AsyncResource.bind(() => {
47
+ try {
48
+ effectScheduler.clearAll();
27
49
  }
28
- const removeExitListener = onSignalExit((code, signal) => {
50
+ catch (error) {
51
+ reject(error);
52
+ }
53
+ });
54
+ cleanups.add(hooksCleanup);
55
+ // Re-renders only happen when the state change; but the readline cursor could change position
56
+ // and that also requires a re-render (and a manual one because we mute the streams).
57
+ // We set the listener after the initial workLoop to avoid a double render if render triggered
58
+ // by a state change sets the cursor to the right position.
59
+ const checkCursorPos = () => screen.checkCursorPos();
60
+ rl.input.on('keypress', checkCursorPos);
61
+ cleanups.add(() => rl.input.removeListener('keypress', checkCursorPos));
62
+ // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
63
+ // triggers after the process is done (which happens after timeouts are done triggering.)
64
+ // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
65
+ rl.on('close', hooksCleanup);
66
+ cleanups.add(() => rl.removeListener('close', hooksCleanup));
67
+ function done(value) {
68
+ // Delay execution to let time to the hookCleanup functions to registers.
69
+ setImmediate(() => {
29
70
  onExit();
30
- reject(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
71
+ // Finally we resolve our promise
72
+ resolve(value);
31
73
  });
32
- const hooksCleanup = AsyncResource.bind(() => {
33
- try {
34
- effectScheduler.clearAll();
35
- }
36
- catch (error) {
37
- reject(error);
38
- }
39
- });
40
- function onExit() {
41
- hooksCleanup();
42
- screen.done({ clearContent: Boolean(context?.clearPromptOnDone) });
43
- removeExitListener();
44
- rl.input.removeListener('keypress', checkCursorPos);
45
- rl.removeListener('close', hooksCleanup);
46
- output.end();
74
+ }
75
+ cycle(() => {
76
+ try {
77
+ const nextView = view(config, done);
78
+ const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
79
+ screen.render(content, bottomContent);
80
+ effectScheduler.run();
47
81
  }
48
- cancel = () => {
49
- onExit();
50
- reject(new CancelPromptError());
51
- };
52
- function done(value) {
53
- // Delay execution to let time to the hookCleanup functions to registers.
54
- setImmediate(() => {
55
- onExit();
56
- // Finally we resolve our promise
57
- resolve(value);
58
- });
82
+ catch (error) {
83
+ fail(error);
59
84
  }
60
- cycle(() => {
61
- try {
62
- const nextView = view(config, done);
63
- const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
64
- screen.render(content, bottomContent);
65
- effectScheduler.run();
66
- }
67
- catch (error) {
68
- onExit();
69
- reject(error);
70
- }
71
- });
72
- // Re-renders only happen when the state change; but the readline cursor could change position
73
- // and that also requires a re-render (and a manual one because we mute the streams).
74
- // We set the listener after the initial workLoop to avoid a double render if render triggered
75
- // by a state change sets the cursor to the right position.
76
- rl.input.on('keypress', checkCursorPos);
77
- // The close event triggers immediately when the user press ctrl+c. SignalExit on the other hand
78
- // triggers after the process is done (which happens after timeouts are done triggering.)
79
- // We triggers the hooks cleanup phase on rl `close` so active timeouts can be cleared.
80
- rl.on('close', hooksCleanup);
81
85
  });
82
86
  });
83
- answer.cancel = cancel;
84
- return answer;
87
+ promise.cancel = () => {
88
+ fail(new CancelPromptError());
89
+ };
90
+ return promise;
85
91
  };
86
92
  return prompt;
87
93
  }
@@ -1,3 +1,11 @@
1
+ export class AbortPromptError extends Error {
2
+ name = 'AbortPromptError';
3
+ message = 'Prompt was aborted';
4
+ constructor(options) {
5
+ super();
6
+ this.cause = options?.cause;
7
+ }
8
+ }
1
9
  export class CancelPromptError extends Error {
2
10
  name = 'CancelPromptError';
3
11
  message = 'Prompt was canceled';
@@ -6,5 +6,5 @@ export declare class Separator {
6
6
  readonly separator: string;
7
7
  readonly type = "separator";
8
8
  constructor(separator?: string);
9
- static isSeparator(choice: undefined | Separator | Record<string, unknown>): choice is Separator;
9
+ static isSeparator(choice: unknown): choice is Separator;
10
10
  }
@@ -1,3 +1,10 @@
1
+ export declare class AbortPromptError extends Error {
2
+ name: string;
3
+ message: string;
4
+ constructor(options?: {
5
+ cause?: unknown;
6
+ });
7
+ }
1
8
  export declare class CancelPromptError extends Error {
2
9
  name: string;
3
10
  message: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "9.0.10",
3
+ "version": "9.1.0",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
@@ -58,9 +58,9 @@
58
58
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/core/README.md",
59
59
  "dependencies": {
60
60
  "@inquirer/figures": "^1.0.5",
61
- "@inquirer/type": "^1.5.2",
61
+ "@inquirer/type": "^1.5.3",
62
62
  "@types/mute-stream": "^0.0.4",
63
- "@types/node": "^22.1.0",
63
+ "@types/node": "^22.5.2",
64
64
  "@types/wrap-ansi": "^3.0.0",
65
65
  "ansi-escapes": "^4.3.2",
66
66
  "cli-spinners": "^2.9.2",
@@ -72,7 +72,7 @@
72
72
  "yoctocolors-cjs": "^2.1.2"
73
73
  },
74
74
  "devDependencies": {
75
- "@inquirer/testing": "^2.1.31"
75
+ "@inquirer/testing": "^2.1.32"
76
76
  },
77
77
  "scripts": {
78
78
  "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
@@ -96,5 +96,5 @@
96
96
  }
97
97
  },
98
98
  "sideEffects": false,
99
- "gitHead": "273b16eeb96fab346854b9b92257c930d7ca7661"
99
+ "gitHead": "0c039599ef88fe9eb804fe083ee386ec906a856f"
100
100
  }