mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-02-03 23:42:50 +00:00
refactor(inquirer):generic type
This commit is contained in:
parent
d8eae6101a
commit
a5d521c242
46
types/inquirer/index.d.ts
vendored
46
types/inquirer/index.d.ts
vendored
@ -1,18 +1,23 @@
|
||||
// TypeScript Version: 2.3
|
||||
// Type definitions for Inquirer.js
|
||||
// Project: https://github.com/SBoudrias/Inquirer.js
|
||||
// Definitions by: Qubo <https://github.com/tkQubo>
|
||||
// Parvez <https://github.com/ppathan>
|
||||
// Jouderian <https://github.com/jouderianjr>
|
||||
// Qibang <https://github.com/bang88>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="rx" />
|
||||
|
||||
import through = require('through');
|
||||
import through = require("through");
|
||||
|
||||
declare namespace inquirer {
|
||||
type Prompts = { [name: string]: PromptModule };
|
||||
type ChoiceType = string | objects.ChoiceOption | objects.Separator;
|
||||
type Questions = Question | Question[] | Rx.Observable<Question>;
|
||||
type Questions<T> =
|
||||
| Question<T>
|
||||
| Question<T>[]
|
||||
| Rx.Observable<Question<T>>;
|
||||
|
||||
interface Inquirer {
|
||||
restoreDefaultPrompts(): void;
|
||||
@ -32,19 +37,19 @@ declare namespace inquirer {
|
||||
* @param cb Callback being passed the user answers
|
||||
* @return
|
||||
*/
|
||||
prompt(questions: Questions, cb: (answers: Answers) => any): ui.Prompt;
|
||||
prompt(questions: Questions): Promise<Answers>;
|
||||
prompt<T>(questions: Questions<T>, cb: (answers: T) => any): ui.Prompt;
|
||||
prompt<T>(questions: Questions<T>): Promise<T>;
|
||||
prompts: Prompts;
|
||||
Separator: objects.SeparatorStatic;
|
||||
ui: {
|
||||
BottomBar: ui.BottomBar;
|
||||
Prompt: ui.Prompt;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
interface PromptModule {
|
||||
(questions: Questions): Promise<Answers>;
|
||||
(questions: Questions, cb: (answers: Answers) => any): ui.Prompt;
|
||||
<T>(questions: Questions<T>): Promise<T>;
|
||||
<T>(questions: Questions<T>, cb: (answers: T) => any): ui.Prompt;
|
||||
/**
|
||||
* Register a prompt type
|
||||
* @param name Prompt type name
|
||||
@ -57,7 +62,7 @@ declare namespace inquirer {
|
||||
restoreDefaultPrompts(): void;
|
||||
}
|
||||
|
||||
interface Question {
|
||||
interface Question<T = Answers> {
|
||||
/**
|
||||
* Type of the prompt.
|
||||
* Possible values:
|
||||
@ -79,24 +84,24 @@ declare namespace inquirer {
|
||||
* The question to print. If defined as a function,
|
||||
* the first parameter will be the current inquirer session answers.
|
||||
*/
|
||||
message?: string | ((answers: Answers) => string);
|
||||
message?: string | ((answers: T) => string);
|
||||
/**
|
||||
* Default value(s) to use if nothing is entered, or a function that returns the default value(s).
|
||||
* If defined as a function, the first parameter will be the current inquirer session answers.
|
||||
*/
|
||||
default?: any | ((answers: Answers) => any);
|
||||
default?: any | ((answers: T) => any);
|
||||
/**
|
||||
* Choices array or a function returning a choices array. If defined as a function,
|
||||
* the first parameter will be the current inquirer session answers.
|
||||
* Array values can be simple strings, or objects containing a name (to display) and a value properties
|
||||
* (to save in the answers hash). Values can also be a Separator.
|
||||
*/
|
||||
choices?: ChoiceType[] | ((answers: Answers) => ChoiceType[]);
|
||||
choices?: ChoiceType[] | ((answers: T) => ChoiceType[]);
|
||||
/**
|
||||
* Receive the user input and should return true if the value is valid, and an error message (String)
|
||||
* otherwise. If false is returned, a default error message is provided.
|
||||
*/
|
||||
validate?(input: string, answers?: Answers): boolean | string;
|
||||
validate?(input: string, answers?: T): boolean | string;
|
||||
/**
|
||||
* Receive the user input and return the filtered value to be used inside the program.
|
||||
* The value returned will be added to the Answers hash.
|
||||
@ -106,7 +111,7 @@ declare namespace inquirer {
|
||||
* Receive the current user answers hash and should return true or false depending on whether or
|
||||
* not this question should be asked. The value can also be a simple boolean.
|
||||
*/
|
||||
when?: boolean | ((answers: Answers) => boolean);
|
||||
when?: boolean | ((answers: T) => boolean);
|
||||
paginated?: boolean;
|
||||
/**
|
||||
* Change the number of lines that will be rendered when using list, rawList, expand or checkbox.
|
||||
@ -143,10 +148,10 @@ declare namespace inquirer {
|
||||
* Once all prompt are over
|
||||
*/
|
||||
onCompletion(): void;
|
||||
processQuestion(question: Question): any;
|
||||
fetchAnswer(question: Question): any;
|
||||
setDefaultType(question: Question): any;
|
||||
filterIfRunnable(question: Question): any;
|
||||
processQuestion<T>(question: Question<T>): any;
|
||||
fetchAnswer<T>(question: Question<T>): any;
|
||||
setDefaultType<T>(question: Question<T>): any;
|
||||
filterIfRunnable<T>(question: Question<T>): any;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -237,7 +242,7 @@ declare namespace inquirer {
|
||||
extra?: any;
|
||||
key?: string;
|
||||
checked?: boolean;
|
||||
disabled?: string | ((answers: Answers) => any);
|
||||
disabled?: string | (<T = Answers>(answers: T) => any);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -247,7 +252,10 @@ declare namespace inquirer {
|
||||
* @param choices All `choice` to keep in the collection
|
||||
*/
|
||||
interface Choices {
|
||||
new (choices: (string | Separator | ChoiceOption)[], answers?: Answers): Choices;
|
||||
new <T = Answers>(
|
||||
choices: (string | Separator | ChoiceOption)[],
|
||||
answers?: T
|
||||
): Choices;
|
||||
choices: Choice[];
|
||||
realChoices: Choice[];
|
||||
length: number;
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
import inquirer = require('inquirer');
|
||||
import inquirer = require("inquirer");
|
||||
|
||||
|
||||
inquirer.prompt([/* Pass your questions in here */], function( answers: inquirer.Answers ) {
|
||||
// Use user feedback for... whatever!!
|
||||
});
|
||||
inquirer.prompt(
|
||||
[
|
||||
/* Pass your questions in here */
|
||||
],
|
||||
function(answers: inquirer.Answers) {
|
||||
// Use user feedback for... whatever!!
|
||||
}
|
||||
);
|
||||
|
||||
//
|
||||
// examples/bottom-bar.js
|
||||
@ -13,29 +17,25 @@ inquirer.prompt([/* Pass your questions in here */], function( answers: inquirer
|
||||
var BottomBar = inquirer.ui.BottomBar;
|
||||
declare var cmdify: any;
|
||||
|
||||
var loader = [
|
||||
"/ Installing",
|
||||
"| Installing",
|
||||
"\\ Installing",
|
||||
"- Installing"
|
||||
];
|
||||
var loader = ["/ Installing", "| Installing", "\\ Installing", "- Installing"];
|
||||
var i = 4;
|
||||
var ui = new BottomBar({ bottomBar: loader[i % 4] });
|
||||
|
||||
setInterval(function() {
|
||||
ui.updateBottomBar( loader[i++ % 4] );
|
||||
}, 300 );
|
||||
ui.updateBottomBar(loader[i++ % 4]);
|
||||
}, 300);
|
||||
|
||||
var spawn = require("child_process").spawn;
|
||||
|
||||
var cmd = spawn(cmdify("npm"), [ "-g", "install", "inquirer" ], { stdio: "pipe" });
|
||||
cmd.stdout.pipe( ui.log );
|
||||
cmd.on( "close", function() {
|
||||
var cmd = spawn(cmdify("npm"), ["-g", "install", "inquirer"], {
|
||||
stdio: "pipe"
|
||||
});
|
||||
cmd.stdout.pipe(ui.log);
|
||||
cmd.on("close", function() {
|
||||
ui.updateBottomBar("Installation done!\n");
|
||||
process.exit();
|
||||
});
|
||||
|
||||
|
||||
//
|
||||
// examples/checkbox.js
|
||||
//
|
||||
@ -47,49 +47,51 @@ cmd.on( "close", function() {
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
inquirer.prompt([
|
||||
{
|
||||
type: "checkbox",
|
||||
message: "Select toppings",
|
||||
name: "toppings",
|
||||
choices: [
|
||||
new inquirer.Separator("The usual:"),
|
||||
{
|
||||
name: "Pepperoni"
|
||||
},
|
||||
{
|
||||
name: "Cheese",
|
||||
checked: true
|
||||
},
|
||||
{
|
||||
name: "Mushroom"
|
||||
},
|
||||
new inquirer.Separator("The extras:"),
|
||||
{
|
||||
name: "Pineapple",
|
||||
},
|
||||
{
|
||||
name: "Bacon"
|
||||
},
|
||||
{
|
||||
name: "Olives",
|
||||
disabled: "out of stock"
|
||||
},
|
||||
{
|
||||
name: "Extra cheese"
|
||||
inquirer.prompt<{ toppings: string }>(
|
||||
[
|
||||
{
|
||||
type: "checkbox",
|
||||
message: "Select toppings",
|
||||
name: "toppings",
|
||||
choices: [
|
||||
new inquirer.Separator("The usual:"),
|
||||
{
|
||||
name: "Pepperoni"
|
||||
},
|
||||
{
|
||||
name: "Cheese",
|
||||
checked: true
|
||||
},
|
||||
{
|
||||
name: "Mushroom"
|
||||
},
|
||||
new inquirer.Separator("The extras:"),
|
||||
{
|
||||
name: "Pineapple"
|
||||
},
|
||||
{
|
||||
name: "Bacon"
|
||||
},
|
||||
{
|
||||
name: "Olives",
|
||||
disabled: "out of stock"
|
||||
},
|
||||
{
|
||||
name: "Extra cheese"
|
||||
}
|
||||
],
|
||||
validate: function(answer) {
|
||||
if (answer.length < 1) {
|
||||
return "You must choose at least one topping.";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
],
|
||||
validate: function( answer ) {
|
||||
if ( answer.length < 1 ) {
|
||||
return "You must choose at least one topping.";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
],
|
||||
function(answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
}
|
||||
], function( answers: inquirer.Answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
});
|
||||
|
||||
);
|
||||
|
||||
//
|
||||
// examples/expand.js
|
||||
@ -102,39 +104,41 @@ inquirer.prompt([
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
inquirer.prompt([
|
||||
{
|
||||
type: "expand",
|
||||
message: "Conflict on `file.js`: ",
|
||||
name: "overwrite",
|
||||
choices: [
|
||||
{
|
||||
key: "y",
|
||||
name: "Overwrite",
|
||||
value: "overwrite"
|
||||
},
|
||||
{
|
||||
key: "a",
|
||||
name: "Overwrite this one and all next",
|
||||
value: "overwrite_all"
|
||||
},
|
||||
{
|
||||
key: "d",
|
||||
name: "Show diff",
|
||||
value: "diff"
|
||||
},
|
||||
new inquirer.Separator(),
|
||||
{
|
||||
key: "x",
|
||||
name: "Abort",
|
||||
value: "abort"
|
||||
}
|
||||
]
|
||||
inquirer.prompt(
|
||||
[
|
||||
{
|
||||
type: "expand",
|
||||
message: "Conflict on `file.js`: ",
|
||||
name: "overwrite",
|
||||
choices: [
|
||||
{
|
||||
key: "y",
|
||||
name: "Overwrite",
|
||||
value: "overwrite"
|
||||
},
|
||||
{
|
||||
key: "a",
|
||||
name: "Overwrite this one and all next",
|
||||
value: "overwrite_all"
|
||||
},
|
||||
{
|
||||
key: "d",
|
||||
name: "Show diff",
|
||||
value: "diff"
|
||||
},
|
||||
new inquirer.Separator(),
|
||||
{
|
||||
key: "x",
|
||||
name: "Abort",
|
||||
value: "abort"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
function(answers: inquirer.Answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
}
|
||||
], function( answers: inquirer.Answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
});
|
||||
|
||||
);
|
||||
|
||||
//
|
||||
// examples/input.js
|
||||
@ -152,21 +156,25 @@ var questions = [
|
||||
type: "input",
|
||||
name: "first_name",
|
||||
message: "What's your first name",
|
||||
prefix: "1 - ",
|
||||
prefix: "1 - "
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
name: "last_name",
|
||||
message: "What's your last name",
|
||||
default: function () { return "Doe"; },
|
||||
default: function() {
|
||||
return "Doe";
|
||||
},
|
||||
suffix: "!!"
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
name: "phone",
|
||||
message: "What's your phone number",
|
||||
validate: function( value: string ): string|boolean {
|
||||
var pass = value.match(/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i);
|
||||
validate: function(value: string): string | boolean {
|
||||
var pass = value.match(
|
||||
/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i
|
||||
);
|
||||
if (pass) {
|
||||
return true;
|
||||
} else {
|
||||
@ -176,15 +184,14 @@ var questions = [
|
||||
}
|
||||
];
|
||||
|
||||
inquirer.prompt( questions, function( answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
inquirer.prompt(questions, function(answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
});
|
||||
|
||||
//
|
||||
// examples/list.js
|
||||
//
|
||||
|
||||
|
||||
/**
|
||||
* List prompt example
|
||||
*/
|
||||
@ -192,29 +199,34 @@ inquirer.prompt( questions, function( answers ) {
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
inquirer.prompt([
|
||||
{
|
||||
type: "list",
|
||||
name: "theme",
|
||||
message: "What do you want to do?",
|
||||
choices: [
|
||||
"Order a pizza",
|
||||
"Make a reservation",
|
||||
new inquirer.Separator(),
|
||||
"Ask opening hours",
|
||||
"Talk to the receptionist"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "list",
|
||||
name: "size",
|
||||
message: "What size do you need",
|
||||
choices: [ "Jumbo", "Large", "Standard", "Medium", "Small", "Micro" ],
|
||||
filter: function( val: string ) { return val.toLowerCase(); }
|
||||
inquirer.prompt(
|
||||
[
|
||||
{
|
||||
type: "list",
|
||||
name: "theme",
|
||||
message: "What do you want to do?",
|
||||
choices: [
|
||||
"Order a pizza",
|
||||
"Make a reservation",
|
||||
new inquirer.Separator(),
|
||||
"Ask opening hours",
|
||||
"Talk to the receptionist"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "list",
|
||||
name: "size",
|
||||
message: "What size do you need",
|
||||
choices: ["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],
|
||||
filter: function(val: string) {
|
||||
return val.toLowerCase();
|
||||
}
|
||||
}
|
||||
],
|
||||
function(answers: inquirer.Answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
}
|
||||
], function( answers: inquirer.Answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
});
|
||||
);
|
||||
|
||||
//
|
||||
// examples/long-list.js
|
||||
@ -227,30 +239,35 @@ inquirer.prompt([
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
var choices = Array.apply(0, new Array(26)).map(function(x: number,y: number) {
|
||||
var choices = Array.apply(0, new Array(26)).map(function(x: number, y: number) {
|
||||
return String.fromCharCode(y + 65);
|
||||
});
|
||||
choices.push("Multiline option \n super cool feature");
|
||||
choices.push("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.");
|
||||
choices.push(
|
||||
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium."
|
||||
);
|
||||
|
||||
inquirer.prompt([
|
||||
{
|
||||
type : "list",
|
||||
name : "letter",
|
||||
message : "What's your favorite letter?",
|
||||
paginated : true,
|
||||
choices : choices
|
||||
},
|
||||
{
|
||||
type : "checkbox",
|
||||
name : "name",
|
||||
message : "Select the letter contained in your name:",
|
||||
paginated : true,
|
||||
choices : choices
|
||||
inquirer.prompt(
|
||||
[
|
||||
{
|
||||
type: "list",
|
||||
name: "letter",
|
||||
message: "What's your favorite letter?",
|
||||
paginated: true,
|
||||
choices: choices
|
||||
},
|
||||
{
|
||||
type: "checkbox",
|
||||
name: "name",
|
||||
message: "Select the letter contained in your name:",
|
||||
paginated: true,
|
||||
choices: choices
|
||||
}
|
||||
],
|
||||
function(answers: inquirer.Answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
}
|
||||
], function( answers: inquirer.Answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
});
|
||||
);
|
||||
|
||||
//
|
||||
// examples/nested-call.js
|
||||
@ -263,19 +280,22 @@ inquirer.prompt([
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
inquirer.prompt({
|
||||
type: "list",
|
||||
name: "chocolate",
|
||||
message: "What's your favorite chocolate?",
|
||||
choices: [ "Mars", "Oh Henry", "Hershey" ]
|
||||
}, function( answers: inquirer.Answers ) {
|
||||
inquirer.prompt({
|
||||
inquirer.prompt(
|
||||
{
|
||||
type: "list",
|
||||
name: "beverage",
|
||||
message: "And your favorite beverage?",
|
||||
choices: [ "Pepsi", "Coke", "7up", "Mountain Dew", "Red Bull" ]
|
||||
});
|
||||
});
|
||||
name: "chocolate",
|
||||
message: "What's your favorite chocolate?",
|
||||
choices: ["Mars", "Oh Henry", "Hershey"]
|
||||
},
|
||||
function(answers: inquirer.Answers) {
|
||||
inquirer.prompt({
|
||||
type: "list",
|
||||
name: "beverage",
|
||||
message: "And your favorite beverage?",
|
||||
choices: ["Pepsi", "Coke", "7up", "Mountain Dew", "Red Bull"]
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
//
|
||||
// examples/password.js
|
||||
@ -288,15 +308,18 @@ inquirer.prompt({
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
inquirer.prompt([
|
||||
{
|
||||
type: "password",
|
||||
message: "Enter your git password",
|
||||
name: "password"
|
||||
inquirer.prompt(
|
||||
[
|
||||
{
|
||||
type: "password",
|
||||
message: "Enter your git password",
|
||||
name: "password"
|
||||
}
|
||||
],
|
||||
function(answers: inquirer.Answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
}
|
||||
], function( answers: inquirer.Answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
});
|
||||
);
|
||||
|
||||
//
|
||||
// examples/pizza.js
|
||||
@ -323,8 +346,10 @@ var questions2 = [
|
||||
type: "input",
|
||||
name: "phone",
|
||||
message: "What's your phone number",
|
||||
validate: function( value: string ): string|boolean {
|
||||
var pass = value.match(/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i);
|
||||
validate: function(value: string): string | boolean {
|
||||
var pass = value.match(
|
||||
/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i
|
||||
);
|
||||
if (pass) {
|
||||
return true;
|
||||
} else {
|
||||
@ -336,14 +361,16 @@ var questions2 = [
|
||||
type: "list",
|
||||
name: "size",
|
||||
message: "What size do you need",
|
||||
choices: [ "Large", "Medium", "Small" ],
|
||||
filter: function( val: string ) { return val.toLowerCase(); }
|
||||
choices: ["Large", "Medium", "Small"],
|
||||
filter: function(val: string) {
|
||||
return val.toLowerCase();
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
name: "quantity",
|
||||
message: "How many do you need",
|
||||
validate: function( value: string ) {
|
||||
validate: function(value: string) {
|
||||
var valid = !isNaN(parseFloat(value));
|
||||
return valid || "Please enter a number";
|
||||
},
|
||||
@ -375,7 +402,7 @@ var questions2 = [
|
||||
type: "rawlist",
|
||||
name: "beverage",
|
||||
message: "You also get a free 2L beverage",
|
||||
choices: [ "Pepsi", "7up", "Coke" ]
|
||||
choices: ["Pepsi", "7up", "Coke"]
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
@ -387,16 +414,16 @@ var questions2 = [
|
||||
type: "list",
|
||||
name: "prize",
|
||||
message: "For leaving a comments, you get a freebie",
|
||||
choices: [ "cake", "fries" ],
|
||||
when: function( answers: inquirer.Answers ) {
|
||||
return answers['comments'] !== "Nope, all good!";
|
||||
choices: ["cake", "fries"],
|
||||
when: function(answers: inquirer.Answers) {
|
||||
return answers["comments"] !== "Nope, all good!";
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
inquirer.prompt( questions, function( answers ) {
|
||||
inquirer.prompt(questions, function(answers) {
|
||||
console.log("\nOrder receipt:");
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
});
|
||||
|
||||
//
|
||||
@ -410,29 +437,34 @@ inquirer.prompt( questions, function( answers ) {
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
inquirer.prompt([
|
||||
{
|
||||
type: "rawlist",
|
||||
name: "theme",
|
||||
message: "What do you want to do?",
|
||||
choices: [
|
||||
"Order a pizza",
|
||||
"Make a reservation",
|
||||
new inquirer.Separator(),
|
||||
"Ask opening hours",
|
||||
"Talk to the receptionist"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "rawlist",
|
||||
name: "size",
|
||||
message: "What size do you need",
|
||||
choices: [ "Jumbo", "Large", "Standard", "Medium", "Small", "Micro" ],
|
||||
filter: function( val: string ) { return val.toLowerCase(); }
|
||||
inquirer.prompt(
|
||||
[
|
||||
{
|
||||
type: "rawlist",
|
||||
name: "theme",
|
||||
message: "What do you want to do?",
|
||||
choices: [
|
||||
"Order a pizza",
|
||||
"Make a reservation",
|
||||
new inquirer.Separator(),
|
||||
"Ask opening hours",
|
||||
"Talk to the receptionist"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "rawlist",
|
||||
name: "size",
|
||||
message: "What size do you need",
|
||||
choices: ["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],
|
||||
filter: function(val: string) {
|
||||
return val.toLowerCase();
|
||||
}
|
||||
}
|
||||
],
|
||||
function(answers: inquirer.Answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
}
|
||||
], function( answers: inquirer.Answers ) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
});
|
||||
);
|
||||
|
||||
//
|
||||
// examples/recursive.js
|
||||
@ -445,7 +477,7 @@ inquirer.prompt([
|
||||
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
var output2: (string|boolean)[] = [];
|
||||
var output2: (string | boolean)[] = [];
|
||||
|
||||
var questions3 = [
|
||||
{
|
||||
@ -456,18 +488,21 @@ var questions3 = [
|
||||
{
|
||||
type: "confirm",
|
||||
name: "askAgain",
|
||||
message: "Want to enter another TV show favorite (just hit enter for YES)?",
|
||||
message:
|
||||
"Want to enter another TV show favorite (just hit enter for YES)?",
|
||||
default: true
|
||||
}
|
||||
];
|
||||
|
||||
function ask() {
|
||||
inquirer.prompt( questions3, function( answers: inquirer.Answers ) {
|
||||
output2.push( answers['tvShow'] );
|
||||
if ( answers['askAgain'] ) {
|
||||
inquirer.prompt<{ tvShow: string; askAgain: boolean }>(questions3, function(
|
||||
answers
|
||||
) {
|
||||
output2.push(answers.tvShow);
|
||||
if (answers.askAgain) {
|
||||
ask();
|
||||
} else {
|
||||
console.log( "Your favorite TV Shows:", output2.join(", ") );
|
||||
console.log("Your favorite TV Shows:", output2.join(", "));
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -478,14 +513,17 @@ ask();
|
||||
// examples/when.js
|
||||
//
|
||||
|
||||
|
||||
/**
|
||||
* When example
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
//var inquirer = require("../lib/inquirer");
|
||||
|
||||
interface Answers4 {
|
||||
bacon: boolean;
|
||||
favorite: string;
|
||||
pizza: boolean;
|
||||
}
|
||||
var questions4 = [
|
||||
{
|
||||
type: "confirm",
|
||||
@ -496,32 +534,32 @@ var questions4 = [
|
||||
type: "input",
|
||||
name: "favorite",
|
||||
message: "Bacon lover, what is your favorite type of bacon?",
|
||||
when: function ( answers: inquirer.Answers ) {
|
||||
return answers['bacon'];
|
||||
when: function(answers: Answers4) {
|
||||
return answers.bacon;
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "confirm",
|
||||
name: "pizza",
|
||||
message: "Ok... Do you like pizza?",
|
||||
when: function (answers: inquirer.Answers) {
|
||||
return !likesFood( "bacon" )(answers);
|
||||
when: function(answers: Answers4) {
|
||||
return !likesFood("bacon")(answers);
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
name: "favorite",
|
||||
message: "Whew! What is your favorite type of pizza?",
|
||||
when: likesFood( "pizza" )
|
||||
when: likesFood("pizza")
|
||||
}
|
||||
];
|
||||
|
||||
function likesFood ( aFood: string ) {
|
||||
return function ( answers: inquirer.Answers ) {
|
||||
return answers[ aFood ];
|
||||
}
|
||||
function likesFood(aFood: string) {
|
||||
return function(answers: inquirer.Answers) {
|
||||
return answers[aFood];
|
||||
};
|
||||
}
|
||||
|
||||
inquirer.prompt(questions, function (answers) {
|
||||
console.log( JSON.stringify(answers, null, " ") );
|
||||
inquirer.prompt<Answers4>(questions, function(answers) {
|
||||
console.log(JSON.stringify(answers, null, " "));
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user