inquirerer 2.3.0 → 2.3.2

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
@@ -189,6 +189,34 @@ close(): void
189
189
  exit(): void
190
190
  ```
191
191
 
192
+ #### Managing Multiple Instances
193
+
194
+ When working with multiple `Inquirerer` instances that share the same input stream (typically `process.stdin`), only one instance should be actively prompting at a time. Each instance attaches its own keyboard listener, so having multiple active instances will cause duplicate or unexpected keypress behavior.
195
+
196
+ **Best practices:**
197
+
198
+ 1. **Reuse a single instance** - Create one `Inquirerer` instance and reuse it for all prompts:
199
+ ```typescript
200
+ const prompter = new Inquirerer();
201
+
202
+ // Use the same instance for multiple prompt sessions
203
+ const answers1 = await prompter.prompt({}, questions1);
204
+ const answers2 = await prompter.prompt({}, questions2);
205
+
206
+ prompter.close(); // Clean up when done
207
+ ```
208
+
209
+ 2. **Close before creating another** - If you need separate instances, close the first before using the second:
210
+ ```typescript
211
+ const prompter1 = new Inquirerer();
212
+ const answers1 = await prompter1.prompt({}, questions1);
213
+ prompter1.close(); // Important: close before creating another
214
+
215
+ const prompter2 = new Inquirerer();
216
+ const answers2 = await prompter2.prompt({}, questions2);
217
+ prompter2.close();
218
+ ```
219
+
192
220
  ### Question Types
193
221
 
194
222
  #### Text Question
package/esm/keypress.js CHANGED
@@ -12,53 +12,24 @@ export const KEY_CODES = {
12
12
  BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
13
13
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
14
14
  };
15
- const sharedInputStates = new WeakMap();
16
- function getOrCreateSharedState(input, proc) {
17
- let state = sharedInputStates.get(input);
18
- if (!state) {
19
- const dataHandler = (key) => {
20
- const currentState = sharedInputStates.get(input);
21
- if (!currentState)
22
- return;
23
- for (const instance of currentState.instances) {
24
- if (instance.isActive()) {
25
- instance.handleKey(key);
26
- }
27
- }
28
- if (key === KEY_CODES.CTRL_C) {
29
- proc.exit(0);
30
- }
31
- };
32
- state = {
33
- dataHandler,
34
- instances: new Set(),
35
- rawModeSet: false
36
- };
37
- sharedInputStates.set(input, state);
38
- input.on('data', dataHandler);
39
- }
40
- return state;
41
- }
42
- function removeFromSharedState(input, instance) {
43
- const state = sharedInputStates.get(input);
44
- if (!state)
45
- return;
46
- state.instances.delete(instance);
47
- if (state.instances.size === 0) {
48
- input.removeListener('data', state.dataHandler);
49
- sharedInputStates.delete(input);
50
- if (state.rawModeSet && typeof input.setRawMode === 'function') {
51
- input.setRawMode(false);
52
- }
53
- }
54
- }
15
+ /**
16
+ * Handles keyboard input for interactive prompts.
17
+ *
18
+ * **Important**: Only one TerminalKeypress instance should be actively listening
19
+ * on a given input stream at a time. If you need multiple Inquirerer instances,
20
+ * call `close()` on the first instance before using the second, or reuse a single
21
+ * instance for all prompts.
22
+ *
23
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
24
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
25
+ */
55
26
  export class TerminalKeypress {
56
27
  listeners = {};
57
28
  active = true;
58
29
  noTty;
59
30
  input;
60
31
  proc;
61
- destroyed = false;
32
+ dataHandler = null;
62
33
  constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
63
34
  this.noTty = noTty;
64
35
  this.input = input;
@@ -67,23 +38,22 @@ export class TerminalKeypress {
67
38
  this.input.resume();
68
39
  this.input.setEncoding('utf8');
69
40
  }
70
- this.registerWithSharedState();
71
- }
72
- registerWithSharedState() {
73
- const state = getOrCreateSharedState(this.input, this.proc);
74
- state.instances.add(this);
41
+ this.setupListeners();
75
42
  }
76
43
  isTTY() {
77
44
  return !this.noTty;
78
45
  }
79
- isActive() {
80
- return this.active && !this.destroyed;
81
- }
82
- handleKey(key) {
83
- if (!this.active || this.destroyed)
84
- return;
85
- const handlers = this.listeners[key];
86
- handlers?.forEach(handler => handler());
46
+ setupListeners() {
47
+ this.dataHandler = (key) => {
48
+ if (!this.active)
49
+ return;
50
+ const handlers = this.listeners[key];
51
+ handlers?.forEach(handler => handler());
52
+ if (key === KEY_CODES.CTRL_C) {
53
+ this.proc.exit(0);
54
+ }
55
+ };
56
+ this.input.on('data', this.dataHandler);
87
57
  }
88
58
  on(key, callback) {
89
59
  if (!this.listeners[key]) {
@@ -110,25 +80,16 @@ export class TerminalKeypress {
110
80
  this.active = true;
111
81
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
112
82
  this.input.setRawMode(true);
113
- const state = sharedInputStates.get(this.input);
114
- if (state) {
115
- state.rawModeSet = true;
116
- }
117
83
  }
118
84
  }
119
85
  destroy() {
120
- if (this.destroyed)
121
- return;
122
- this.destroyed = true;
123
- this.active = false;
124
- this.clearHandlers();
125
- removeFromSharedState(this.input, this);
126
- const state = sharedInputStates.get(this.input);
127
- if (!state || state.instances.size === 0) {
128
- if (typeof this.input.setRawMode === 'function') {
129
- this.input.setRawMode(false);
130
- }
131
- this.input.pause();
86
+ if (typeof this.input.setRawMode === 'function') {
87
+ this.input.setRawMode(false);
88
+ }
89
+ this.input.pause();
90
+ if (this.dataHandler) {
91
+ this.input.removeListener('data', this.dataHandler);
92
+ this.dataHandler = null;
132
93
  }
133
94
  }
134
95
  }
package/keypress.d.ts CHANGED
@@ -14,18 +14,27 @@ export declare const KEY_CODES: {
14
14
  BACKSPACE: string;
15
15
  BACKSPACE_LEGACY: string;
16
16
  };
17
+ /**
18
+ * Handles keyboard input for interactive prompts.
19
+ *
20
+ * **Important**: Only one TerminalKeypress instance should be actively listening
21
+ * on a given input stream at a time. If you need multiple Inquirerer instances,
22
+ * call `close()` on the first instance before using the second, or reuse a single
23
+ * instance for all prompts.
24
+ *
25
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
26
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
27
+ */
17
28
  export declare class TerminalKeypress {
18
29
  private listeners;
19
30
  private active;
20
31
  private noTty;
21
32
  private input;
22
33
  private proc;
23
- private destroyed;
34
+ private dataHandler;
24
35
  constructor(noTty?: boolean, input?: Readable, proc?: ProcessWrapper);
25
- private registerWithSharedState;
26
36
  isTTY(): boolean;
27
- isActive(): boolean;
28
- handleKey(key: string): void;
37
+ private setupListeners;
29
38
  on(key: string, callback: KeyHandler): void;
30
39
  off(key: string, callback: KeyHandler): void;
31
40
  clearHandlers(): void;
package/keypress.js CHANGED
@@ -15,53 +15,24 @@ exports.KEY_CODES = {
15
15
  BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
16
16
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
17
17
  };
18
- const sharedInputStates = new WeakMap();
19
- function getOrCreateSharedState(input, proc) {
20
- let state = sharedInputStates.get(input);
21
- if (!state) {
22
- const dataHandler = (key) => {
23
- const currentState = sharedInputStates.get(input);
24
- if (!currentState)
25
- return;
26
- for (const instance of currentState.instances) {
27
- if (instance.isActive()) {
28
- instance.handleKey(key);
29
- }
30
- }
31
- if (key === exports.KEY_CODES.CTRL_C) {
32
- proc.exit(0);
33
- }
34
- };
35
- state = {
36
- dataHandler,
37
- instances: new Set(),
38
- rawModeSet: false
39
- };
40
- sharedInputStates.set(input, state);
41
- input.on('data', dataHandler);
42
- }
43
- return state;
44
- }
45
- function removeFromSharedState(input, instance) {
46
- const state = sharedInputStates.get(input);
47
- if (!state)
48
- return;
49
- state.instances.delete(instance);
50
- if (state.instances.size === 0) {
51
- input.removeListener('data', state.dataHandler);
52
- sharedInputStates.delete(input);
53
- if (state.rawModeSet && typeof input.setRawMode === 'function') {
54
- input.setRawMode(false);
55
- }
56
- }
57
- }
18
+ /**
19
+ * Handles keyboard input for interactive prompts.
20
+ *
21
+ * **Important**: Only one TerminalKeypress instance should be actively listening
22
+ * on a given input stream at a time. If you need multiple Inquirerer instances,
23
+ * call `close()` on the first instance before using the second, or reuse a single
24
+ * instance for all prompts.
25
+ *
26
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
27
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
28
+ */
58
29
  class TerminalKeypress {
59
30
  listeners = {};
60
31
  active = true;
61
32
  noTty;
62
33
  input;
63
34
  proc;
64
- destroyed = false;
35
+ dataHandler = null;
65
36
  constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
66
37
  this.noTty = noTty;
67
38
  this.input = input;
@@ -70,23 +41,22 @@ class TerminalKeypress {
70
41
  this.input.resume();
71
42
  this.input.setEncoding('utf8');
72
43
  }
73
- this.registerWithSharedState();
74
- }
75
- registerWithSharedState() {
76
- const state = getOrCreateSharedState(this.input, this.proc);
77
- state.instances.add(this);
44
+ this.setupListeners();
78
45
  }
79
46
  isTTY() {
80
47
  return !this.noTty;
81
48
  }
82
- isActive() {
83
- return this.active && !this.destroyed;
84
- }
85
- handleKey(key) {
86
- if (!this.active || this.destroyed)
87
- return;
88
- const handlers = this.listeners[key];
89
- handlers?.forEach(handler => handler());
49
+ setupListeners() {
50
+ this.dataHandler = (key) => {
51
+ if (!this.active)
52
+ return;
53
+ const handlers = this.listeners[key];
54
+ handlers?.forEach(handler => handler());
55
+ if (key === exports.KEY_CODES.CTRL_C) {
56
+ this.proc.exit(0);
57
+ }
58
+ };
59
+ this.input.on('data', this.dataHandler);
90
60
  }
91
61
  on(key, callback) {
92
62
  if (!this.listeners[key]) {
@@ -113,25 +83,16 @@ class TerminalKeypress {
113
83
  this.active = true;
114
84
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
115
85
  this.input.setRawMode(true);
116
- const state = sharedInputStates.get(this.input);
117
- if (state) {
118
- state.rawModeSet = true;
119
- }
120
86
  }
121
87
  }
122
88
  destroy() {
123
- if (this.destroyed)
124
- return;
125
- this.destroyed = true;
126
- this.active = false;
127
- this.clearHandlers();
128
- removeFromSharedState(this.input, this);
129
- const state = sharedInputStates.get(this.input);
130
- if (!state || state.instances.size === 0) {
131
- if (typeof this.input.setRawMode === 'function') {
132
- this.input.setRawMode(false);
133
- }
134
- this.input.pause();
89
+ if (typeof this.input.setRawMode === 'function') {
90
+ this.input.setRawMode(false);
91
+ }
92
+ this.input.pause();
93
+ if (this.dataHandler) {
94
+ this.input.removeListener('data', this.dataHandler);
95
+ this.dataHandler = null;
135
96
  }
136
97
  }
137
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
6
6
  "main": "index.js",
@@ -41,5 +41,5 @@
41
41
  "clean-ansi": "0.1.5",
42
42
  "makage": "0.1.8"
43
43
  },
44
- "gitHead": "264e5515ffaa2089b420f7c96afc287eec93196b"
44
+ "gitHead": "acd6339f6890d5609a9d7b7ce2799d752b2a766e"
45
45
  }