Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-plop): bypass list prompts with choices being a function #372

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-planets-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"node-plop": minor
---

Ability to bypass choices prompts using a choices function
11 changes: 8 additions & 3 deletions packages/node-plop/src/prompt-bypass.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ const flag = {

// generic list bypass function. used for all types of lists.
// accepts value, index, or key as matching criteria
const listTypeBypass = (v, prompt) => {
const choice = prompt.choices.find((c, idx) => choiceMatchesValue(c, idx, v));
const listTypeBypass = (v, prompt, answers) => {
let choicesArray = prompt.choices;
if (typeof prompt.choices === 'function') {
choicesArray = prompt.choices(answers);
}

const choice = choicesArray.find((c, idx) => choiceMatchesValue(c, idx, v));
if (choice != null) {
return getChoiceValue(choice);
}
Expand Down Expand Up @@ -166,7 +171,7 @@ export default async function (prompts, bypassArr, plop) {
// get the real answer data out of the bypass function and attach it
// to the answer data object
const bypassIsFunc = typeof bypass === "function";
const value = bypassIsFunc ? bypass.call(null, val, p) : val;
const value = bypassIsFunc ? bypass.call(null, val, p, answers) : val;

// if inquirer prompt has a filter function - call it
const answer = p.filter ? p.filter(value, answers) : value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ describe("prompt-bypass-list", function () {
},
];

const dynamicPrompts = [
{
type: "list",
name: "country",
message: "Country",
choices: [
{ key: "UK", value: "United Kingdom" },
'US'
]
},
{
type: "list",
name: "city",
message: "City",
choices: ({ country }) => {
if (country === "United Kingdom") {
return [
"London",
"Manchested"
]
} else if (country === "US") {
return [
"New York",
"Dallas"
]
} else {
return [];
}
}
}
]

test("verify good bypass input", async function () {
const [, byValue] = await promptBypass(prompts, ["eh"], plop);
expect(byValue.list).toBe("eh");
Expand Down Expand Up @@ -69,6 +101,14 @@ describe("prompt-bypass-list", function () {

const [, byNameObject] = await promptBypass(prompts, "ff", plop);
expect(byNameObject.list).toEqual({ prop: "value" });

const [, byNameDynamic] = await promptBypass(dynamicPrompts, ["UK", "London"], plop);
expect(byNameDynamic.city).toBe("London");
expect(byNameDynamic.country).toBe("United Kingdom");

const [, byIndexNumberDynamic] = await promptBypass(dynamicPrompts, [1, 1], plop);
expect(byIndexNumberDynamic.city).toBe("Dallas");
expect(byIndexNumberDynamic.country).toBe("US");
});

test("verify bad bypass input", async function () {
Expand All @@ -81,5 +121,11 @@ describe("prompt-bypass-list", function () {
await expect(() =>
promptBypass(prompts, [6], { is: plop })
).rejects.toThrow();
await expect(() =>
promptBypass(dynamicPrompts, ["UK", "Dallas"], { is: plop })
).rejects.toThrow();
await expect(() =>
promptBypass(dynamicPrompts, ["asdf", "asdf"], { is: plop })
).rejects.toThrow();
});
});