/
prompt-for-input-vals.js
132 lines (120 loc) · 4.56 KB
/
prompt-for-input-vals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
module.exports = {
friendlyName: 'Prompt for input values',
description: 'Prompt command-line user for the specified machine input values.',
inputs: {
prompts: {
description: 'An array of prompt objects this machine will use to properly prompt for and validate user-defined values for runtime inputs.',
extendedDescription: '`expectType` may be "string" or "password". If it is "password", the relevant prompt will not display typed characters.',
example: [{
name: 'foobar',
example: 'here is what a foobar should look like',
description: 'The foobar that will be used to foo the bar (i.e. input description)',
expectType: 'json'
}]
}
},
exits: {
error: {
description: 'Unexpected error occurred.',
},
success: {
description: 'Done.',
example: [{
name: 'foobar',
value: 'stuff and things'
}]
}
},
fn: function (inputs, exits){
/**
* Module dependencies
*/
var inquirer = require('inquirer');
var chalk = require('chalk');
var _ = require('lodash');
var Path = require('path');
var util = require('util');
// Build array of prompt objects for inquirer
var promptArray;
try {
promptArray = _.reduce(inputs.prompts, function (memo, promptDef){
var inquirerPromptDef = {
name: promptDef.name,
message: (
( promptDef.description ? chalk.reset('Please enter '+promptDef.description[0].toLowerCase()+promptDef.description.slice(1))
: chalk.reset('Please enter a value for ')+chalk.bold(promptDef.name)
) +'\n'+
chalk.bold(promptDef.name)+': '
),
// chalk.reset(chalk.gray(promptDef.description?' '+(promptDef.description[0].toLowerCase()+promptDef.description.slice(1))+'':''))+'\n'+chalk.bold(promptDef.name)+': ',
// message: util.format(chalk.reset('Please enter a value for ')+'%s', chalk.bold(promptDef.name) + chalk.reset(chalk.gray(promptDef.description?' '+(promptDef.description[0].toLowerCase()+promptDef.description.slice(1))+'':''))+'\n'+chalk.bold(promptDef.name)+': '),
// message: util.format('%s', promptDef.name, chalk.reset(chalk.gray(promptDef.description?', '+(promptDef.description[0].toLowerCase()+promptDef.description.slice(1))+'':'')), (promptDef.example?('\n(e.g. '+promptDef.example+')'):''),'\n?'),
// message: util.format('Please enter a value for "%s"', promptDef.name, chalk.reset(chalk.gray(promptDef.description?', '+(promptDef.description[0].toLowerCase()+promptDef.description.slice(1))+'':'')), '\n?'),
type: (function (){
if (promptDef.expectType === 'string') {
return 'input';
}
if (promptDef.expectType === 'password') {
return 'password';
}
if (promptDef.expectType === 'json') {
return 'input';
}
throw new Error('Unexpected `expectType` for prompt: '+promptDef.expectType);
})(),
validate: function _isTruthy(value){
var parsedValue;
// Value is truthy
if (value) {
if (promptDef.expectType === 'json'){
try {
// TODO: use `rttc.parseHuman()` here
parsedValue = JSON.parse(value);
// Don't allow null
if (_.isNull(parsedValue)) {
return '`null` is not allowed, sorry';
}
// Allow booleans, strings, numbers, objects, arrays
return true;
}
catch (e){}
return 'enter valid JSON (don\'t forget double quotes!)';
}
return true;
}
// Value is falsy
else {
if (promptDef.example) {
return 'e.g. '+promptDef.example;
}
return false;
}
}
};
// if (promptDef.example){
// inquirerPromptDef.default = promptDef.example;
// }
memo.push(inquirerPromptDef);
return memo;
}, []);
}
catch (e) {
return exits.error(e);
}
inquirer.prompt(promptArray, function (answers) {
var resultArray = [];
try {
_.each(answers, function (answer, key){
resultArray.push({
name: key,
value: answer
});
});
}
catch (e) {
return exits.error(e);
}
return exits.success(resultArray);
});
}
};