Skip to content

Commit

Permalink
Fix for hyphens in JSONPath and some other aestethic tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Apr 6, 2021
1 parent 05065ea commit 54f235e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sam-patterns-cli",
"version": "0.0.8",
"version": "0.0.9",
"description": "Command line interface for quickly using patterns from https://github.com/aws-samples/serverless-patterns/",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/import/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function run(cmd) {

const patterns = await githubUtil.getPatterns();

const pattern = await inputUtil.list("Select pattern", patterns);
const pattern = await inputUtil.autocomplete("Select pattern", patterns);
let templateString;
for (const fileName of pattern.setting.fileNames) {
try {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/import/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function propertyTransforms(template) {
const metadata = template.Metadata;
for (const property of metadata.PatternTransform.Properties || []) {
try {

if (!jp.query(template, property.JSONPath).length) continue;
let defaultValue, value;
switch (property.InputType) {
Expand All @@ -54,7 +55,7 @@ async function propertyTransforms(template) {
case "runtime-select":
value =
process.env.SAM_PATTERNS_DEFAULT_RUNTIME ||
(await inputUtil.list(
(await inputUtil.autocomplete(
`Select Lambda runtime for ${JSONPathToFrieldlyName(
property.JSONPath
)}.`,
Expand Down
12 changes: 12 additions & 0 deletions src/commands/import/transformer.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const transformer = require("./transformer");
const inquirer = require("inquirer");
const jp = require("jsonpath");
test("Fix dashed field name", async () => {
const obj = {
inner: {
"detail-type": "abc",
},
};

const value = jp.query(obj, '$["detail-type"]');

console.log("value", value);
});

test("Test transform", async () => {
inquirer.prompt = (questions) => Promise.resolve({ text: "test" });
Expand Down
27 changes: 20 additions & 7 deletions src/commands/share/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function run(cmd) {
string += word;
list.push(string);
}
const dynamic = await inputUtil.list(
const dynamic = await inputUtil.autocomplete(
`Select dynamic value for ${resource}`,
list
);
Expand Down Expand Up @@ -94,14 +94,14 @@ async function run(cmd) {
do {
const flattened = flatten(sharedTemplate);
const paths = yamleize(flattened, customizables);
item = await inputUtil.list2("Select item to modify", [
item = await inputUtil.list("Select item to modify", [
...paths,
new Separator("---"),
"Done",
new Separator("---"),
]);
if (item === "Done") break;
const action = await inputUtil.list("Select action", [
const action = await inputUtil.autocomplete("Select action", [
"Set default value",
"Make customisable",
"Delete",
Expand All @@ -113,13 +113,22 @@ async function run(cmd) {
const message = await inputUtil.text("Prompt message");
customizables.push(item.path);
metadata.PatternTransform.Properties.push({
JSONPath: "$." + item,
JSONPath:
"$." +
item.path
.split(".")
.map((p) => (p.includes("-") ? `["${p}"]` : p))
.join(".")
.replace(/\.\[/g, "["),
Message: message,
InputType: typeof flattened[item.path],
});
}
if (action === "Set default value") {
flattened[item.path] = await inputUtil.text("Set new value", flattened[item.path]);
flattened[item.path] = await inputUtil.text(
"Set new value",
flattened[item.path]
);
}
sharedTemplate = unflatten(flattened);
} while (true);
Expand All @@ -144,7 +153,7 @@ async function run(cmd) {
);

const sources = settingsUtil.get();
const repo = await inputUtil.list(
const repo = await inputUtil.autocomplete(
"Select repository",
sources.map((p) => {
return { name: `${p.owner}/${p.repo}`, value: p };
Expand Down Expand Up @@ -208,7 +217,11 @@ function yamleize(flattened, customizables) {
}
list.push({
name:
" ".repeat(split.length - 1) + propertyName + ": " + flattened[row] + (customizables.includes(row) ? " [✎ ]" : ""),
" ".repeat(split.length - 1) +
propertyName +
": " +
flattened[row] +
(customizables.includes(row) ? " [✎ ]" : ""),
value: { path: row, value: flattened[row] },
});
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/share/share.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const share = require("./share");
const inquirer = require("inquirer");

test("Test transform", async () => {
return;
await share.flattenAndIndent(template);
});

Expand Down
2 changes: 1 addition & 1 deletion src/commands/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function run(cmd) {
"Template filename(s):",
"template.yaml,template.yml"
)).split(",").map(p=>p.trim());
settings.url = await inputUtil.text("URL (use #PATTERN_NAME# as placeholder):", `https://github.com/${settings.owner}/${settings.repo}/tree/main/${settings.root}/#PATTERN_NAME#`);
settings.url = await inputUtil.text("URL (use #PATTERN_NAME# as placeholder):", `https://github.com/${settings.owner}/${settings.repo}/tree/main${settings.root}#PATTERN_NAME#`);
settingsUtil.save(settings);
}

Expand Down
15 changes: 8 additions & 7 deletions src/shared/inputUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ inquirer.registerPrompt(
require("inquirer-autocomplete-prompt")
);

async function choices(message, items, type, defaults) {
async function choices(message, items, type, defaults, pageSize = 5) {
return (
await inquirer.prompt({
type: type,
name: "item",
choices: items,
message: message,
default: defaults,
pageSize: 15,
pageSize: pageSize,
source: function (answersYet, input) {
if (!input) {
return items;
Expand All @@ -38,11 +38,12 @@ async function text(message, defaultValue) {
})
).text;
}
async function list(message, items) {
return await choices(message, items, "autocomplete");
async function autocomplete(message, items) {
return await choices(message, items, "autocomplete", null, 7);
}
async function list2(message, items) {
return await choices(message, items, "list");

async function list(message, items) {
return await choices(message, items, "list", null, 15);
}

async function checkbox(message, items, defaults) {
Expand All @@ -65,8 +66,8 @@ async function prompt(message) {
}

module.exports = {
autocomplete,
list,
list2,
checkbox,
text,
prompt,
Expand Down

0 comments on commit 54f235e

Please sign in to comment.