@inquirer/core 2.1.0 → 2.3.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.
- package/dist/cjs/index.js +38 -13
- package/dist/esm/index.mjs +38 -13
- package/package.json +3 -3
package/dist/cjs/index.js
CHANGED
|
@@ -34,20 +34,36 @@ const hooksEffect = [];
|
|
|
34
34
|
let index = 0;
|
|
35
35
|
let handleChange = () => { };
|
|
36
36
|
let sessionRl;
|
|
37
|
-
|
|
37
|
+
function resetHookState() {
|
|
38
38
|
hooks.length = 0;
|
|
39
39
|
hooksCleanup.length = 0;
|
|
40
40
|
hooksEffect.length = 0;
|
|
41
41
|
index = 0;
|
|
42
42
|
handleChange = () => { };
|
|
43
43
|
sessionRl = undefined;
|
|
44
|
-
}
|
|
45
|
-
|
|
44
|
+
}
|
|
45
|
+
function cleanupHook(index) {
|
|
46
46
|
const cleanFn = hooksCleanup[index];
|
|
47
47
|
if (typeof cleanFn === 'function') {
|
|
48
48
|
cleanFn();
|
|
49
49
|
}
|
|
50
|
-
}
|
|
50
|
+
}
|
|
51
|
+
function mergeStateUpdates(fn) {
|
|
52
|
+
const wrapped = (...args) => {
|
|
53
|
+
let shouldUpdate = false;
|
|
54
|
+
const oldHandleChange = handleChange;
|
|
55
|
+
handleChange = () => {
|
|
56
|
+
shouldUpdate = true;
|
|
57
|
+
};
|
|
58
|
+
const returnValue = fn(...args);
|
|
59
|
+
if (shouldUpdate) {
|
|
60
|
+
oldHandleChange();
|
|
61
|
+
}
|
|
62
|
+
handleChange = oldHandleChange;
|
|
63
|
+
return returnValue;
|
|
64
|
+
};
|
|
65
|
+
return wrapped;
|
|
66
|
+
}
|
|
51
67
|
function useState(defaultValue) {
|
|
52
68
|
const _idx = index;
|
|
53
69
|
index++;
|
|
@@ -62,9 +78,12 @@ function useState(defaultValue) {
|
|
|
62
78
|
return [
|
|
63
79
|
hooks[_idx],
|
|
64
80
|
(newValue) => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
81
|
+
// Noop if the value is still the same.
|
|
82
|
+
if (hooks[_idx] !== newValue) {
|
|
83
|
+
hooks[_idx] = newValue;
|
|
84
|
+
// Trigger re-render
|
|
85
|
+
handleChange();
|
|
86
|
+
}
|
|
68
87
|
},
|
|
69
88
|
];
|
|
70
89
|
}
|
|
@@ -82,14 +101,14 @@ function useEffect(cb, depArray) {
|
|
|
82
101
|
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
83
102
|
}
|
|
84
103
|
if (hasChanged) {
|
|
85
|
-
hooksEffect.push(() => {
|
|
104
|
+
hooksEffect.push(mergeStateUpdates(() => {
|
|
86
105
|
cleanupHook(_idx);
|
|
87
106
|
const cleanFn = cb(rl);
|
|
88
107
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
89
108
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
90
109
|
}
|
|
91
110
|
hooksCleanup[_idx] = cleanFn;
|
|
92
|
-
});
|
|
111
|
+
}));
|
|
93
112
|
}
|
|
94
113
|
hooks[_idx] = depArray;
|
|
95
114
|
}
|
|
@@ -100,9 +119,9 @@ function useKeypress(userHandler) {
|
|
|
100
119
|
throw new Error('useKeypress must be used within a prompt');
|
|
101
120
|
}
|
|
102
121
|
useEffect(() => {
|
|
103
|
-
const handler = (_input, event) => {
|
|
122
|
+
const handler = mergeStateUpdates((_input, event) => {
|
|
104
123
|
userHandler(event, rl);
|
|
105
|
-
};
|
|
124
|
+
});
|
|
106
125
|
rl.input.on('keypress', handler);
|
|
107
126
|
return () => {
|
|
108
127
|
rl.input.removeListener('keypress', handler);
|
|
@@ -117,8 +136,9 @@ exports.useRef = useRef;
|
|
|
117
136
|
function createPrompt(view) {
|
|
118
137
|
const prompt = (config, context) => {
|
|
119
138
|
var _a, _b;
|
|
120
|
-
|
|
121
|
-
|
|
139
|
+
if (sessionRl) {
|
|
140
|
+
throw new Error('An inquirer prompt is already running.\nMake sure you await the result of the previous prompt before calling another prompt.');
|
|
141
|
+
}
|
|
122
142
|
// Default `input` to stdin
|
|
123
143
|
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
124
144
|
// Add mute capabilities to the output
|
|
@@ -149,6 +169,7 @@ function createPrompt(view) {
|
|
|
149
169
|
screen.clearContent();
|
|
150
170
|
}
|
|
151
171
|
screen.done();
|
|
172
|
+
resetHookState();
|
|
152
173
|
process.removeListener('SIGINT', onForceExit);
|
|
153
174
|
};
|
|
154
175
|
cancel = () => {
|
|
@@ -186,12 +207,16 @@ function createPrompt(view) {
|
|
|
186
207
|
screen.render(content, bottomContent);
|
|
187
208
|
}
|
|
188
209
|
catch (err) {
|
|
210
|
+
onExit();
|
|
189
211
|
reject(err);
|
|
190
212
|
}
|
|
191
213
|
};
|
|
192
214
|
// TODO: we should display a loader while we get the default options.
|
|
193
215
|
(0, options_mjs_1.getPromptConfig)(config).then(workLoop, reject);
|
|
194
216
|
});
|
|
217
|
+
answer.catch(() => {
|
|
218
|
+
resetHookState();
|
|
219
|
+
});
|
|
195
220
|
answer.cancel = cancel;
|
|
196
221
|
return answer;
|
|
197
222
|
};
|
package/dist/esm/index.mjs
CHANGED
|
@@ -13,20 +13,36 @@ const hooksEffect = [];
|
|
|
13
13
|
let index = 0;
|
|
14
14
|
let handleChange = () => { };
|
|
15
15
|
let sessionRl;
|
|
16
|
-
|
|
16
|
+
function resetHookState() {
|
|
17
17
|
hooks.length = 0;
|
|
18
18
|
hooksCleanup.length = 0;
|
|
19
19
|
hooksEffect.length = 0;
|
|
20
20
|
index = 0;
|
|
21
21
|
handleChange = () => { };
|
|
22
22
|
sessionRl = undefined;
|
|
23
|
-
}
|
|
24
|
-
|
|
23
|
+
}
|
|
24
|
+
function cleanupHook(index) {
|
|
25
25
|
const cleanFn = hooksCleanup[index];
|
|
26
26
|
if (typeof cleanFn === 'function') {
|
|
27
27
|
cleanFn();
|
|
28
28
|
}
|
|
29
|
-
}
|
|
29
|
+
}
|
|
30
|
+
function mergeStateUpdates(fn) {
|
|
31
|
+
const wrapped = (...args) => {
|
|
32
|
+
let shouldUpdate = false;
|
|
33
|
+
const oldHandleChange = handleChange;
|
|
34
|
+
handleChange = () => {
|
|
35
|
+
shouldUpdate = true;
|
|
36
|
+
};
|
|
37
|
+
const returnValue = fn(...args);
|
|
38
|
+
if (shouldUpdate) {
|
|
39
|
+
oldHandleChange();
|
|
40
|
+
}
|
|
41
|
+
handleChange = oldHandleChange;
|
|
42
|
+
return returnValue;
|
|
43
|
+
};
|
|
44
|
+
return wrapped;
|
|
45
|
+
}
|
|
30
46
|
export function useState(defaultValue) {
|
|
31
47
|
const _idx = index;
|
|
32
48
|
index++;
|
|
@@ -41,9 +57,12 @@ export function useState(defaultValue) {
|
|
|
41
57
|
return [
|
|
42
58
|
hooks[_idx],
|
|
43
59
|
(newValue) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
// Noop if the value is still the same.
|
|
61
|
+
if (hooks[_idx] !== newValue) {
|
|
62
|
+
hooks[_idx] = newValue;
|
|
63
|
+
// Trigger re-render
|
|
64
|
+
handleChange();
|
|
65
|
+
}
|
|
47
66
|
},
|
|
48
67
|
];
|
|
49
68
|
}
|
|
@@ -60,14 +79,14 @@ export function useEffect(cb, depArray) {
|
|
|
60
79
|
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
61
80
|
}
|
|
62
81
|
if (hasChanged) {
|
|
63
|
-
hooksEffect.push(() => {
|
|
82
|
+
hooksEffect.push(mergeStateUpdates(() => {
|
|
64
83
|
cleanupHook(_idx);
|
|
65
84
|
const cleanFn = cb(rl);
|
|
66
85
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
67
86
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
68
87
|
}
|
|
69
88
|
hooksCleanup[_idx] = cleanFn;
|
|
70
|
-
});
|
|
89
|
+
}));
|
|
71
90
|
}
|
|
72
91
|
hooks[_idx] = depArray;
|
|
73
92
|
}
|
|
@@ -77,9 +96,9 @@ export function useKeypress(userHandler) {
|
|
|
77
96
|
throw new Error('useKeypress must be used within a prompt');
|
|
78
97
|
}
|
|
79
98
|
useEffect(() => {
|
|
80
|
-
const handler = (_input, event) => {
|
|
99
|
+
const handler = mergeStateUpdates((_input, event) => {
|
|
81
100
|
userHandler(event, rl);
|
|
82
|
-
};
|
|
101
|
+
});
|
|
83
102
|
rl.input.on('keypress', handler);
|
|
84
103
|
return () => {
|
|
85
104
|
rl.input.removeListener('keypress', handler);
|
|
@@ -91,8 +110,9 @@ export function useRef(val) {
|
|
|
91
110
|
}
|
|
92
111
|
export function createPrompt(view) {
|
|
93
112
|
const prompt = (config, context) => {
|
|
94
|
-
|
|
95
|
-
|
|
113
|
+
if (sessionRl) {
|
|
114
|
+
throw new Error('An inquirer prompt is already running.\nMake sure you await the result of the previous prompt before calling another prompt.');
|
|
115
|
+
}
|
|
96
116
|
// Default `input` to stdin
|
|
97
117
|
const input = context?.input ?? process.stdin;
|
|
98
118
|
// Add mute capabilities to the output
|
|
@@ -123,6 +143,7 @@ export function createPrompt(view) {
|
|
|
123
143
|
screen.clearContent();
|
|
124
144
|
}
|
|
125
145
|
screen.done();
|
|
146
|
+
resetHookState();
|
|
126
147
|
process.removeListener('SIGINT', onForceExit);
|
|
127
148
|
};
|
|
128
149
|
cancel = () => {
|
|
@@ -160,12 +181,16 @@ export function createPrompt(view) {
|
|
|
160
181
|
screen.render(content, bottomContent);
|
|
161
182
|
}
|
|
162
183
|
catch (err) {
|
|
184
|
+
onExit();
|
|
163
185
|
reject(err);
|
|
164
186
|
}
|
|
165
187
|
};
|
|
166
188
|
// TODO: we should display a loader while we get the default options.
|
|
167
189
|
getPromptConfig(config).then(workLoop, reject);
|
|
168
190
|
});
|
|
191
|
+
answer.catch(() => {
|
|
192
|
+
resetHookState();
|
|
193
|
+
});
|
|
169
194
|
answer.cancel = cancel;
|
|
170
195
|
return answer;
|
|
171
196
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=14.18.0"
|
|
6
6
|
},
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"wrap-ansi": "^6.0.1"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@inquirer/testing": "^2.
|
|
73
|
+
"@inquirer/testing": "^2.1.0"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
|
|
@@ -94,5 +94,5 @@
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
},
|
|
97
|
-
"gitHead": "
|
|
97
|
+
"gitHead": "4c139f50414bd538eae1d48e61fd2586581d3e70"
|
|
98
98
|
}
|