forked from ljacobsson/evb-cli
/
input-util.js
237 lines (214 loc) · 5.74 KB
/
input-util.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// date-prompt fails if stdout doesn't isn't a window context
if (!process.stdout.getWindowSize) {
process.stdout.getWindowSize = function () {
return [10, 10];
};
}
const inquirer = require("inquirer");
const prompt = inquirer.createPromptModule();
const datePrompt = require("date-prompt");
const BACK = "↩ back";
const UNDO = "⎌ undo";
const DONE = "✔ done";
const backNavigation = [BACK, new inquirer.Separator("-------------")];
const doneNavigation = [DONE, UNDO, new inquirer.Separator("-------------")];
const filterRules = [
"equals",
"prefix",
"anything-but",
"numeric",
"exists",
"null",
];
const numericOperators = [">", "<", "=", ">=", "<=", "!=", "range"];
async function text(message, def) {
const response = await prompt({
name: "id",
type: "input",
message: message,
default: def,
});
return response.id;
}
async function getStringValue(fieldName, type) {
const rules = JSON.parse(JSON.stringify(filterRules));
const rule = await prompt({
name: "id",
type: "list",
message: `Enter rule for ${fieldName} matching`,
choices: [...rules],
});
let val = undefined;
if (rule.id !== "exists" && rule.id !== "numeric") {
const value = await prompt({
name: "id",
type: "input",
message: `Enter value for ${fieldName}. Comma separate for array`,
});
val = value.id.includes(",")
? value.id.split(",").map((p) => p.trim())
: value.id;
} else if (rule.id === "exists") {
val = true;
} else if (rule.id === "numeric") {
const operator = await prompt({
name: "id",
type: "list",
message: `Select operator`,
choices: numericOperators,
});
if (operator.id === "range") {
const lower = await prompt({
name: "id",
type: "input",
message: `Lower bound for ${fieldName}`,
});
const upper = await prompt({
name: "id",
type: "input",
message: `Upper bound for ${fieldName}`,
});
val = [">=", parseFloat(lower.id), "<", parseFloat(upper.id)];
} else {
const value = await prompt({
name: "id",
type: "input",
message: `Enter value for ${fieldName}`,
});
val = [operator.id, parseFloat(value.id)];
}
}
let returnObj = {};
let ruleObj = rule.id === "equals" ? val : undefined;
if (!ruleObj) {
ruleObj = {};
ruleObj[rule.id] = val;
}
if (!Array.isArray(ruleObj)) {
returnObj[fieldName] = [];
returnObj[fieldName].push(ruleObj);
} else {
returnObj[fieldName] = ruleObj;
}
return returnObj;
}
async function getProperty(currentObject, objectArray) {
let fieldList = Object.keys(currentObject.properties);
const property = await prompt({
name: "id",
type: "list",
message: `Add ${
objectArray[objectArray.length - 1] ||
currentObject["x-amazon-events-detail-type"]
} item`,
choices: [
...(objectArray.length ? backNavigation : doneNavigation),
...fieldList,
],
});
objectArray.push(property.id);
const chosenProp = currentObject.properties[property.id];
return { property, chosenProp };
}
async function getDetailTypeName(schemas, sourceName) {
const detailTypes = schemas
.filter((p) => p.SchemaName.startsWith(`${sourceName}@`))
.map((p) => p.SchemaName.split("@")[1]);
const detailType = await prompt({
name: "id",
type: "list",
message: "Select detail-type",
choices: detailTypes,
});
const detailTypeName = detailType.id;
return detailTypeName;
}
async function getSourceName(schemas) {
const sources = [...new Set(schemas.map((p) => p.SchemaName.split("@")[0]))];
const source = await prompt({
name: "id",
type: "list",
message: "Select source",
choices: sources,
});
const sourceName = source.id;
return sourceName;
}
async function getRegistry(schemas) {
const registriesResponse = await schemas.listRegistries().promise();
const registries = [
...new Set(registriesResponse.Registries.map((p) => p.RegistryName)),
];
const registry = await prompt({
name: "id",
type: "list",
message: "Select registry",
choices: registries,
});
return registry;
}
async function selectFrom(list, message, skipBack) {
const answer = await prompt({
name: "id",
type: "list",
message: message || "Please select",
choices: [!skipBack ? BACK : null, ...list].filter((p) => p),
});
return answer.id;
}
async function multiSelectFrom(list, message, skipBack) {
const answer = await prompt({
name: "id",
type: "checkbox",
message: message || "Please select",
choices: [!skipBack ? BACK : null, ...list].filter((p) => p),
});
return answer.id;
}
async function getEventBusName(eventbridge) {
const eventBusesResponse = await eventbridge.listEventBuses().promise();
let eventBuses = [
...new Set(eventBusesResponse.EventBuses.map((p) => p.Name)),
];
if (process.env.EVB_DEMO_MODE) {
eventBuses= ["default", "examplebus"];
}
const eventBusName = await prompt({
name: "id",
type: "list",
message: "Select eventbus",
choices: eventBuses,
});
return eventBusName.id;
}
async function getPropertyValue(chosenProp, property) {
let answer = undefined;
switch (chosenProp.type) {
case "string":
answer = await getStringValue(property.id, chosenProp.type);
break;
// placeholder for dateselector, etc
default:
answer = await getStringValue(property.id, chosenProp.type);
}
return answer;
}
async function getDate(message) {
const answer = await datePrompt(message);
return answer;
}
module.exports = {
getEventBusName,
getRegistry,
getSourceName,
getDetailTypeName,
selectFrom,
multiSelectFrom,
getProperty,
getPropertyValue,
text,
getDate,
BACK,
DONE,
UNDO,
};