UNPKG

1.87 kBJavaScriptView Raw
1/**
2 * Inquirer.js
3 * A collection of common interactive command line user interfaces.
4 */
5import { input, select, number, confirm, rawlist, expand, checkbox, password, editor, Separator, } from '@inquirer/prompts';
6import { default as PromptsRunner } from './ui/prompt.mjs';
7const defaultPrompts = {
8 input,
9 select,
10 /** @deprecated `list` is now named `select` */
11 list: select,
12 number,
13 confirm,
14 rawlist,
15 expand,
16 checkbox,
17 password,
18 editor,
19};
20/**
21 * Create a new self-contained prompt module.
22 */
23export function createPromptModule(opt) {
24 function promptModule(questions, answers) {
25 const runner = new PromptsRunner(promptModule.prompts, opt);
26 try {
27 return runner.run(questions, answers);
28 }
29 catch (error) {
30 const promise = Promise.reject(error);
31 return Object.assign(promise, { ui: runner });
32 }
33 }
34 promptModule.prompts = { ...defaultPrompts };
35 /**
36 * Register a prompt type
37 */
38 promptModule.registerPrompt = function (name, prompt) {
39 promptModule.prompts[name] = prompt;
40 return this;
41 };
42 /**
43 * Register the defaults provider prompts
44 */
45 promptModule.restoreDefaultPrompts = function () {
46 promptModule.prompts = { ...defaultPrompts };
47 };
48 return promptModule;
49}
50/**
51 * Public CLI helper interface
52 */
53const prompt = createPromptModule();
54// Expose helper functions on the top level for easiest usage by common users
55function registerPrompt(name, newPrompt) {
56 prompt.registerPrompt(name, newPrompt);
57}
58function restoreDefaultPrompts() {
59 prompt.restoreDefaultPrompts();
60}
61const inquirer = {
62 prompt,
63 ui: {
64 Prompt: PromptsRunner,
65 },
66 createPromptModule,
67 registerPrompt,
68 restoreDefaultPrompts,
69 Separator,
70};
71export default inquirer;