Skip to content

Commit

Permalink
Fixed issue with npm initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jul 5, 2022
1 parent 19ccbc0 commit 07b3225
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Piral Changelog

## 0.14.29 (tbd)

- Fixed issue with npm initializer using CLI aliases
- Added support for aliases in the npm initializers

## 0.14.28 (July 1, 2022)

- Improved download for templates falling back to default registry
Expand Down
18 changes: 15 additions & 3 deletions src/tooling/piral-cli/src/questionnaire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type FlagType = 'string' | 'number' | 'boolean' | 'object';
interface Flag {
name: string;
type?: FlagType;
alias: Array<string>;
values?: Array<any>;
describe?: string;
default?: any;
Expand All @@ -16,16 +17,23 @@ interface Flag {
function getCommandData(retrieve: any) {
const instructions: Array<Flag> = [];
const fn = {
alias(name: string, altName: string) {
return this.swap(name, (flag) => ({
...flag,
alias: [...flag.alias, altName],
}));
},
positional(name: string, info: Flag) {
instructions.push({
...info,
alias: [],
name,
});
return this;
},
swap(name: string, swapper: (flag: Flag) => Flag) {
const [flag] = instructions.filter((m) => m.name === name);
const newFlag = swapper(flag || { name });
const newFlag = swapper(flag || { name, alias: [] });

if (!flag) {
instructions.push(newFlag);
Expand Down Expand Up @@ -135,7 +143,7 @@ export function runQuestionnaireFor(
const questions = instructions
.filter((instruction) => !ignored.includes(instruction.name))
.filter((instruction) => !acceptAll || (instruction.default === undefined && instruction.required))
.filter((instruction) => args[instruction.name] === undefined)
.filter((instruction) => [...instruction.alias, instruction.name].every((m) => args[m] === undefined))
.filter((instruction) => instruction.type !== 'object')
.map((instruction) => ({
name: instruction.name,
Expand All @@ -146,12 +154,16 @@ export function runQuestionnaireFor(
validate: instruction.type === 'number' ? (input: string) => !isNaN(+input) : () => true,
}));


return inquirer.prompt(questions).then((answers) => {
const parameters: any = {};

for (const instruction of instructions) {
const name = instruction.name;
const value = answers[name] ?? ignoredInstructions[name] ?? args[name];
const value =
answers[name] ??
ignoredInstructions[name] ??
[...instruction.alias, instruction.name].map((m) => args[m]).find((v) => v !== undefined);
parameters[name] = value !== undefined ? getValue(instruction.type, value as any) : instruction.default;
}

Expand Down

0 comments on commit 07b3225

Please sign in to comment.