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

prompt support for value type number #12927

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 15 additions & 1 deletion packages/angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export abstract class SchematicCommand<

if (options.interactive !== false && process.stdout.isTTY) {
workflow.registry.usePromptProvider((definitions: Array<schema.PromptDefinition>) => {
const numberQuestions: string[] = [];
const questions: inquirer.Questions = definitions.map(definition => {
const question: inquirer.Question = {
name: definition.id,
Expand Down Expand Up @@ -321,6 +322,10 @@ export abstract class SchematicCommand<
}
});
break;
case 'number':
numberQuestions.push(definition.id);
question.type = 'number';
break;
default:
question.type = definition.type;
break;
Expand All @@ -329,7 +334,16 @@ export abstract class SchematicCommand<
return question;
});

return inquirer.prompt(questions);
return inquirer.prompt(questions).then((answers) => {
Object.entries(answers).forEach(([name, answer]) => {
if (numberQuestions.indexOf(name) > -1) {
const parsed = parseInt(answer);
answers[name] = isNaN(parsed) ? '' : parsed;
}
});

return answers;
});
});
}

Expand Down
9 changes: 8 additions & 1 deletion packages/angular_devkit/core/src/json/schema/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ export class CoreSchemaRegistry implements SchemaRegistry {
type = 'confirmation';
} else if (Array.isArray(parentSchema.enum)) {
type = 'list';
} else if (parentSchema.type === 'number') {
type = 'number';
} else {
type = 'input';
}
Expand Down Expand Up @@ -570,7 +572,12 @@ export class CoreSchemaRegistry implements SchemaRegistry {
items,
default: typeof parentSchema.default == 'object' ? undefined : parentSchema.default,
async validator(data: string) {
const result = it.self.validate(parentSchema, data);
let convertedData: string | number = data;
if (parentSchema.type === 'number') {
const parsed = parseInt(data);
convertedData = isNaN(parsed) ? '' : parsed;
}
const result = it.self.validate(parentSchema, convertedData);
if (typeof result === 'boolean') {
return result;
} else {
Expand Down