inquirerer 2.3.1 → 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 +28 -0
- package/esm/keypress.js +35 -113
- package/keypress.d.ts +14 -5
- package/keypress.js +35 -113
- package/package.json +2 -2
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,63 +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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
owner.handleKey(key);
|
|
27
|
-
// Handle Ctrl+C via the current owner's process wrapper
|
|
28
|
-
if (key === KEY_CODES.CTRL_C) {
|
|
29
|
-
owner.exitProcess(0);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
state = {
|
|
34
|
-
dataHandler,
|
|
35
|
-
instances: new Set(),
|
|
36
|
-
activeStack: [],
|
|
37
|
-
rawModeSet: false
|
|
38
|
-
};
|
|
39
|
-
sharedInputStates.set(input, state);
|
|
40
|
-
input.on('data', dataHandler);
|
|
41
|
-
}
|
|
42
|
-
return state;
|
|
43
|
-
}
|
|
44
|
-
function removeFromSharedState(input, instance) {
|
|
45
|
-
const state = sharedInputStates.get(input);
|
|
46
|
-
if (!state)
|
|
47
|
-
return;
|
|
48
|
-
state.instances.delete(instance);
|
|
49
|
-
// Remove from active stack as well
|
|
50
|
-
const stackIndex = state.activeStack.indexOf(instance);
|
|
51
|
-
if (stackIndex !== -1) {
|
|
52
|
-
state.activeStack.splice(stackIndex, 1);
|
|
53
|
-
}
|
|
54
|
-
// If stack is now empty, disable raw mode
|
|
55
|
-
if (state.activeStack.length === 0 && state.rawModeSet) {
|
|
56
|
-
if (typeof input.setRawMode === 'function') {
|
|
57
|
-
input.setRawMode(false);
|
|
58
|
-
}
|
|
59
|
-
state.rawModeSet = false;
|
|
60
|
-
}
|
|
61
|
-
if (state.instances.size === 0) {
|
|
62
|
-
input.removeListener('data', state.dataHandler);
|
|
63
|
-
sharedInputStates.delete(input);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
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
|
+
*/
|
|
66
26
|
export class TerminalKeypress {
|
|
67
27
|
listeners = {};
|
|
28
|
+
active = true;
|
|
68
29
|
noTty;
|
|
69
30
|
input;
|
|
70
31
|
proc;
|
|
71
|
-
|
|
32
|
+
dataHandler = null;
|
|
72
33
|
constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
|
|
73
34
|
this.noTty = noTty;
|
|
74
35
|
this.input = input;
|
|
@@ -77,32 +38,22 @@ export class TerminalKeypress {
|
|
|
77
38
|
this.input.resume();
|
|
78
39
|
this.input.setEncoding('utf8');
|
|
79
40
|
}
|
|
80
|
-
this.
|
|
81
|
-
}
|
|
82
|
-
registerWithSharedState() {
|
|
83
|
-
const state = getOrCreateSharedState(this.input);
|
|
84
|
-
state.instances.add(this);
|
|
41
|
+
this.setupListeners();
|
|
85
42
|
}
|
|
86
43
|
isTTY() {
|
|
87
44
|
return !this.noTty;
|
|
88
45
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
this.
|
|
100
|
-
}
|
|
101
|
-
handleKey(key) {
|
|
102
|
-
if (this.destroyed)
|
|
103
|
-
return;
|
|
104
|
-
const handlers = this.listeners[key];
|
|
105
|
-
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);
|
|
106
57
|
}
|
|
107
58
|
on(key, callback) {
|
|
108
59
|
if (!this.listeners[key]) {
|
|
@@ -122,52 +73,23 @@ export class TerminalKeypress {
|
|
|
122
73
|
this.listeners = {};
|
|
123
74
|
}
|
|
124
75
|
pause() {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return;
|
|
128
|
-
// Remove from active stack (from anywhere, not just top)
|
|
129
|
-
const stackIndex = state.activeStack.indexOf(this);
|
|
130
|
-
if (stackIndex !== -1) {
|
|
131
|
-
state.activeStack.splice(stackIndex, 1);
|
|
132
|
-
}
|
|
133
|
-
// If stack is now empty, disable raw mode
|
|
134
|
-
if (state.activeStack.length === 0 && state.rawModeSet) {
|
|
135
|
-
if (this.isTTY() && typeof this.input.setRawMode === 'function') {
|
|
136
|
-
this.input.setRawMode(false);
|
|
137
|
-
}
|
|
138
|
-
state.rawModeSet = false;
|
|
139
|
-
}
|
|
76
|
+
this.active = false;
|
|
77
|
+
this.clearHandlers();
|
|
140
78
|
}
|
|
141
79
|
resume() {
|
|
142
|
-
|
|
143
|
-
return;
|
|
144
|
-
const state = sharedInputStates.get(this.input);
|
|
145
|
-
if (!state)
|
|
146
|
-
return;
|
|
147
|
-
// Move-to-top semantics: remove from anywhere in stack, then push to top
|
|
148
|
-
const existingIndex = state.activeStack.indexOf(this);
|
|
149
|
-
if (existingIndex !== -1) {
|
|
150
|
-
state.activeStack.splice(existingIndex, 1);
|
|
151
|
-
}
|
|
152
|
-
state.activeStack.push(this);
|
|
153
|
-
// Enable raw mode if TTY
|
|
80
|
+
this.active = true;
|
|
154
81
|
if (this.isTTY() && typeof this.input.setRawMode === 'function') {
|
|
155
82
|
this.input.setRawMode(true);
|
|
156
|
-
state.rawModeSet = true;
|
|
157
83
|
}
|
|
158
84
|
}
|
|
159
85
|
destroy() {
|
|
160
|
-
if (this.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
this.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (typeof this.input.setRawMode === 'function') {
|
|
168
|
-
this.input.setRawMode(false);
|
|
169
|
-
}
|
|
170
|
-
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;
|
|
171
93
|
}
|
|
172
94
|
}
|
|
173
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;
|
|
30
|
+
private active;
|
|
19
31
|
private noTty;
|
|
20
32
|
private input;
|
|
21
33
|
private proc;
|
|
22
|
-
private
|
|
34
|
+
private dataHandler;
|
|
23
35
|
constructor(noTty?: boolean, input?: Readable, proc?: ProcessWrapper);
|
|
24
|
-
private registerWithSharedState;
|
|
25
36
|
isTTY(): boolean;
|
|
26
|
-
|
|
27
|
-
exitProcess(code?: number): void;
|
|
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,63 +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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
owner.handleKey(key);
|
|
30
|
-
// Handle Ctrl+C via the current owner's process wrapper
|
|
31
|
-
if (key === exports.KEY_CODES.CTRL_C) {
|
|
32
|
-
owner.exitProcess(0);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
state = {
|
|
37
|
-
dataHandler,
|
|
38
|
-
instances: new Set(),
|
|
39
|
-
activeStack: [],
|
|
40
|
-
rawModeSet: false
|
|
41
|
-
};
|
|
42
|
-
sharedInputStates.set(input, state);
|
|
43
|
-
input.on('data', dataHandler);
|
|
44
|
-
}
|
|
45
|
-
return state;
|
|
46
|
-
}
|
|
47
|
-
function removeFromSharedState(input, instance) {
|
|
48
|
-
const state = sharedInputStates.get(input);
|
|
49
|
-
if (!state)
|
|
50
|
-
return;
|
|
51
|
-
state.instances.delete(instance);
|
|
52
|
-
// Remove from active stack as well
|
|
53
|
-
const stackIndex = state.activeStack.indexOf(instance);
|
|
54
|
-
if (stackIndex !== -1) {
|
|
55
|
-
state.activeStack.splice(stackIndex, 1);
|
|
56
|
-
}
|
|
57
|
-
// If stack is now empty, disable raw mode
|
|
58
|
-
if (state.activeStack.length === 0 && state.rawModeSet) {
|
|
59
|
-
if (typeof input.setRawMode === 'function') {
|
|
60
|
-
input.setRawMode(false);
|
|
61
|
-
}
|
|
62
|
-
state.rawModeSet = false;
|
|
63
|
-
}
|
|
64
|
-
if (state.instances.size === 0) {
|
|
65
|
-
input.removeListener('data', state.dataHandler);
|
|
66
|
-
sharedInputStates.delete(input);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
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
|
+
*/
|
|
69
29
|
class TerminalKeypress {
|
|
70
30
|
listeners = {};
|
|
31
|
+
active = true;
|
|
71
32
|
noTty;
|
|
72
33
|
input;
|
|
73
34
|
proc;
|
|
74
|
-
|
|
35
|
+
dataHandler = null;
|
|
75
36
|
constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
|
|
76
37
|
this.noTty = noTty;
|
|
77
38
|
this.input = input;
|
|
@@ -80,32 +41,22 @@ class TerminalKeypress {
|
|
|
80
41
|
this.input.resume();
|
|
81
42
|
this.input.setEncoding('utf8');
|
|
82
43
|
}
|
|
83
|
-
this.
|
|
84
|
-
}
|
|
85
|
-
registerWithSharedState() {
|
|
86
|
-
const state = getOrCreateSharedState(this.input);
|
|
87
|
-
state.instances.add(this);
|
|
44
|
+
this.setupListeners();
|
|
88
45
|
}
|
|
89
46
|
isTTY() {
|
|
90
47
|
return !this.noTty;
|
|
91
48
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.
|
|
103
|
-
}
|
|
104
|
-
handleKey(key) {
|
|
105
|
-
if (this.destroyed)
|
|
106
|
-
return;
|
|
107
|
-
const handlers = this.listeners[key];
|
|
108
|
-
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);
|
|
109
60
|
}
|
|
110
61
|
on(key, callback) {
|
|
111
62
|
if (!this.listeners[key]) {
|
|
@@ -125,52 +76,23 @@ class TerminalKeypress {
|
|
|
125
76
|
this.listeners = {};
|
|
126
77
|
}
|
|
127
78
|
pause() {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return;
|
|
131
|
-
// Remove from active stack (from anywhere, not just top)
|
|
132
|
-
const stackIndex = state.activeStack.indexOf(this);
|
|
133
|
-
if (stackIndex !== -1) {
|
|
134
|
-
state.activeStack.splice(stackIndex, 1);
|
|
135
|
-
}
|
|
136
|
-
// If stack is now empty, disable raw mode
|
|
137
|
-
if (state.activeStack.length === 0 && state.rawModeSet) {
|
|
138
|
-
if (this.isTTY() && typeof this.input.setRawMode === 'function') {
|
|
139
|
-
this.input.setRawMode(false);
|
|
140
|
-
}
|
|
141
|
-
state.rawModeSet = false;
|
|
142
|
-
}
|
|
79
|
+
this.active = false;
|
|
80
|
+
this.clearHandlers();
|
|
143
81
|
}
|
|
144
82
|
resume() {
|
|
145
|
-
|
|
146
|
-
return;
|
|
147
|
-
const state = sharedInputStates.get(this.input);
|
|
148
|
-
if (!state)
|
|
149
|
-
return;
|
|
150
|
-
// Move-to-top semantics: remove from anywhere in stack, then push to top
|
|
151
|
-
const existingIndex = state.activeStack.indexOf(this);
|
|
152
|
-
if (existingIndex !== -1) {
|
|
153
|
-
state.activeStack.splice(existingIndex, 1);
|
|
154
|
-
}
|
|
155
|
-
state.activeStack.push(this);
|
|
156
|
-
// Enable raw mode if TTY
|
|
83
|
+
this.active = true;
|
|
157
84
|
if (this.isTTY() && typeof this.input.setRawMode === 'function') {
|
|
158
85
|
this.input.setRawMode(true);
|
|
159
|
-
state.rawModeSet = true;
|
|
160
86
|
}
|
|
161
87
|
}
|
|
162
88
|
destroy() {
|
|
163
|
-
if (this.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
this.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (typeof this.input.setRawMode === 'function') {
|
|
171
|
-
this.input.setRawMode(false);
|
|
172
|
-
}
|
|
173
|
-
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;
|
|
174
96
|
}
|
|
175
97
|
}
|
|
176
98
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "2.3.
|
|
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": "
|
|
44
|
+
"gitHead": "acd6339f6890d5609a9d7b7ce2799d752b2a766e"
|
|
45
45
|
}
|