@inquirer/core 3.1.1 → 4.0.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 +177 -171
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/esm/index.mjs +174 -169
- package/dist/esm/types/index.d.mts +1 -1
- package/package.json +6 -7
package/dist/cjs/index.js
CHANGED
|
@@ -29,8 +29,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
29
29
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
30
|
};
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.createPrompt = exports.usePagination = exports.
|
|
32
|
+
exports.createPrompt = exports.usePagination = exports.useKeypress = exports.useRef = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
33
33
|
const readline = __importStar(require("node:readline"));
|
|
34
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
34
35
|
const type_1 = require("@inquirer/type");
|
|
35
36
|
const chalk_1 = __importDefault(require("chalk"));
|
|
36
37
|
const cli_width_1 = __importDefault(require("cli-width"));
|
|
@@ -42,120 +43,126 @@ var prefix_mjs_1 = require('./lib/prefix.js');
|
|
|
42
43
|
Object.defineProperty(exports, "usePrefix", { enumerable: true, get: function () { return prefix_mjs_1.usePrefix; } });
|
|
43
44
|
__exportStar(require('./lib/key.js'), exports);
|
|
44
45
|
__exportStar(require('./lib/Separator.js'), exports);
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function resetHookState() {
|
|
52
|
-
hooks.length = 0;
|
|
53
|
-
hooksCleanup.length = 0;
|
|
54
|
-
hooksEffect.length = 0;
|
|
55
|
-
index = 0;
|
|
56
|
-
handleChange = () => { };
|
|
57
|
-
sessionRl = undefined;
|
|
58
|
-
}
|
|
59
|
-
function cleanupHook(index) {
|
|
60
|
-
const cleanFn = hooksCleanup[index];
|
|
61
|
-
if (typeof cleanFn === 'function') {
|
|
62
|
-
cleanFn();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function mergeStateUpdates(fn) {
|
|
66
|
-
const wrapped = (...args) => {
|
|
67
|
-
let shouldUpdate = false;
|
|
68
|
-
const oldHandleChange = handleChange;
|
|
69
|
-
handleChange = () => {
|
|
70
|
-
shouldUpdate = true;
|
|
71
|
-
};
|
|
72
|
-
const returnValue = fn(...args);
|
|
73
|
-
if (shouldUpdate) {
|
|
74
|
-
oldHandleChange();
|
|
46
|
+
const hookStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
47
|
+
const context = {
|
|
48
|
+
getStore() {
|
|
49
|
+
const store = hookStorage.getStore();
|
|
50
|
+
if (!store) {
|
|
51
|
+
throw new Error('[Inquirer] Hook functions can only be called from within a prompt');
|
|
75
52
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
53
|
+
return store;
|
|
54
|
+
},
|
|
55
|
+
withPointer(cb) {
|
|
56
|
+
const store = context.getStore();
|
|
57
|
+
const value = cb(store.index, store);
|
|
58
|
+
store.index++;
|
|
59
|
+
return value;
|
|
60
|
+
},
|
|
61
|
+
handleChange() {
|
|
62
|
+
const { handleChange } = context.getStore();
|
|
63
|
+
handleChange();
|
|
64
|
+
},
|
|
65
|
+
mergeStateUpdates(fn) {
|
|
66
|
+
const wrapped = (...args) => {
|
|
67
|
+
const store = context.getStore();
|
|
68
|
+
let shouldUpdate = false;
|
|
69
|
+
const oldHandleChange = store.handleChange;
|
|
70
|
+
store.handleChange = () => {
|
|
71
|
+
shouldUpdate = true;
|
|
72
|
+
};
|
|
73
|
+
const returnValue = fn(...args);
|
|
74
|
+
if (shouldUpdate) {
|
|
75
|
+
oldHandleChange();
|
|
76
|
+
}
|
|
77
|
+
store.handleChange = oldHandleChange;
|
|
78
|
+
return returnValue;
|
|
79
|
+
};
|
|
80
|
+
return wrapped;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
const effectScheduler = {
|
|
84
|
+
queue(cb) {
|
|
85
|
+
const store = context.getStore();
|
|
86
|
+
const { index } = store;
|
|
87
|
+
store.hooksEffect.push(() => {
|
|
88
|
+
var _a, _b;
|
|
89
|
+
(_b = (_a = store.hooksCleanup)[index]) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
90
|
+
const cleanFn = cb(store.rl);
|
|
91
|
+
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
92
|
+
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
93
|
+
}
|
|
94
|
+
store.hooksCleanup[index] = cleanFn;
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
run: context.mergeStateUpdates(() => {
|
|
98
|
+
const store = context.getStore();
|
|
99
|
+
store.hooksEffect.forEach((effect) => {
|
|
100
|
+
effect();
|
|
101
|
+
});
|
|
102
|
+
store.hooksEffect.length = 0;
|
|
103
|
+
}),
|
|
104
|
+
};
|
|
81
105
|
function useState(defaultValue) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return [
|
|
93
|
-
hooks[_idx],
|
|
94
|
-
(newValue) => {
|
|
95
|
-
// Noop if the value is still the same.
|
|
96
|
-
if (hooks[_idx] !== newValue) {
|
|
97
|
-
hooks[_idx] = newValue;
|
|
98
|
-
// Trigger re-render
|
|
99
|
-
handleChange();
|
|
106
|
+
return context.withPointer((pointer, store) => {
|
|
107
|
+
const { hooks } = store;
|
|
108
|
+
if (!(pointer in hooks)) {
|
|
109
|
+
if (typeof defaultValue === 'function') {
|
|
110
|
+
hooks[pointer] = defaultValue();
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
hooks[pointer] = defaultValue;
|
|
100
114
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
115
|
+
}
|
|
116
|
+
return [
|
|
117
|
+
hooks[pointer],
|
|
118
|
+
(newValue) => {
|
|
119
|
+
// Noop if the value is still the same.
|
|
120
|
+
if (hooks[pointer] !== newValue) {
|
|
121
|
+
hooks[pointer] = newValue;
|
|
122
|
+
// Trigger re-render
|
|
123
|
+
context.handleChange();
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
];
|
|
127
|
+
});
|
|
103
128
|
}
|
|
104
129
|
exports.useState = useState;
|
|
105
130
|
function useEffect(cb, depArray) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
116
|
-
}
|
|
117
|
-
if (hasChanged) {
|
|
118
|
-
hooksEffect.push(mergeStateUpdates(() => {
|
|
119
|
-
cleanupHook(_idx);
|
|
120
|
-
const cleanFn = cb(rl);
|
|
121
|
-
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
122
|
-
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
123
|
-
}
|
|
124
|
-
hooksCleanup[_idx] = cleanFn;
|
|
125
|
-
}));
|
|
126
|
-
}
|
|
127
|
-
hooks[_idx] = depArray;
|
|
131
|
+
return context.withPointer((pointer, store) => {
|
|
132
|
+
const { hooks } = store;
|
|
133
|
+
const oldDeps = hooks[pointer];
|
|
134
|
+
const hasChanged = !Array.isArray(oldDeps) || depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
135
|
+
if (hasChanged) {
|
|
136
|
+
effectScheduler.queue(cb);
|
|
137
|
+
}
|
|
138
|
+
hooks[pointer] = depArray;
|
|
139
|
+
});
|
|
128
140
|
}
|
|
129
141
|
exports.useEffect = useEffect;
|
|
142
|
+
function useRef(val) {
|
|
143
|
+
return useState({ current: val })[0];
|
|
144
|
+
}
|
|
145
|
+
exports.useRef = useRef;
|
|
130
146
|
function useKeypress(userHandler) {
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
userHandler(event, rl);
|
|
138
|
-
});
|
|
147
|
+
const signal = useRef(userHandler);
|
|
148
|
+
signal.current = userHandler;
|
|
149
|
+
useEffect((rl) => {
|
|
150
|
+
const handler = node_async_hooks_1.AsyncResource.bind(context.mergeStateUpdates((_input, event) => {
|
|
151
|
+
signal.current(event, rl);
|
|
152
|
+
}));
|
|
139
153
|
rl.input.on('keypress', handler);
|
|
140
154
|
return () => {
|
|
141
155
|
rl.input.removeListener('keypress', handler);
|
|
142
156
|
};
|
|
143
|
-
}, [
|
|
157
|
+
}, []);
|
|
144
158
|
}
|
|
145
159
|
exports.useKeypress = useKeypress;
|
|
146
|
-
function useRef(val) {
|
|
147
|
-
return useState({ current: val })[0];
|
|
148
|
-
}
|
|
149
|
-
exports.useRef = useRef;
|
|
150
160
|
function usePagination(output, { active, pageSize = 7, }) {
|
|
161
|
+
const { rl } = context.getStore();
|
|
151
162
|
const state = useRef({
|
|
152
163
|
pointer: 0,
|
|
153
164
|
lastIndex: 0,
|
|
154
165
|
});
|
|
155
|
-
const rl = sessionRl;
|
|
156
|
-
if (!rl) {
|
|
157
|
-
throw new Error('usePagination must be used within a prompt');
|
|
158
|
-
}
|
|
159
166
|
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: rl.output });
|
|
160
167
|
const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
|
|
161
168
|
// Make sure there's enough lines to paginate
|
|
@@ -179,97 +186,96 @@ exports.usePagination = usePagination;
|
|
|
179
186
|
function createPrompt(view) {
|
|
180
187
|
const prompt = (config, context) => {
|
|
181
188
|
var _a, _b;
|
|
182
|
-
if (sessionRl) {
|
|
183
|
-
throw new Error('An inquirer prompt is already running.\nMake sure you await the result of the previous prompt before calling another prompt.');
|
|
184
|
-
}
|
|
185
189
|
// Default `input` to stdin
|
|
186
190
|
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
187
191
|
// Add mute capabilities to the output
|
|
188
192
|
const output = new mute_stream_1.default();
|
|
189
193
|
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
190
|
-
|
|
194
|
+
const rl = readline.createInterface({
|
|
191
195
|
terminal: true,
|
|
192
196
|
input,
|
|
193
197
|
output,
|
|
194
198
|
});
|
|
195
|
-
const screen = new screen_manager_mjs_1.default(
|
|
199
|
+
const screen = new screen_manager_mjs_1.default(rl);
|
|
200
|
+
const store = {
|
|
201
|
+
rl,
|
|
202
|
+
hooks: [],
|
|
203
|
+
hooksCleanup: [],
|
|
204
|
+
hooksEffect: [],
|
|
205
|
+
index: 0,
|
|
206
|
+
handleChange() { },
|
|
207
|
+
};
|
|
196
208
|
let cancel = () => { };
|
|
197
209
|
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
210
|
+
hookStorage.run(store, () => {
|
|
211
|
+
const checkCursorPos = () => {
|
|
212
|
+
screen.checkCursorPos();
|
|
213
|
+
};
|
|
214
|
+
const onExit = node_async_hooks_1.AsyncResource.bind(() => {
|
|
215
|
+
try {
|
|
216
|
+
store.hooksCleanup.forEach((cleanFn) => {
|
|
217
|
+
cleanFn === null || cleanFn === void 0 ? void 0 : cleanFn();
|
|
218
|
+
});
|
|
206
219
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
};
|
|
222
|
-
cancel = () => {
|
|
223
|
-
onExit();
|
|
224
|
-
reject(new Error('Prompt was canceled'));
|
|
225
|
-
};
|
|
226
|
-
let shouldHandleExit = true;
|
|
227
|
-
const onForceExit = () => {
|
|
228
|
-
if (shouldHandleExit) {
|
|
229
|
-
shouldHandleExit = false;
|
|
230
|
-
onExit();
|
|
231
|
-
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
235
|
-
process.on('SIGINT', onForceExit);
|
|
236
|
-
const done = (value) => {
|
|
237
|
-
// Delay execution to let time to the hookCleanup functions to registers.
|
|
238
|
-
setImmediate(() => {
|
|
220
|
+
catch (err) {
|
|
221
|
+
reject(err);
|
|
222
|
+
}
|
|
223
|
+
if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
|
|
224
|
+
screen.clean();
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
screen.clearContent();
|
|
228
|
+
}
|
|
229
|
+
screen.done();
|
|
230
|
+
process.removeListener('SIGINT', onForceExit);
|
|
231
|
+
store.rl.input.removeListener('keypress', checkCursorPos);
|
|
232
|
+
});
|
|
233
|
+
cancel = node_async_hooks_1.AsyncResource.bind(() => {
|
|
239
234
|
onExit();
|
|
240
|
-
|
|
241
|
-
resolve(value);
|
|
235
|
+
reject(new Error('Prompt was canceled'));
|
|
242
236
|
});
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const nextView = view(resolvedConfig, done);
|
|
250
|
-
for (const effect of hooksEffect) {
|
|
251
|
-
effect();
|
|
237
|
+
let shouldHandleExit = true;
|
|
238
|
+
const onForceExit = node_async_hooks_1.AsyncResource.bind(() => {
|
|
239
|
+
if (shouldHandleExit) {
|
|
240
|
+
shouldHandleExit = false;
|
|
241
|
+
onExit();
|
|
242
|
+
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
252
243
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
244
|
+
});
|
|
245
|
+
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
246
|
+
process.on('SIGINT', onForceExit);
|
|
247
|
+
const done = (value) => {
|
|
248
|
+
// Delay execution to let time to the hookCleanup functions to registers.
|
|
249
|
+
setImmediate(() => {
|
|
250
|
+
onExit();
|
|
251
|
+
// Finally we resolve our promise
|
|
252
|
+
resolve(value);
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
const workLoop = (resolvedConfig) => {
|
|
256
|
+
store.index = 0;
|
|
257
|
+
store.handleChange = () => workLoop(resolvedConfig);
|
|
258
|
+
try {
|
|
259
|
+
const nextView = view(resolvedConfig, done);
|
|
260
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
261
|
+
screen.render(content, bottomContent);
|
|
262
|
+
effectScheduler.run();
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
onExit();
|
|
266
|
+
reject(err);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
// TODO: we should display a loader while we get the default options.
|
|
270
|
+
(0, options_mjs_1.getPromptConfig)(config).then((resolvedConfig) => {
|
|
271
|
+
workLoop(resolvedConfig);
|
|
272
|
+
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
273
|
+
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
274
|
+
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
275
|
+
// by a state change sets the cursor to the right position.
|
|
276
|
+
store.rl.input.on('keypress', checkCursorPos);
|
|
277
|
+
}, reject);
|
|
278
|
+
});
|
|
273
279
|
});
|
|
274
280
|
answer.cancel = cancel;
|
|
275
281
|
return answer;
|
|
@@ -18,10 +18,10 @@ export type KeypressEvent = {
|
|
|
18
18
|
type NotFunction<T> = T extends Function ? never : T;
|
|
19
19
|
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
20
20
|
export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
|
|
21
|
-
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
22
21
|
export declare function useRef<Value>(val: Value): {
|
|
23
22
|
current: Value;
|
|
24
23
|
};
|
|
24
|
+
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
25
25
|
export declare function usePagination(output: string, { active, pageSize, }: {
|
|
26
26
|
active: number;
|
|
27
27
|
pageSize?: number;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as readline from 'node:readline';
|
|
2
|
+
import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';
|
|
2
3
|
import { CancelablePromise } from '@inquirer/type';
|
|
3
4
|
import chalk from 'chalk';
|
|
4
5
|
import cliWidth from 'cli-width';
|
|
@@ -9,116 +10,121 @@ import { breakLines } from './lib/utils.mjs';
|
|
|
9
10
|
export { usePrefix } from './lib/prefix.mjs';
|
|
10
11
|
export * from './lib/key.mjs';
|
|
11
12
|
export * from './lib/Separator.mjs';
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function resetHookState() {
|
|
19
|
-
hooks.length = 0;
|
|
20
|
-
hooksCleanup.length = 0;
|
|
21
|
-
hooksEffect.length = 0;
|
|
22
|
-
index = 0;
|
|
23
|
-
handleChange = () => { };
|
|
24
|
-
sessionRl = undefined;
|
|
25
|
-
}
|
|
26
|
-
function cleanupHook(index) {
|
|
27
|
-
const cleanFn = hooksCleanup[index];
|
|
28
|
-
if (typeof cleanFn === 'function') {
|
|
29
|
-
cleanFn();
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function mergeStateUpdates(fn) {
|
|
33
|
-
const wrapped = (...args) => {
|
|
34
|
-
let shouldUpdate = false;
|
|
35
|
-
const oldHandleChange = handleChange;
|
|
36
|
-
handleChange = () => {
|
|
37
|
-
shouldUpdate = true;
|
|
38
|
-
};
|
|
39
|
-
const returnValue = fn(...args);
|
|
40
|
-
if (shouldUpdate) {
|
|
41
|
-
oldHandleChange();
|
|
42
|
-
}
|
|
43
|
-
handleChange = oldHandleChange;
|
|
44
|
-
return returnValue;
|
|
45
|
-
};
|
|
46
|
-
return wrapped;
|
|
47
|
-
}
|
|
48
|
-
export function useState(defaultValue) {
|
|
49
|
-
const _idx = index;
|
|
50
|
-
index++;
|
|
51
|
-
if (!(_idx in hooks)) {
|
|
52
|
-
if (typeof defaultValue === 'function') {
|
|
53
|
-
hooks[_idx] = defaultValue();
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
hooks[_idx] = defaultValue;
|
|
13
|
+
const hookStorage = new AsyncLocalStorage();
|
|
14
|
+
const context = {
|
|
15
|
+
getStore() {
|
|
16
|
+
const store = hookStorage.getStore();
|
|
17
|
+
if (!store) {
|
|
18
|
+
throw new Error('[Inquirer] Hook functions can only be called from within a prompt');
|
|
57
19
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
20
|
+
return store;
|
|
21
|
+
},
|
|
22
|
+
withPointer(cb) {
|
|
23
|
+
const store = context.getStore();
|
|
24
|
+
const value = cb(store.index, store);
|
|
25
|
+
store.index++;
|
|
26
|
+
return value;
|
|
27
|
+
},
|
|
28
|
+
handleChange() {
|
|
29
|
+
const { handleChange } = context.getStore();
|
|
30
|
+
handleChange();
|
|
31
|
+
},
|
|
32
|
+
mergeStateUpdates(fn) {
|
|
33
|
+
const wrapped = (...args) => {
|
|
34
|
+
const store = context.getStore();
|
|
35
|
+
let shouldUpdate = false;
|
|
36
|
+
const oldHandleChange = store.handleChange;
|
|
37
|
+
store.handleChange = () => {
|
|
38
|
+
shouldUpdate = true;
|
|
39
|
+
};
|
|
40
|
+
const returnValue = fn(...args);
|
|
41
|
+
if (shouldUpdate) {
|
|
42
|
+
oldHandleChange();
|
|
67
43
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
82
|
-
}
|
|
83
|
-
if (hasChanged) {
|
|
84
|
-
hooksEffect.push(mergeStateUpdates(() => {
|
|
85
|
-
cleanupHook(_idx);
|
|
86
|
-
const cleanFn = cb(rl);
|
|
44
|
+
store.handleChange = oldHandleChange;
|
|
45
|
+
return returnValue;
|
|
46
|
+
};
|
|
47
|
+
return wrapped;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
const effectScheduler = {
|
|
51
|
+
queue(cb) {
|
|
52
|
+
const store = context.getStore();
|
|
53
|
+
const { index } = store;
|
|
54
|
+
store.hooksEffect.push(() => {
|
|
55
|
+
store.hooksCleanup[index]?.();
|
|
56
|
+
const cleanFn = cb(store.rl);
|
|
87
57
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
88
58
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
89
59
|
}
|
|
90
|
-
hooksCleanup[
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
|
|
60
|
+
store.hooksCleanup[index] = cleanFn;
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
run: context.mergeStateUpdates(() => {
|
|
64
|
+
const store = context.getStore();
|
|
65
|
+
store.hooksEffect.forEach((effect) => {
|
|
66
|
+
effect();
|
|
67
|
+
});
|
|
68
|
+
store.hooksEffect.length = 0;
|
|
69
|
+
}),
|
|
70
|
+
};
|
|
71
|
+
export function useState(defaultValue) {
|
|
72
|
+
return context.withPointer((pointer, store) => {
|
|
73
|
+
const { hooks } = store;
|
|
74
|
+
if (!(pointer in hooks)) {
|
|
75
|
+
if (typeof defaultValue === 'function') {
|
|
76
|
+
hooks[pointer] = defaultValue();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
hooks[pointer] = defaultValue;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return [
|
|
83
|
+
hooks[pointer],
|
|
84
|
+
(newValue) => {
|
|
85
|
+
// Noop if the value is still the same.
|
|
86
|
+
if (hooks[pointer] !== newValue) {
|
|
87
|
+
hooks[pointer] = newValue;
|
|
88
|
+
// Trigger re-render
|
|
89
|
+
context.handleChange();
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
export function useEffect(cb, depArray) {
|
|
96
|
+
return context.withPointer((pointer, store) => {
|
|
97
|
+
const { hooks } = store;
|
|
98
|
+
const oldDeps = hooks[pointer];
|
|
99
|
+
const hasChanged = !Array.isArray(oldDeps) || depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
100
|
+
if (hasChanged) {
|
|
101
|
+
effectScheduler.queue(cb);
|
|
102
|
+
}
|
|
103
|
+
hooks[pointer] = depArray;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function useRef(val) {
|
|
107
|
+
return useState({ current: val })[0];
|
|
94
108
|
}
|
|
95
109
|
export function useKeypress(userHandler) {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
userHandler(event, rl);
|
|
103
|
-
});
|
|
110
|
+
const signal = useRef(userHandler);
|
|
111
|
+
signal.current = userHandler;
|
|
112
|
+
useEffect((rl) => {
|
|
113
|
+
const handler = AsyncResource.bind(context.mergeStateUpdates((_input, event) => {
|
|
114
|
+
signal.current(event, rl);
|
|
115
|
+
}));
|
|
104
116
|
rl.input.on('keypress', handler);
|
|
105
117
|
return () => {
|
|
106
118
|
rl.input.removeListener('keypress', handler);
|
|
107
119
|
};
|
|
108
|
-
}, [
|
|
109
|
-
}
|
|
110
|
-
export function useRef(val) {
|
|
111
|
-
return useState({ current: val })[0];
|
|
120
|
+
}, []);
|
|
112
121
|
}
|
|
113
122
|
export function usePagination(output, { active, pageSize = 7, }) {
|
|
123
|
+
const { rl } = context.getStore();
|
|
114
124
|
const state = useRef({
|
|
115
125
|
pointer: 0,
|
|
116
126
|
lastIndex: 0,
|
|
117
127
|
});
|
|
118
|
-
const rl = sessionRl;
|
|
119
|
-
if (!rl) {
|
|
120
|
-
throw new Error('usePagination must be used within a prompt');
|
|
121
|
-
}
|
|
122
128
|
const width = cliWidth({ defaultWidth: 80, output: rl.output });
|
|
123
129
|
const lines = breakLines(output, width).split('\n');
|
|
124
130
|
// Make sure there's enough lines to paginate
|
|
@@ -140,97 +146,96 @@ export function usePagination(output, { active, pageSize = 7, }) {
|
|
|
140
146
|
}
|
|
141
147
|
export function createPrompt(view) {
|
|
142
148
|
const prompt = (config, context) => {
|
|
143
|
-
if (sessionRl) {
|
|
144
|
-
throw new Error('An inquirer prompt is already running.\nMake sure you await the result of the previous prompt before calling another prompt.');
|
|
145
|
-
}
|
|
146
149
|
// Default `input` to stdin
|
|
147
150
|
const input = context?.input ?? process.stdin;
|
|
148
151
|
// Add mute capabilities to the output
|
|
149
152
|
const output = new MuteStream();
|
|
150
153
|
output.pipe(context?.output ?? process.stdout);
|
|
151
|
-
|
|
154
|
+
const rl = readline.createInterface({
|
|
152
155
|
terminal: true,
|
|
153
156
|
input,
|
|
154
157
|
output,
|
|
155
158
|
});
|
|
156
|
-
const screen = new ScreenManager(
|
|
159
|
+
const screen = new ScreenManager(rl);
|
|
160
|
+
const store = {
|
|
161
|
+
rl,
|
|
162
|
+
hooks: [],
|
|
163
|
+
hooksCleanup: [],
|
|
164
|
+
hooksEffect: [],
|
|
165
|
+
index: 0,
|
|
166
|
+
handleChange() { },
|
|
167
|
+
};
|
|
157
168
|
let cancel = () => { };
|
|
158
169
|
const answer = new CancelablePromise((resolve, reject) => {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
170
|
+
hookStorage.run(store, () => {
|
|
171
|
+
const checkCursorPos = () => {
|
|
172
|
+
screen.checkCursorPos();
|
|
173
|
+
};
|
|
174
|
+
const onExit = AsyncResource.bind(() => {
|
|
175
|
+
try {
|
|
176
|
+
store.hooksCleanup.forEach((cleanFn) => {
|
|
177
|
+
cleanFn?.();
|
|
178
|
+
});
|
|
167
179
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
};
|
|
183
|
-
cancel = () => {
|
|
184
|
-
onExit();
|
|
185
|
-
reject(new Error('Prompt was canceled'));
|
|
186
|
-
};
|
|
187
|
-
let shouldHandleExit = true;
|
|
188
|
-
const onForceExit = () => {
|
|
189
|
-
if (shouldHandleExit) {
|
|
190
|
-
shouldHandleExit = false;
|
|
191
|
-
onExit();
|
|
192
|
-
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
196
|
-
process.on('SIGINT', onForceExit);
|
|
197
|
-
const done = (value) => {
|
|
198
|
-
// Delay execution to let time to the hookCleanup functions to registers.
|
|
199
|
-
setImmediate(() => {
|
|
180
|
+
catch (err) {
|
|
181
|
+
reject(err);
|
|
182
|
+
}
|
|
183
|
+
if (context?.clearPromptOnDone) {
|
|
184
|
+
screen.clean();
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
screen.clearContent();
|
|
188
|
+
}
|
|
189
|
+
screen.done();
|
|
190
|
+
process.removeListener('SIGINT', onForceExit);
|
|
191
|
+
store.rl.input.removeListener('keypress', checkCursorPos);
|
|
192
|
+
});
|
|
193
|
+
cancel = AsyncResource.bind(() => {
|
|
200
194
|
onExit();
|
|
201
|
-
|
|
202
|
-
resolve(value);
|
|
195
|
+
reject(new Error('Prompt was canceled'));
|
|
203
196
|
});
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const nextView = view(resolvedConfig, done);
|
|
211
|
-
for (const effect of hooksEffect) {
|
|
212
|
-
effect();
|
|
197
|
+
let shouldHandleExit = true;
|
|
198
|
+
const onForceExit = AsyncResource.bind(() => {
|
|
199
|
+
if (shouldHandleExit) {
|
|
200
|
+
shouldHandleExit = false;
|
|
201
|
+
onExit();
|
|
202
|
+
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
213
203
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
204
|
+
});
|
|
205
|
+
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
206
|
+
process.on('SIGINT', onForceExit);
|
|
207
|
+
const done = (value) => {
|
|
208
|
+
// Delay execution to let time to the hookCleanup functions to registers.
|
|
209
|
+
setImmediate(() => {
|
|
210
|
+
onExit();
|
|
211
|
+
// Finally we resolve our promise
|
|
212
|
+
resolve(value);
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
const workLoop = (resolvedConfig) => {
|
|
216
|
+
store.index = 0;
|
|
217
|
+
store.handleChange = () => workLoop(resolvedConfig);
|
|
218
|
+
try {
|
|
219
|
+
const nextView = view(resolvedConfig, done);
|
|
220
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
221
|
+
screen.render(content, bottomContent);
|
|
222
|
+
effectScheduler.run();
|
|
223
|
+
}
|
|
224
|
+
catch (err) {
|
|
225
|
+
onExit();
|
|
226
|
+
reject(err);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
// TODO: we should display a loader while we get the default options.
|
|
230
|
+
getPromptConfig(config).then((resolvedConfig) => {
|
|
231
|
+
workLoop(resolvedConfig);
|
|
232
|
+
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
233
|
+
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
234
|
+
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
235
|
+
// by a state change sets the cursor to the right position.
|
|
236
|
+
store.rl.input.on('keypress', checkCursorPos);
|
|
237
|
+
}, reject);
|
|
238
|
+
});
|
|
234
239
|
});
|
|
235
240
|
answer.cancel = cancel;
|
|
236
241
|
return answer;
|
|
@@ -18,10 +18,10 @@ export type KeypressEvent = {
|
|
|
18
18
|
type NotFunction<T> = T extends Function ? never : T;
|
|
19
19
|
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
20
20
|
export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
|
|
21
|
-
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
22
21
|
export declare function useRef<Value>(val: Value): {
|
|
23
22
|
current: Value;
|
|
24
23
|
};
|
|
24
|
+
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
25
25
|
export declare function usePagination(output: string, { active, pageSize, }: {
|
|
26
26
|
active: number;
|
|
27
27
|
pageSize?: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=14.18.0"
|
|
6
6
|
},
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
"license": "MIT",
|
|
58
58
|
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/core/README.md",
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@inquirer/type": "^1.1.
|
|
60
|
+
"@inquirer/type": "^1.1.2",
|
|
61
61
|
"@types/mute-stream": "^0.0.1",
|
|
62
|
-
"@types/node": "^20.
|
|
62
|
+
"@types/node": "^20.5.6",
|
|
63
63
|
"@types/wrap-ansi": "^3.0.0",
|
|
64
64
|
"ansi-escapes": "^4.3.2",
|
|
65
65
|
"chalk": "^4.1.2",
|
|
@@ -72,14 +72,13 @@
|
|
|
72
72
|
"wrap-ansi": "^6.2.0"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@inquirer/testing": "^2.1.
|
|
75
|
+
"@inquirer/testing": "^2.1.4"
|
|
76
76
|
},
|
|
77
77
|
"scripts": {
|
|
78
78
|
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
|
|
79
79
|
"clean": "rm -rf dist",
|
|
80
80
|
"tsc:esm": "tsc -p ./tsconfig.json",
|
|
81
|
-
"tsc:cjs": "tsc -p ./tsconfig.cjs.json &&
|
|
82
|
-
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
|
|
81
|
+
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
|
|
83
82
|
},
|
|
84
83
|
"publishConfig": {
|
|
85
84
|
"access": "public"
|
|
@@ -96,5 +95,5 @@
|
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
},
|
|
99
|
-
"gitHead": "
|
|
98
|
+
"gitHead": "ea5baff21fb29ed3c0931ea4030cc1dd1e92fc8d"
|
|
100
99
|
}
|