Skip to content

Commit

Permalink
Adding undo, numeric and template inject features
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Aug 22, 2020
1 parent 5008ec2 commit 63661e5
Show file tree
Hide file tree
Showing 5 changed files with 640 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/code-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const AWS = require("aws-sdk");
const schemas = new AWS.Schemas();
const inputUtil = require("./input-util");
const languages = ["Java8", "Python36", "TypeScript3"];
const fs = require("fs");
const inquirer = require("inquirer");
const prompt = inquirer.createPromptModule();

async function browse(l) {
const registry = await inputUtil.getRegistry(schemas);
const schemaResponse = await schemas
.listSchemas({ RegistryName: registry.id })
.promise();

const sourceName = await inputUtil.getSourceName(schemaResponse);
const detailTypeName = await inputUtil.getDetailTypeName(
schemaResponse,
sourceName
);
const schemaName = `${sourceName}@${detailTypeName}`;

const language = await prompt({
name: "id",
type: "list",
message: `Select language`,
choices: languages
});

// const source = await schemas.putCodeBinding({Language: language.id, RegistryName: registry.id, SchemaName: schemaName}).promise();
// console.log(source.$response.data);

const binding = await schemas.getCodeBindingSource({ Language: language.id, SchemaName:schemaName, RegistryName: registry.id}).promise();
fs.writeFileSync("binding.zip", binding.Body);
}
module.exports = {
browse
};
211 changes: 211 additions & 0 deletions src/input-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
const inquirer = require("inquirer");
const prompt = inquirer.createPromptModule();
const BACK = "↩ back";
const UNDO = "⎌ undo";
const DONE = "✔ done";
const CONSUME_LOCALLY = "⚡ consume locally";
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.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.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 getEventBusName(eventbridge) {
const eventBusesResponse = await eventbridge.listEventBuses().promise();
const eventBuses = [
...new Set(eventBusesResponse.EventBuses.map((p) => p.Name)),
];
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;
}

module.exports = {
getEventBusName,
getRegistry,
getSourceName,
getDetailTypeName,
selectFrom,
getProperty,
getPropertyValue,
text,
BACK,
DONE,
UNDO,
CONSUME_LOCALLY,
};
Loading

0 comments on commit 63661e5

Please sign in to comment.