From 820c221107b6d1af7a2790a322c50b769ca72e53 Mon Sep 17 00:00:00 2001 From: Magnus Johansson Date: Wed, 22 Mar 2023 20:54:05 +0100 Subject: [PATCH 1/3] CLI bypass for choices that is a function --- packages/node-plop/src/prompt-bypass.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/node-plop/src/prompt-bypass.js b/packages/node-plop/src/prompt-bypass.js index cc49f92..4d63e8d 100644 --- a/packages/node-plop/src/prompt-bypass.js +++ b/packages/node-plop/src/prompt-bypass.js @@ -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); } @@ -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; From bbb596f36b8ca43de0eb350b3717d0e7e8313bc4 Mon Sep 17 00:00:00 2001 From: Magnus Johansson Date: Wed, 22 Mar 2023 21:02:21 +0100 Subject: [PATCH 2/3] Adding changeset --- .changeset/small-planets-agree.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/small-planets-agree.md diff --git a/.changeset/small-planets-agree.md b/.changeset/small-planets-agree.md new file mode 100644 index 0000000..69c19b7 --- /dev/null +++ b/.changeset/small-planets-agree.md @@ -0,0 +1,5 @@ +--- +"node-plop": minor +--- + +Ability to bypass choices prompts using a choices function From 56d0f13ae334252346966123b2c157db8f724879 Mon Sep 17 00:00:00 2001 From: Magnus Johansson Date: Wed, 22 Mar 2023 21:13:48 +0100 Subject: [PATCH 3/3] Adding unit tests --- .../prompt-bypass-list.spec.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/node-plop/tests/prompt-bypass-list/prompt-bypass-list.spec.js b/packages/node-plop/tests/prompt-bypass-list/prompt-bypass-list.spec.js index e3b5ff9..4eec4ff 100644 --- a/packages/node-plop/tests/prompt-bypass-list/prompt-bypass-list.spec.js +++ b/packages/node-plop/tests/prompt-bypass-list/prompt-bypass-list.spec.js @@ -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"); @@ -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 () { @@ -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(); }); });