@inquirer/core 9.0.5 → 9.0.7

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.
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.createPrompt = createPrompt;
30
30
  const readline = __importStar(require("node:readline"));
31
+ const node_async_hooks_1 = require("node:async_hooks");
31
32
  const type_1 = require("@inquirer/type");
32
33
  const mute_stream_1 = __importDefault(require("mute-stream"));
33
34
  const signal_exit_1 = require("signal-exit");
@@ -50,7 +51,7 @@ function createPrompt(view) {
50
51
  const screen = new screen_manager_mjs_1.default(rl);
51
52
  let cancel = () => { };
52
53
  const answer = new type_1.CancelablePromise((resolve, reject) => {
53
- (0, hook_engine_mjs_1.withHooks)(rl, (store) => {
54
+ (0, hook_engine_mjs_1.withHooks)(rl, (cycle) => {
54
55
  function checkCursorPos() {
55
56
  screen.checkCursorPos();
56
57
  }
@@ -58,11 +59,9 @@ function createPrompt(view) {
58
59
  onExit();
59
60
  reject(new errors_mjs_1.ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
60
61
  });
61
- function onExit() {
62
+ const onExit = node_async_hooks_1.AsyncResource.bind(() => {
62
63
  try {
63
- store.hooksCleanup.forEach((cleanFn) => {
64
- cleanFn === null || cleanFn === void 0 ? void 0 : cleanFn();
65
- });
64
+ hook_engine_mjs_1.effectScheduler.clearAll();
66
65
  }
67
66
  catch (error) {
68
67
  reject(error);
@@ -75,8 +74,8 @@ function createPrompt(view) {
75
74
  }
76
75
  screen.done();
77
76
  removeExitListener();
78
- store.rl.input.removeListener('keypress', checkCursorPos);
79
- }
77
+ rl.input.removeListener('keypress', checkCursorPos);
78
+ });
80
79
  cancel = () => {
81
80
  onExit();
82
81
  reject(new errors_mjs_1.CancelPromptError());
@@ -89,8 +88,7 @@ function createPrompt(view) {
89
88
  resolve(value);
90
89
  });
91
90
  }
92
- function workLoop() {
93
- store.index = 0;
91
+ cycle(() => {
94
92
  try {
95
93
  const nextView = view(config, done);
96
94
  const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
@@ -101,14 +99,12 @@ function createPrompt(view) {
101
99
  onExit();
102
100
  reject(error);
103
101
  }
104
- }
105
- store.handleChange = () => workLoop();
106
- workLoop();
102
+ });
107
103
  // Re-renders only happen when the state change; but the readline cursor could change position
108
104
  // and that also requires a re-render (and a manual one because we mute the streams).
109
105
  // We set the listener after the initial workLoop to avoid a double render if render triggered
110
106
  // by a state change sets the cursor to the right position.
111
- store.rl.input.on('keypress', checkCursorPos);
107
+ rl.input.on('keypress', checkCursorPos);
112
108
  });
113
109
  });
114
110
  answer.cancel = cancel;
@@ -4,6 +4,12 @@ exports.ValidationError = exports.HookError = exports.ExitPromptError = exports.
4
4
  class CancelPromptError extends Error {
5
5
  constructor() {
6
6
  super(...arguments);
7
+ Object.defineProperty(this, "name", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: 'CancelPromptError'
12
+ });
7
13
  Object.defineProperty(this, "message", {
8
14
  enumerable: true,
9
15
  configurable: true,
@@ -14,11 +20,38 @@ class CancelPromptError extends Error {
14
20
  }
15
21
  exports.CancelPromptError = CancelPromptError;
16
22
  class ExitPromptError extends Error {
23
+ constructor() {
24
+ super(...arguments);
25
+ Object.defineProperty(this, "name", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: 'ExitPromptError'
30
+ });
31
+ }
17
32
  }
18
33
  exports.ExitPromptError = ExitPromptError;
19
34
  class HookError extends Error {
35
+ constructor() {
36
+ super(...arguments);
37
+ Object.defineProperty(this, "name", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: 'HookError'
42
+ });
43
+ }
20
44
  }
21
45
  exports.HookError = HookError;
22
46
  class ValidationError extends Error {
47
+ constructor() {
48
+ super(...arguments);
49
+ Object.defineProperty(this, "name", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: 'ValidationError'
54
+ });
55
+ }
23
56
  }
24
57
  exports.ValidationError = ValidationError;
@@ -25,7 +25,14 @@ function createStore(rl) {
25
25
  function withHooks(rl, cb) {
26
26
  const store = createStore(rl);
27
27
  return hookStorage.run(store, () => {
28
- cb(store);
28
+ function cycle(render) {
29
+ store.handleChange = () => {
30
+ store.index = 0;
31
+ render();
32
+ };
33
+ store.handleChange();
34
+ }
35
+ cb(cycle);
29
36
  });
30
37
  }
31
38
  // Safe getStore utility that'll return the store or throw if undefined.
@@ -101,4 +108,12 @@ exports.effectScheduler = {
101
108
  store.hooksEffect.length = 0;
102
109
  })();
103
110
  },
111
+ clearAll() {
112
+ const store = getStore();
113
+ store.hooksCleanup.forEach((cleanFn) => {
114
+ cleanFn === null || cleanFn === void 0 ? void 0 : cleanFn();
115
+ });
116
+ store.hooksEffect.length = 0;
117
+ store.hooksCleanup.length = 0;
118
+ },
104
119
  };
@@ -1,9 +1,13 @@
1
1
  export declare class CancelPromptError extends Error {
2
+ name: string;
2
3
  message: string;
3
4
  }
4
5
  export declare class ExitPromptError extends Error {
6
+ name: string;
5
7
  }
6
8
  export declare class HookError extends Error {
9
+ name: string;
7
10
  }
8
11
  export declare class ValidationError extends Error {
12
+ name: string;
9
13
  }
@@ -1,13 +1,5 @@
1
1
  import type { InquirerReadline } from '@inquirer/type';
2
- type HookStore = {
3
- rl: InquirerReadline;
4
- hooks: any[];
5
- hooksCleanup: Array<void | (() => void)>;
6
- hooksEffect: Array<() => void>;
7
- index: number;
8
- handleChange: () => void;
9
- };
10
- export declare function withHooks(rl: InquirerReadline, cb: (store: HookStore) => void): void;
2
+ export declare function withHooks(rl: InquirerReadline, cb: (cycle: (render: () => void) => void) => void): void;
11
3
  export declare function readline(): InquirerReadline;
12
4
  export declare function withUpdates<T extends (...args: any) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
13
5
  type SetPointer<Value> = {
@@ -26,5 +18,6 @@ export declare function handleChange(): void;
26
18
  export declare const effectScheduler: {
27
19
  queue(cb: (readline: InquirerReadline) => void): void;
28
20
  run(): void;
21
+ clearAll(): void;
29
22
  };
30
23
  export {};
@@ -1,4 +1,5 @@
1
1
  import * as readline from 'node:readline';
2
+ import { AsyncResource } from 'node:async_hooks';
2
3
  import { CancelablePromise } from '@inquirer/type';
3
4
  import MuteStream from 'mute-stream';
4
5
  import { onExit as onSignalExit } from 'signal-exit';
@@ -20,7 +21,7 @@ export function createPrompt(view) {
20
21
  const screen = new ScreenManager(rl);
21
22
  let cancel = () => { };
22
23
  const answer = new CancelablePromise((resolve, reject) => {
23
- withHooks(rl, (store) => {
24
+ withHooks(rl, (cycle) => {
24
25
  function checkCursorPos() {
25
26
  screen.checkCursorPos();
26
27
  }
@@ -28,11 +29,9 @@ export function createPrompt(view) {
28
29
  onExit();
29
30
  reject(new ExitPromptError(`User force closed the prompt with ${code} ${signal}`));
30
31
  });
31
- function onExit() {
32
+ const onExit = AsyncResource.bind(() => {
32
33
  try {
33
- store.hooksCleanup.forEach((cleanFn) => {
34
- cleanFn?.();
35
- });
34
+ effectScheduler.clearAll();
36
35
  }
37
36
  catch (error) {
38
37
  reject(error);
@@ -45,8 +44,8 @@ export function createPrompt(view) {
45
44
  }
46
45
  screen.done();
47
46
  removeExitListener();
48
- store.rl.input.removeListener('keypress', checkCursorPos);
49
- }
47
+ rl.input.removeListener('keypress', checkCursorPos);
48
+ });
50
49
  cancel = () => {
51
50
  onExit();
52
51
  reject(new CancelPromptError());
@@ -59,8 +58,7 @@ export function createPrompt(view) {
59
58
  resolve(value);
60
59
  });
61
60
  }
62
- function workLoop() {
63
- store.index = 0;
61
+ cycle(() => {
64
62
  try {
65
63
  const nextView = view(config, done);
66
64
  const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
@@ -71,14 +69,12 @@ export function createPrompt(view) {
71
69
  onExit();
72
70
  reject(error);
73
71
  }
74
- }
75
- store.handleChange = () => workLoop();
76
- workLoop();
72
+ });
77
73
  // Re-renders only happen when the state change; but the readline cursor could change position
78
74
  // and that also requires a re-render (and a manual one because we mute the streams).
79
75
  // We set the listener after the initial workLoop to avoid a double render if render triggered
80
76
  // by a state change sets the cursor to the right position.
81
- store.rl.input.on('keypress', checkCursorPos);
77
+ rl.input.on('keypress', checkCursorPos);
82
78
  });
83
79
  });
84
80
  answer.cancel = cancel;
@@ -1,9 +1,13 @@
1
1
  export class CancelPromptError extends Error {
2
+ name = 'CancelPromptError';
2
3
  message = 'Prompt was canceled';
3
4
  }
4
5
  export class ExitPromptError extends Error {
6
+ name = 'ExitPromptError';
5
7
  }
6
8
  export class HookError extends Error {
9
+ name = 'HookError';
7
10
  }
8
11
  export class ValidationError extends Error {
12
+ name = 'ValidationError';
9
13
  }
@@ -17,7 +17,14 @@ function createStore(rl) {
17
17
  export function withHooks(rl, cb) {
18
18
  const store = createStore(rl);
19
19
  return hookStorage.run(store, () => {
20
- cb(store);
20
+ function cycle(render) {
21
+ store.handleChange = () => {
22
+ store.index = 0;
23
+ render();
24
+ };
25
+ store.handleChange();
26
+ }
27
+ cb(cycle);
21
28
  });
22
29
  }
23
30
  // Safe getStore utility that'll return the store or throw if undefined.
@@ -92,4 +99,12 @@ export const effectScheduler = {
92
99
  store.hooksEffect.length = 0;
93
100
  })();
94
101
  },
102
+ clearAll() {
103
+ const store = getStore();
104
+ store.hooksCleanup.forEach((cleanFn) => {
105
+ cleanFn?.();
106
+ });
107
+ store.hooksEffect.length = 0;
108
+ store.hooksCleanup.length = 0;
109
+ },
95
110
  };
@@ -1,9 +1,13 @@
1
1
  export declare class CancelPromptError extends Error {
2
+ name: string;
2
3
  message: string;
3
4
  }
4
5
  export declare class ExitPromptError extends Error {
6
+ name: string;
5
7
  }
6
8
  export declare class HookError extends Error {
9
+ name: string;
7
10
  }
8
11
  export declare class ValidationError extends Error {
12
+ name: string;
9
13
  }
@@ -1,13 +1,5 @@
1
1
  import type { InquirerReadline } from '@inquirer/type';
2
- type HookStore = {
3
- rl: InquirerReadline;
4
- hooks: any[];
5
- hooksCleanup: Array<void | (() => void)>;
6
- hooksEffect: Array<() => void>;
7
- index: number;
8
- handleChange: () => void;
9
- };
10
- export declare function withHooks(rl: InquirerReadline, cb: (store: HookStore) => void): void;
2
+ export declare function withHooks(rl: InquirerReadline, cb: (cycle: (render: () => void) => void) => void): void;
11
3
  export declare function readline(): InquirerReadline;
12
4
  export declare function withUpdates<T extends (...args: any) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
13
5
  type SetPointer<Value> = {
@@ -26,5 +18,6 @@ export declare function handleChange(): void;
26
18
  export declare const effectScheduler: {
27
19
  queue(cb: (readline: InquirerReadline) => void): void;
28
20
  run(): void;
21
+ clearAll(): void;
29
22
  };
30
23
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "9.0.5",
3
+ "version": "9.0.7",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
@@ -60,7 +60,7 @@
60
60
  "@inquirer/figures": "^1.0.5",
61
61
  "@inquirer/type": "^1.5.1",
62
62
  "@types/mute-stream": "^0.0.4",
63
- "@types/node": "^20.14.11",
63
+ "@types/node": "^22.0.0",
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.28"
75
+ "@inquirer/testing": "^2.1.30"
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": "92eb67edb388404a6d737c0605648ac797c84fb6"
99
+ "gitHead": "b89972b992a65005afd6397b57aa4635010a5ef7"
100
100
  }