@inquirer/core 3.1.2 → 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 +178 -175
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/esm/index.mjs +174 -172
- package/dist/esm/types/index.d.mts +1 -1
- package/package.json +4 -4
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,125 +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 mergeStateUpdates(fn) {
|
|
60
|
-
const wrapped = (...args) => {
|
|
61
|
-
let shouldUpdate = false;
|
|
62
|
-
const oldHandleChange = handleChange;
|
|
63
|
-
handleChange = () => {
|
|
64
|
-
shouldUpdate = true;
|
|
65
|
-
};
|
|
66
|
-
const returnValue = fn(...args);
|
|
67
|
-
if (shouldUpdate) {
|
|
68
|
-
oldHandleChange();
|
|
69
|
-
}
|
|
70
|
-
handleChange = oldHandleChange;
|
|
71
|
-
return returnValue;
|
|
72
|
-
};
|
|
73
|
-
return wrapped;
|
|
74
|
-
}
|
|
75
|
-
function cleanupHook(index) {
|
|
76
|
-
const cleanFn = hooksCleanup[index];
|
|
77
|
-
if (typeof cleanFn === 'function') {
|
|
78
|
-
cleanFn();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const runEffects = mergeStateUpdates(() => {
|
|
82
|
-
for (const effect of hooksEffect) {
|
|
83
|
-
effect();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
function useState(defaultValue) {
|
|
87
|
-
const _idx = index;
|
|
88
|
-
index++;
|
|
89
|
-
if (!(_idx in hooks)) {
|
|
90
|
-
if (typeof defaultValue === 'function') {
|
|
91
|
-
hooks[_idx] = defaultValue();
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
hooks[_idx] = defaultValue;
|
|
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');
|
|
95
52
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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();
|
|
105
76
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
121
|
-
}
|
|
122
|
-
if (hasChanged) {
|
|
123
|
-
hooksEffect.push(() => {
|
|
124
|
-
cleanupHook(_idx);
|
|
125
|
-
const cleanFn = cb(rl);
|
|
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);
|
|
126
91
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
127
92
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
128
93
|
}
|
|
129
|
-
hooksCleanup[
|
|
94
|
+
store.hooksCleanup[index] = cleanFn;
|
|
130
95
|
});
|
|
131
|
-
}
|
|
132
|
-
|
|
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
|
+
};
|
|
105
|
+
function useState(defaultValue) {
|
|
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;
|
|
114
|
+
}
|
|
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
|
+
});
|
|
128
|
+
}
|
|
129
|
+
exports.useState = useState;
|
|
130
|
+
function useEffect(cb, 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
|
+
});
|
|
133
140
|
}
|
|
134
141
|
exports.useEffect = useEffect;
|
|
142
|
+
function useRef(val) {
|
|
143
|
+
return useState({ current: val })[0];
|
|
144
|
+
}
|
|
145
|
+
exports.useRef = useRef;
|
|
135
146
|
function useKeypress(userHandler) {
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
userHandler(event, rl);
|
|
143
|
-
});
|
|
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
|
+
}));
|
|
144
153
|
rl.input.on('keypress', handler);
|
|
145
154
|
return () => {
|
|
146
155
|
rl.input.removeListener('keypress', handler);
|
|
147
156
|
};
|
|
148
|
-
}, [
|
|
157
|
+
}, []);
|
|
149
158
|
}
|
|
150
159
|
exports.useKeypress = useKeypress;
|
|
151
|
-
function useRef(val) {
|
|
152
|
-
return useState({ current: val })[0];
|
|
153
|
-
}
|
|
154
|
-
exports.useRef = useRef;
|
|
155
160
|
function usePagination(output, { active, pageSize = 7, }) {
|
|
161
|
+
const { rl } = context.getStore();
|
|
156
162
|
const state = useRef({
|
|
157
163
|
pointer: 0,
|
|
158
164
|
lastIndex: 0,
|
|
159
165
|
});
|
|
160
|
-
const rl = sessionRl;
|
|
161
|
-
if (!rl) {
|
|
162
|
-
throw new Error('usePagination must be used within a prompt');
|
|
163
|
-
}
|
|
164
166
|
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: rl.output });
|
|
165
167
|
const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
|
|
166
168
|
// Make sure there's enough lines to paginate
|
|
@@ -184,95 +186,96 @@ exports.usePagination = usePagination;
|
|
|
184
186
|
function createPrompt(view) {
|
|
185
187
|
const prompt = (config, context) => {
|
|
186
188
|
var _a, _b;
|
|
187
|
-
if (sessionRl) {
|
|
188
|
-
throw new Error('An inquirer prompt is already running.\nMake sure you await the result of the previous prompt before calling another prompt.');
|
|
189
|
-
}
|
|
190
189
|
// Default `input` to stdin
|
|
191
190
|
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
192
191
|
// Add mute capabilities to the output
|
|
193
192
|
const output = new mute_stream_1.default();
|
|
194
193
|
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
195
|
-
|
|
194
|
+
const rl = readline.createInterface({
|
|
196
195
|
terminal: true,
|
|
197
196
|
input,
|
|
198
197
|
output,
|
|
199
198
|
});
|
|
200
|
-
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
|
+
};
|
|
201
208
|
let cancel = () => { };
|
|
202
209
|
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
+
});
|
|
211
219
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.removeListener('keypress', checkCursorPos);
|
|
225
|
-
resetHookState();
|
|
226
|
-
};
|
|
227
|
-
cancel = () => {
|
|
228
|
-
onExit();
|
|
229
|
-
reject(new Error('Prompt was canceled'));
|
|
230
|
-
};
|
|
231
|
-
let shouldHandleExit = true;
|
|
232
|
-
const onForceExit = () => {
|
|
233
|
-
if (shouldHandleExit) {
|
|
234
|
-
shouldHandleExit = false;
|
|
235
|
-
onExit();
|
|
236
|
-
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
240
|
-
process.on('SIGINT', onForceExit);
|
|
241
|
-
const done = (value) => {
|
|
242
|
-
// Delay execution to let time to the hookCleanup functions to registers.
|
|
243
|
-
setImmediate(() => {
|
|
244
|
-
onExit();
|
|
245
|
-
// Finally we resolve our promise
|
|
246
|
-
resolve(value);
|
|
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);
|
|
247
232
|
});
|
|
248
|
-
|
|
249
|
-
const workLoop = (resolvedConfig) => {
|
|
250
|
-
index = 0;
|
|
251
|
-
hooksEffect.length = 0;
|
|
252
|
-
handleChange = () => workLoop(resolvedConfig);
|
|
253
|
-
try {
|
|
254
|
-
const nextView = view(resolvedConfig, done);
|
|
255
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
256
|
-
screen.render(content, bottomContent);
|
|
257
|
-
runEffects();
|
|
258
|
-
}
|
|
259
|
-
catch (err) {
|
|
233
|
+
cancel = node_async_hooks_1.AsyncResource.bind(() => {
|
|
260
234
|
onExit();
|
|
261
|
-
reject(
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
235
|
+
reject(new Error('Prompt was canceled'));
|
|
236
|
+
});
|
|
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'));
|
|
243
|
+
}
|
|
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
|
+
});
|
|
276
279
|
});
|
|
277
280
|
answer.cancel = cancel;
|
|
278
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,121 +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 mergeStateUpdates(fn) {
|
|
27
|
-
const wrapped = (...args) => {
|
|
28
|
-
let shouldUpdate = false;
|
|
29
|
-
const oldHandleChange = handleChange;
|
|
30
|
-
handleChange = () => {
|
|
31
|
-
shouldUpdate = true;
|
|
32
|
-
};
|
|
33
|
-
const returnValue = fn(...args);
|
|
34
|
-
if (shouldUpdate) {
|
|
35
|
-
oldHandleChange();
|
|
36
|
-
}
|
|
37
|
-
handleChange = oldHandleChange;
|
|
38
|
-
return returnValue;
|
|
39
|
-
};
|
|
40
|
-
return wrapped;
|
|
41
|
-
}
|
|
42
|
-
function cleanupHook(index) {
|
|
43
|
-
const cleanFn = hooksCleanup[index];
|
|
44
|
-
if (typeof cleanFn === 'function') {
|
|
45
|
-
cleanFn();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
const runEffects = mergeStateUpdates(() => {
|
|
49
|
-
for (const effect of hooksEffect) {
|
|
50
|
-
effect();
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
export function useState(defaultValue) {
|
|
54
|
-
const _idx = index;
|
|
55
|
-
index++;
|
|
56
|
-
if (!(_idx in hooks)) {
|
|
57
|
-
if (typeof defaultValue === 'function') {
|
|
58
|
-
hooks[_idx] = defaultValue();
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
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');
|
|
62
19
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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();
|
|
72
43
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
87
|
-
}
|
|
88
|
-
if (hasChanged) {
|
|
89
|
-
hooksEffect.push(() => {
|
|
90
|
-
cleanupHook(_idx);
|
|
91
|
-
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);
|
|
92
57
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
93
58
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
94
59
|
}
|
|
95
|
-
hooksCleanup[
|
|
60
|
+
store.hooksCleanup[index] = cleanFn;
|
|
96
61
|
});
|
|
97
|
-
}
|
|
98
|
-
|
|
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];
|
|
99
108
|
}
|
|
100
109
|
export function useKeypress(userHandler) {
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
userHandler(event, rl);
|
|
108
|
-
});
|
|
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
|
+
}));
|
|
109
116
|
rl.input.on('keypress', handler);
|
|
110
117
|
return () => {
|
|
111
118
|
rl.input.removeListener('keypress', handler);
|
|
112
119
|
};
|
|
113
|
-
}, [
|
|
114
|
-
}
|
|
115
|
-
export function useRef(val) {
|
|
116
|
-
return useState({ current: val })[0];
|
|
120
|
+
}, []);
|
|
117
121
|
}
|
|
118
122
|
export function usePagination(output, { active, pageSize = 7, }) {
|
|
123
|
+
const { rl } = context.getStore();
|
|
119
124
|
const state = useRef({
|
|
120
125
|
pointer: 0,
|
|
121
126
|
lastIndex: 0,
|
|
122
127
|
});
|
|
123
|
-
const rl = sessionRl;
|
|
124
|
-
if (!rl) {
|
|
125
|
-
throw new Error('usePagination must be used within a prompt');
|
|
126
|
-
}
|
|
127
128
|
const width = cliWidth({ defaultWidth: 80, output: rl.output });
|
|
128
129
|
const lines = breakLines(output, width).split('\n');
|
|
129
130
|
// Make sure there's enough lines to paginate
|
|
@@ -145,95 +146,96 @@ export function usePagination(output, { active, pageSize = 7, }) {
|
|
|
145
146
|
}
|
|
146
147
|
export function createPrompt(view) {
|
|
147
148
|
const prompt = (config, context) => {
|
|
148
|
-
if (sessionRl) {
|
|
149
|
-
throw new Error('An inquirer prompt is already running.\nMake sure you await the result of the previous prompt before calling another prompt.');
|
|
150
|
-
}
|
|
151
149
|
// Default `input` to stdin
|
|
152
150
|
const input = context?.input ?? process.stdin;
|
|
153
151
|
// Add mute capabilities to the output
|
|
154
152
|
const output = new MuteStream();
|
|
155
153
|
output.pipe(context?.output ?? process.stdout);
|
|
156
|
-
|
|
154
|
+
const rl = readline.createInterface({
|
|
157
155
|
terminal: true,
|
|
158
156
|
input,
|
|
159
157
|
output,
|
|
160
158
|
});
|
|
161
|
-
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
|
+
};
|
|
162
168
|
let cancel = () => { };
|
|
163
169
|
const answer = new CancelablePromise((resolve, reject) => {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
+
});
|
|
172
179
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
sessionRl?.input.removeListener('keypress', checkCursorPos);
|
|
186
|
-
resetHookState();
|
|
187
|
-
};
|
|
188
|
-
cancel = () => {
|
|
189
|
-
onExit();
|
|
190
|
-
reject(new Error('Prompt was canceled'));
|
|
191
|
-
};
|
|
192
|
-
let shouldHandleExit = true;
|
|
193
|
-
const onForceExit = () => {
|
|
194
|
-
if (shouldHandleExit) {
|
|
195
|
-
shouldHandleExit = false;
|
|
196
|
-
onExit();
|
|
197
|
-
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
201
|
-
process.on('SIGINT', onForceExit);
|
|
202
|
-
const done = (value) => {
|
|
203
|
-
// Delay execution to let time to the hookCleanup functions to registers.
|
|
204
|
-
setImmediate(() => {
|
|
205
|
-
onExit();
|
|
206
|
-
// Finally we resolve our promise
|
|
207
|
-
resolve(value);
|
|
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);
|
|
208
192
|
});
|
|
209
|
-
|
|
210
|
-
const workLoop = (resolvedConfig) => {
|
|
211
|
-
index = 0;
|
|
212
|
-
hooksEffect.length = 0;
|
|
213
|
-
handleChange = () => workLoop(resolvedConfig);
|
|
214
|
-
try {
|
|
215
|
-
const nextView = view(resolvedConfig, done);
|
|
216
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
217
|
-
screen.render(content, bottomContent);
|
|
218
|
-
runEffects();
|
|
219
|
-
}
|
|
220
|
-
catch (err) {
|
|
193
|
+
cancel = AsyncResource.bind(() => {
|
|
221
194
|
onExit();
|
|
222
|
-
reject(
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
195
|
+
reject(new Error('Prompt was canceled'));
|
|
196
|
+
});
|
|
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'));
|
|
203
|
+
}
|
|
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
|
+
});
|
|
237
239
|
});
|
|
238
240
|
answer.cancel = cancel;
|
|
239
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
|
},
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"dependencies": {
|
|
60
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,7 +72,7 @@
|
|
|
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",
|
|
@@ -95,5 +95,5 @@
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
},
|
|
98
|
-
"gitHead": "
|
|
98
|
+
"gitHead": "ea5baff21fb29ed3c0931ea4030cc1dd1e92fc8d"
|
|
99
99
|
}
|