@inquirer/core 4.0.0 → 4.1.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 +17 -268
- package/dist/cjs/lib/Separator.js +2 -5
- package/dist/cjs/lib/create-prompt.js +111 -0
- package/dist/cjs/lib/hook-engine.js +100 -0
- package/dist/cjs/lib/read-line.type.js +2 -0
- package/dist/cjs/lib/screen-manager.js +3 -6
- package/dist/cjs/lib/use-effect.js +15 -0
- package/dist/cjs/lib/use-keypress.js +20 -0
- package/dist/cjs/lib/use-pagination.js +34 -0
- package/dist/cjs/lib/{prefix.js → use-prefix.js} +7 -9
- package/dist/cjs/lib/use-ref.js +8 -0
- package/dist/cjs/lib/use-state.js +28 -0
- package/dist/cjs/lib/utils.js +2 -5
- package/dist/cjs/types/index.d.ts +9 -36
- package/dist/cjs/types/lib/create-prompt.d.ts +11 -0
- package/dist/cjs/types/lib/hook-engine.d.ts +24 -0
- package/dist/cjs/types/lib/key.d.ts +1 -2
- package/dist/cjs/types/lib/read-line.type.d.ts +9 -0
- package/dist/cjs/types/lib/screen-manager.d.ts +1 -1
- package/dist/cjs/types/lib/use-effect.d.ts +2 -0
- package/dist/cjs/types/lib/use-keypress.d.ts +3 -0
- package/dist/cjs/types/lib/use-pagination.d.ts +4 -0
- package/dist/cjs/types/lib/use-ref.d.ts +3 -0
- package/dist/cjs/types/lib/use-state.d.ts +3 -0
- package/dist/esm/index.mjs +8 -243
- package/dist/esm/lib/create-prompt.mjs +99 -0
- package/dist/esm/lib/hook-engine.mjs +91 -0
- package/dist/esm/lib/read-line.type.mjs +1 -0
- package/dist/esm/lib/use-effect.mjs +11 -0
- package/dist/esm/lib/use-keypress.mjs +16 -0
- package/dist/esm/lib/use-pagination.mjs +30 -0
- package/dist/esm/lib/{prefix.mjs → use-prefix.mjs} +3 -2
- package/dist/esm/lib/use-ref.mjs +4 -0
- package/dist/esm/lib/use-state.mjs +24 -0
- package/dist/esm/lib/utils.mjs +1 -1
- package/dist/esm/types/index.d.mts +9 -36
- package/dist/esm/types/lib/create-prompt.d.mts +11 -0
- package/dist/esm/types/lib/hook-engine.d.mts +24 -0
- package/dist/esm/types/lib/key.d.mts +1 -2
- package/dist/esm/types/lib/read-line.type.d.mts +9 -0
- package/dist/esm/types/lib/screen-manager.d.mts +1 -1
- package/dist/esm/types/lib/use-effect.d.mts +2 -0
- package/dist/esm/types/lib/use-keypress.d.mts +3 -0
- package/dist/esm/types/lib/use-pagination.d.mts +4 -0
- package/dist/esm/types/lib/use-ref.d.mts +3 -0
- package/dist/esm/types/lib/use-state.d.mts +3 -0
- package/package.json +9 -9
- package/dist/cjs/lib/options.js +0 -19
- package/dist/cjs/types/lib/options.d.ts +0 -2
- package/dist/esm/lib/options.mjs +0 -8
- package/dist/esm/types/lib/options.d.mts +0 -2
- /package/dist/cjs/types/lib/{prefix.d.ts → use-prefix.d.ts} +0 -0
- /package/dist/esm/types/lib/{prefix.d.mts → use-prefix.d.mts} +0 -0
package/dist/cjs/index.js
CHANGED
|
@@ -10,276 +10,25 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
15
|
};
|
|
28
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
-
};
|
|
31
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.createPrompt = exports.usePagination = exports.useKeypress = exports.useRef = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
33
|
-
const readline = __importStar(require("node:readline"));
|
|
34
|
-
const node_async_hooks_1 = require("node:async_hooks");
|
|
35
|
-
const type_1 = require("@inquirer/type");
|
|
36
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
37
|
-
const cli_width_1 = __importDefault(require("cli-width"));
|
|
38
|
-
const mute_stream_1 = __importDefault(require("mute-stream"));
|
|
39
|
-
const screen_manager_mjs_1 = __importDefault(require('./lib/screen-manager.js'));
|
|
40
|
-
const options_mjs_1 = require('./lib/options.js');
|
|
41
|
-
const utils_mjs_1 = require('./lib/utils.js');
|
|
42
|
-
var prefix_mjs_1 = require('./lib/prefix.js');
|
|
43
|
-
Object.defineProperty(exports, "usePrefix", { enumerable: true, get: function () { return prefix_mjs_1.usePrefix; } });
|
|
17
|
+
exports.Separator = exports.createPrompt = exports.usePagination = exports.useKeypress = exports.useRef = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
44
18
|
__exportStar(require('./lib/key.js'), exports);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
};
|
|
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
|
-
});
|
|
140
|
-
}
|
|
141
|
-
exports.useEffect = useEffect;
|
|
142
|
-
function useRef(val) {
|
|
143
|
-
return useState({ current: val })[0];
|
|
144
|
-
}
|
|
145
|
-
exports.useRef = useRef;
|
|
146
|
-
function useKeypress(userHandler) {
|
|
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
|
-
}));
|
|
153
|
-
rl.input.on('keypress', handler);
|
|
154
|
-
return () => {
|
|
155
|
-
rl.input.removeListener('keypress', handler);
|
|
156
|
-
};
|
|
157
|
-
}, []);
|
|
158
|
-
}
|
|
159
|
-
exports.useKeypress = useKeypress;
|
|
160
|
-
function usePagination(output, { active, pageSize = 7, }) {
|
|
161
|
-
const { rl } = context.getStore();
|
|
162
|
-
const state = useRef({
|
|
163
|
-
pointer: 0,
|
|
164
|
-
lastIndex: 0,
|
|
165
|
-
});
|
|
166
|
-
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: rl.output });
|
|
167
|
-
const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
|
|
168
|
-
// Make sure there's enough lines to paginate
|
|
169
|
-
if (lines.length <= pageSize) {
|
|
170
|
-
return output;
|
|
171
|
-
}
|
|
172
|
-
const middleOfList = Math.floor(pageSize / 2);
|
|
173
|
-
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
174
|
-
const { pointer: prevPointer, lastIndex } = state.current;
|
|
175
|
-
if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
|
|
176
|
-
state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
|
|
177
|
-
}
|
|
178
|
-
state.current.lastIndex = active;
|
|
179
|
-
// Duplicate the lines so it give an infinite list look
|
|
180
|
-
const infinite = [lines, lines, lines].flat();
|
|
181
|
-
const topIndex = Math.max(0, active + lines.length - state.current.pointer);
|
|
182
|
-
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
183
|
-
return section + '\n' + chalk_1.default.dim('(Move up and down to reveal more choices)');
|
|
184
|
-
}
|
|
185
|
-
exports.usePagination = usePagination;
|
|
186
|
-
function createPrompt(view) {
|
|
187
|
-
const prompt = (config, context) => {
|
|
188
|
-
var _a, _b;
|
|
189
|
-
// Default `input` to stdin
|
|
190
|
-
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
191
|
-
// Add mute capabilities to the output
|
|
192
|
-
const output = new mute_stream_1.default();
|
|
193
|
-
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
194
|
-
const rl = readline.createInterface({
|
|
195
|
-
terminal: true,
|
|
196
|
-
input,
|
|
197
|
-
output,
|
|
198
|
-
});
|
|
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
|
-
};
|
|
208
|
-
let cancel = () => { };
|
|
209
|
-
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
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
|
-
});
|
|
219
|
-
}
|
|
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(() => {
|
|
234
|
-
onExit();
|
|
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
|
-
});
|
|
279
|
-
});
|
|
280
|
-
answer.cancel = cancel;
|
|
281
|
-
return answer;
|
|
282
|
-
};
|
|
283
|
-
return prompt;
|
|
284
|
-
}
|
|
285
|
-
exports.createPrompt = createPrompt;
|
|
19
|
+
var use_prefix_mjs_1 = require('./lib/use-prefix.js');
|
|
20
|
+
Object.defineProperty(exports, "usePrefix", { enumerable: true, get: function () { return use_prefix_mjs_1.usePrefix; } });
|
|
21
|
+
var use_state_mjs_1 = require('./lib/use-state.js');
|
|
22
|
+
Object.defineProperty(exports, "useState", { enumerable: true, get: function () { return use_state_mjs_1.useState; } });
|
|
23
|
+
var use_effect_mjs_1 = require('./lib/use-effect.js');
|
|
24
|
+
Object.defineProperty(exports, "useEffect", { enumerable: true, get: function () { return use_effect_mjs_1.useEffect; } });
|
|
25
|
+
var use_ref_mjs_1 = require('./lib/use-ref.js');
|
|
26
|
+
Object.defineProperty(exports, "useRef", { enumerable: true, get: function () { return use_ref_mjs_1.useRef; } });
|
|
27
|
+
var use_keypress_mjs_1 = require('./lib/use-keypress.js');
|
|
28
|
+
Object.defineProperty(exports, "useKeypress", { enumerable: true, get: function () { return use_keypress_mjs_1.useKeypress; } });
|
|
29
|
+
var use_pagination_mjs_1 = require('./lib/use-pagination.js');
|
|
30
|
+
Object.defineProperty(exports, "usePagination", { enumerable: true, get: function () { return use_pagination_mjs_1.usePagination; } });
|
|
31
|
+
var create_prompt_mjs_1 = require('./lib/create-prompt.js');
|
|
32
|
+
Object.defineProperty(exports, "createPrompt", { enumerable: true, get: function () { return create_prompt_mjs_1.createPrompt; } });
|
|
33
|
+
var Separator_mjs_1 = require('./lib/Separator.js');
|
|
34
|
+
Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return Separator_mjs_1.Separator; } });
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Separator = void 0;
|
|
7
|
-
const chalk_1 =
|
|
8
|
-
const figures_1 =
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
const figures_1 = require("figures");
|
|
9
6
|
/**
|
|
10
7
|
* Separator object
|
|
11
8
|
* Used to space/separate choices group
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.createPrompt = void 0;
|
|
13
|
+
const readline = require("node:readline");
|
|
14
|
+
const type_1 = require("@inquirer/type");
|
|
15
|
+
const mute_stream_1 = require("mute-stream");
|
|
16
|
+
const signal_exit_1 = require("signal-exit");
|
|
17
|
+
const screen_manager_mjs_1 = require('./screen-manager.js');
|
|
18
|
+
const hook_engine_mjs_1 = require('./hook-engine.js');
|
|
19
|
+
// Take an AsyncPromptConfig and resolves all it's values.
|
|
20
|
+
function getPromptConfig(config) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const message = typeof config.message === 'function' ? config.message() : config.message;
|
|
23
|
+
return Object.assign(Object.assign({ validate: () => true }, config), { message: yield message });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function createPrompt(view) {
|
|
27
|
+
const prompt = (config, context) => {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
// Default `input` to stdin
|
|
30
|
+
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
31
|
+
// Add mute capabilities to the output
|
|
32
|
+
const output = new mute_stream_1.default();
|
|
33
|
+
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
34
|
+
const rl = readline.createInterface({
|
|
35
|
+
terminal: true,
|
|
36
|
+
input,
|
|
37
|
+
output,
|
|
38
|
+
});
|
|
39
|
+
const screen = new screen_manager_mjs_1.default(rl);
|
|
40
|
+
let cancel = () => { };
|
|
41
|
+
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
42
|
+
(0, hook_engine_mjs_1.withHooks)(rl, (store) => {
|
|
43
|
+
function checkCursorPos() {
|
|
44
|
+
screen.checkCursorPos();
|
|
45
|
+
}
|
|
46
|
+
const removeExitListener = (0, signal_exit_1.onExit)((code, signal) => {
|
|
47
|
+
onExit();
|
|
48
|
+
reject(new Error(`User force closed the prompt with ${code} ${signal}`));
|
|
49
|
+
});
|
|
50
|
+
function onExit() {
|
|
51
|
+
try {
|
|
52
|
+
store.hooksCleanup.forEach((cleanFn) => {
|
|
53
|
+
cleanFn === null || cleanFn === void 0 ? void 0 : cleanFn();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
reject(err);
|
|
58
|
+
}
|
|
59
|
+
if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
|
|
60
|
+
screen.clean();
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
screen.clearContent();
|
|
64
|
+
}
|
|
65
|
+
screen.done();
|
|
66
|
+
removeExitListener();
|
|
67
|
+
store.rl.input.removeListener('keypress', checkCursorPos);
|
|
68
|
+
}
|
|
69
|
+
cancel = () => {
|
|
70
|
+
onExit();
|
|
71
|
+
reject(new Error('Prompt was canceled'));
|
|
72
|
+
};
|
|
73
|
+
function done(value) {
|
|
74
|
+
// Delay execution to let time to the hookCleanup functions to registers.
|
|
75
|
+
setImmediate(() => {
|
|
76
|
+
onExit();
|
|
77
|
+
// Finally we resolve our promise
|
|
78
|
+
resolve(value);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function workLoop(resolvedConfig) {
|
|
82
|
+
store.index = 0;
|
|
83
|
+
store.handleChange = () => workLoop(resolvedConfig);
|
|
84
|
+
try {
|
|
85
|
+
const nextView = view(resolvedConfig, done);
|
|
86
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
87
|
+
screen.render(content, bottomContent);
|
|
88
|
+
hook_engine_mjs_1.effectScheduler.run();
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
onExit();
|
|
92
|
+
reject(err);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// TODO: we should display a loader while we get the default options.
|
|
96
|
+
getPromptConfig(config).then((resolvedConfig) => {
|
|
97
|
+
workLoop(resolvedConfig);
|
|
98
|
+
// Re-renders only happen when the state change; but the readline cursor could change position
|
|
99
|
+
// and that also requires a re-render (and a manual one because we mute the streams).
|
|
100
|
+
// We set the listener after the initial workLoop to avoid a double render if render triggered
|
|
101
|
+
// by a state change sets the cursor to the right position.
|
|
102
|
+
store.rl.input.on('keypress', checkCursorPos);
|
|
103
|
+
}, reject);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
answer.cancel = cancel;
|
|
107
|
+
return answer;
|
|
108
|
+
};
|
|
109
|
+
return prompt;
|
|
110
|
+
}
|
|
111
|
+
exports.createPrompt = createPrompt;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.effectScheduler = exports.handleChange = exports.withPointer = exports.withUpdates = exports.readline = exports.withHooks = void 0;
|
|
4
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
+
const hookStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
6
|
+
function createStore(rl) {
|
|
7
|
+
const store = {
|
|
8
|
+
rl,
|
|
9
|
+
hooks: [],
|
|
10
|
+
hooksCleanup: [],
|
|
11
|
+
hooksEffect: [],
|
|
12
|
+
index: 0,
|
|
13
|
+
handleChange() { },
|
|
14
|
+
};
|
|
15
|
+
return store;
|
|
16
|
+
}
|
|
17
|
+
// Run callback in with the hook engine setup.
|
|
18
|
+
function withHooks(rl, cb) {
|
|
19
|
+
const store = createStore(rl);
|
|
20
|
+
return hookStorage.run(store, () => {
|
|
21
|
+
cb(store);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
exports.withHooks = withHooks;
|
|
25
|
+
// Safe getStore utility that'll return the store or throw if undefined.
|
|
26
|
+
function getStore() {
|
|
27
|
+
const store = hookStorage.getStore();
|
|
28
|
+
if (!store) {
|
|
29
|
+
throw new Error('[Inquirer] Hook functions can only be called from within a prompt');
|
|
30
|
+
}
|
|
31
|
+
return store;
|
|
32
|
+
}
|
|
33
|
+
function readline() {
|
|
34
|
+
return getStore().rl;
|
|
35
|
+
}
|
|
36
|
+
exports.readline = readline;
|
|
37
|
+
// Merge state updates happening within the callback function to avoid multiple renders.
|
|
38
|
+
function withUpdates(fn) {
|
|
39
|
+
const wrapped = (...args) => {
|
|
40
|
+
const store = getStore();
|
|
41
|
+
let shouldUpdate = false;
|
|
42
|
+
const oldHandleChange = store.handleChange;
|
|
43
|
+
store.handleChange = () => {
|
|
44
|
+
shouldUpdate = true;
|
|
45
|
+
};
|
|
46
|
+
const returnValue = fn(...args);
|
|
47
|
+
if (shouldUpdate) {
|
|
48
|
+
oldHandleChange();
|
|
49
|
+
}
|
|
50
|
+
store.handleChange = oldHandleChange;
|
|
51
|
+
return returnValue;
|
|
52
|
+
};
|
|
53
|
+
return node_async_hooks_1.AsyncResource.bind(wrapped);
|
|
54
|
+
}
|
|
55
|
+
exports.withUpdates = withUpdates;
|
|
56
|
+
function withPointer(cb) {
|
|
57
|
+
const store = getStore();
|
|
58
|
+
const { index } = store;
|
|
59
|
+
const pointer = {
|
|
60
|
+
get() {
|
|
61
|
+
return store.hooks[index];
|
|
62
|
+
},
|
|
63
|
+
set(value) {
|
|
64
|
+
store.hooks[index] = value;
|
|
65
|
+
},
|
|
66
|
+
initialized: index in store.hooks,
|
|
67
|
+
};
|
|
68
|
+
const returnValue = cb(pointer);
|
|
69
|
+
store.index++;
|
|
70
|
+
return returnValue;
|
|
71
|
+
}
|
|
72
|
+
exports.withPointer = withPointer;
|
|
73
|
+
function handleChange() {
|
|
74
|
+
getStore().handleChange();
|
|
75
|
+
}
|
|
76
|
+
exports.handleChange = handleChange;
|
|
77
|
+
exports.effectScheduler = {
|
|
78
|
+
queue(cb) {
|
|
79
|
+
const store = getStore();
|
|
80
|
+
const { index } = store;
|
|
81
|
+
store.hooksEffect.push(() => {
|
|
82
|
+
var _a, _b;
|
|
83
|
+
(_b = (_a = store.hooksCleanup)[index]) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
84
|
+
const cleanFn = cb(readline());
|
|
85
|
+
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
86
|
+
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
87
|
+
}
|
|
88
|
+
store.hooksCleanup[index] = cleanFn;
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
run() {
|
|
92
|
+
const store = getStore();
|
|
93
|
+
withUpdates(() => {
|
|
94
|
+
store.hooksEffect.forEach((effect) => {
|
|
95
|
+
effect();
|
|
96
|
+
});
|
|
97
|
+
})();
|
|
98
|
+
store.hooksEffect.length = 0;
|
|
99
|
+
},
|
|
100
|
+
};
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const cli_width_1 =
|
|
7
|
-
const strip_ansi_1 =
|
|
8
|
-
const ansi_escapes_1 =
|
|
3
|
+
const cli_width_1 = require("cli-width");
|
|
4
|
+
const strip_ansi_1 = require("strip-ansi");
|
|
5
|
+
const ansi_escapes_1 = require("ansi-escapes");
|
|
9
6
|
const utils_mjs_1 = require('./utils.js');
|
|
10
7
|
const height = (content) => content.split('\n').length;
|
|
11
8
|
const lastLine = (content) => { var _a; return (_a = content.split('\n').pop()) !== null && _a !== void 0 ? _a : ''; };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useEffect = void 0;
|
|
4
|
+
const hook_engine_mjs_1 = require('./hook-engine.js');
|
|
5
|
+
function useEffect(cb, depArray) {
|
|
6
|
+
(0, hook_engine_mjs_1.withPointer)((pointer) => {
|
|
7
|
+
const oldDeps = pointer.get();
|
|
8
|
+
const hasChanged = !Array.isArray(oldDeps) || depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
9
|
+
if (hasChanged) {
|
|
10
|
+
hook_engine_mjs_1.effectScheduler.queue(cb);
|
|
11
|
+
}
|
|
12
|
+
pointer.set(depArray);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
exports.useEffect = useEffect;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useKeypress = void 0;
|
|
4
|
+
const use_ref_mjs_1 = require('./use-ref.js');
|
|
5
|
+
const use_effect_mjs_1 = require('./use-effect.js');
|
|
6
|
+
const hook_engine_mjs_1 = require('./hook-engine.js');
|
|
7
|
+
function useKeypress(userHandler) {
|
|
8
|
+
const signal = (0, use_ref_mjs_1.useRef)(userHandler);
|
|
9
|
+
signal.current = userHandler;
|
|
10
|
+
(0, use_effect_mjs_1.useEffect)((rl) => {
|
|
11
|
+
const handler = (0, hook_engine_mjs_1.withUpdates)((_input, event) => {
|
|
12
|
+
signal.current(event, rl);
|
|
13
|
+
});
|
|
14
|
+
rl.input.on('keypress', handler);
|
|
15
|
+
return () => {
|
|
16
|
+
rl.input.removeListener('keypress', handler);
|
|
17
|
+
};
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
exports.useKeypress = useKeypress;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePagination = void 0;
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
const cli_width_1 = require("cli-width");
|
|
6
|
+
const utils_mjs_1 = require('./utils.js');
|
|
7
|
+
const hook_engine_mjs_1 = require('./hook-engine.js');
|
|
8
|
+
const use_ref_mjs_1 = require('./use-ref.js');
|
|
9
|
+
function usePagination(output, { active, pageSize = 7, }) {
|
|
10
|
+
const rl = (0, hook_engine_mjs_1.readline)();
|
|
11
|
+
const state = (0, use_ref_mjs_1.useRef)({
|
|
12
|
+
pointer: 0,
|
|
13
|
+
lastIndex: 0,
|
|
14
|
+
});
|
|
15
|
+
const width = (0, cli_width_1.default)({ defaultWidth: 80, output: rl.output });
|
|
16
|
+
const lines = (0, utils_mjs_1.breakLines)(output, width).split('\n');
|
|
17
|
+
// Make sure there's enough lines to paginate
|
|
18
|
+
if (lines.length <= pageSize) {
|
|
19
|
+
return output;
|
|
20
|
+
}
|
|
21
|
+
const middleOfList = Math.floor(pageSize / 2);
|
|
22
|
+
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
23
|
+
const { pointer: prevPointer, lastIndex } = state.current;
|
|
24
|
+
if (prevPointer < middleOfList && lastIndex < active && active - lastIndex < pageSize) {
|
|
25
|
+
state.current.pointer = Math.min(middleOfList, prevPointer + active - lastIndex);
|
|
26
|
+
}
|
|
27
|
+
state.current.lastIndex = active;
|
|
28
|
+
// Duplicate the lines so it give an infinite list look
|
|
29
|
+
const infinite = [lines, lines, lines].flat();
|
|
30
|
+
const topIndex = Math.max(0, active + lines.length - state.current.pointer);
|
|
31
|
+
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
32
|
+
return section + '\n' + chalk_1.default.dim('(Use arrow keys to reveal more choices)');
|
|
33
|
+
}
|
|
34
|
+
exports.usePagination = usePagination;
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.usePrefix = void 0;
|
|
7
|
-
const chalk_1 =
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
const spinners = require("cli-spinners");
|
|
6
|
+
const use_state_mjs_1 = require('./use-state.js');
|
|
7
|
+
const use_effect_mjs_1 = require('./use-effect.js');
|
|
8
|
+
const spinner = spinners.dots;
|
|
11
9
|
function usePrefix(isLoading = false) {
|
|
12
|
-
const [tick, setTick] = (0,
|
|
13
|
-
(0,
|
|
10
|
+
const [tick, setTick] = (0, use_state_mjs_1.useState)(0);
|
|
11
|
+
(0, use_effect_mjs_1.useEffect)(() => {
|
|
14
12
|
if (isLoading) {
|
|
15
13
|
const timeout = setTimeout(() => {
|
|
16
14
|
setTick(tick + 1);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useRef = void 0;
|
|
4
|
+
const use_state_mjs_1 = require('./use-state.js');
|
|
5
|
+
function useRef(val) {
|
|
6
|
+
return (0, use_state_mjs_1.useState)({ current: val })[0];
|
|
7
|
+
}
|
|
8
|
+
exports.useRef = useRef;
|