1
|
|
|
import { resolve } from 'bluebird'; |
2
|
|
|
import { |
3
|
|
|
__, |
4
|
|
|
assoc, |
5
|
|
|
concat, |
6
|
|
|
curry, |
7
|
|
|
has, |
8
|
|
|
keys, |
9
|
|
|
map, |
10
|
|
|
merge, |
11
|
|
|
reduce, |
12
|
|
|
toPairs |
13
|
|
|
} from 'ramda'; |
14
|
|
|
import { green, red, yellow } from 'colors/safe'; |
15
|
|
|
import { createPromptModule } from 'inquirer'; |
16
|
|
|
import DatePickerPrompt from 'inquirer-datepicker-prompt'; |
17
|
|
|
import ChalkPipe from 'inquirer-chalk-pipe'; |
18
|
|
|
import { validator, filter } from './types'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Emits a warning to stdout |
22
|
|
|
* |
23
|
|
|
* @param {String} message |
24
|
|
|
* @return {Promise} |
25
|
|
|
*/ |
26
|
|
|
export const emitWarning = concat(' ⚠ Warning: ') & yellow & console.log & resolve; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Emits an error to stdout |
30
|
|
|
* |
31
|
|
|
* @param {String} message |
32
|
|
|
* @return {Promise} |
33
|
|
|
*/ |
34
|
|
|
export const emitError = concat(' ✗ Error: ') & red & console.log & resolve; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Emits a success message |
38
|
|
|
* |
39
|
|
|
* @param {String} message |
40
|
|
|
* @return {Promise} |
41
|
|
|
*/ |
42
|
|
|
export const emitSuccess = concat(' ✔ Success: ') & green & console.log & resolve; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Renames the keys of an object |
46
|
|
|
* |
47
|
|
|
* @sig {a: b} -> {a: *} -> {b: *} |
48
|
|
|
*/ |
49
|
|
|
const renameKeys = curry((keysMap, obj) => reduce((acc, key) => |
50
|
|
|
assoc(keysMap[key] || key, obj[key], acc), {}, keys(obj))); |
51
|
|
|
|
52
|
|
|
const components = { |
53
|
|
|
Calendar: ~({ |
54
|
|
|
type: 'datetime', |
55
|
|
|
format: ['m', '/', 'd', '/', 'yy'], |
56
|
|
|
filter: filter.Calendar }), |
57
|
|
|
Char: ({ type }) => ({ filter: filter.Char(type.length) }), |
58
|
|
|
Checkbox: ~({ type: 'confirm' }), |
59
|
|
|
Color: ~({ type: 'chalk-pipe' }), |
60
|
|
|
DoubleRange: ({ type }) => ({ |
61
|
|
|
filter: filter.Double, |
62
|
|
|
validate: validator.Range(type.from, type.to) }), |
63
|
|
|
DateTime: ~({ type: 'datetime' }), |
64
|
|
|
Double: ~({ validate: validator.Double, filter: filter.Double }), |
65
|
|
|
Email: ~({ validate: validator.Email }), |
66
|
|
|
Integer: ~({ validate: validator.Integer, filter: filter.Integer }), |
67
|
|
|
IntegerRange: ({ type }) => ({ |
68
|
|
|
filter: filter.Integer, |
69
|
|
|
validate: validator.Range(type.from, type.to) }), |
70
|
|
|
IntegerMultiRange: ({ type }) => ({ |
71
|
|
|
filter: filter.IntegerMultiRange, |
72
|
|
|
validate: validator.IntegerMultiRange(type.from, type.to) }), |
73
|
|
|
Natural: ~({ validate: validator.Natural, filter: filter.Integer }), |
74
|
|
|
OneOf: ({ type }) => ({ type: 'list', choices: type.values }), |
75
|
|
|
String: ~({ type: 'input' }), |
76
|
|
|
Url: ~({ validate: validator.Url }), |
77
|
|
|
Money: ~({ validate: validator.Money, filter: filter.Money }) |
78
|
|
|
}; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Converts a Rung CLI question object to an Inquirer question object |
82
|
|
|
* |
83
|
|
|
* @author Marcelo Haskell Camargo |
84
|
|
|
* @param {String} name |
|
|
|
|
85
|
|
|
* @param {Object} config |
|
|
|
|
86
|
|
|
* @return {Object} |
87
|
|
|
*/ |
88
|
|
|
function toInquirerQuestion([name, config]) { |
89
|
|
|
const component = has(config.type.name, components) |
90
|
|
|
? components[config.type.name] |
91
|
|
|
: components.String; |
92
|
|
|
|
93
|
|
|
return merge(config |
94
|
|
|
| renameKeys({ description: 'message' }) |
95
|
|
|
| merge(__, { name }), component(config)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the pure JS values from received questions that will be answered |
100
|
|
|
* |
101
|
|
|
* @author Marcelo Haskell Camargo |
102
|
|
|
* @param {Object} questions |
103
|
|
|
* @return {Promise} answers for the questions by key |
104
|
|
|
*/ |
105
|
|
|
export function ask(questions) { |
106
|
|
|
const prompt = createPromptModule(); |
107
|
|
|
prompt.registerPrompt('datetime', DatePickerPrompt); |
108
|
|
|
prompt.registerPrompt('chalk-pipe', ChalkPipe); |
109
|
|
|
return resolve(prompt(questions | toPairs | map(toInquirerQuestion))); |
110
|
|
|
} |
111
|
|
|
|