Skip to content

Commit

Permalink
feat(@angular/cli): initialize a console prompt provider for schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and vikerman committed Aug 29, 2018
1 parent 516f52e commit 489f0e9
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -69,6 +69,7 @@
"@types/copy-webpack-plugin": "^4.4.1",
"@types/express": "^4.16.0",
"@types/glob": "^5.0.35",
"@types/inquirer": "^0.0.42",
"@types/istanbul": "^0.4.30",
"@types/jasmine": "^2.8.8",
"@types/loader-utils": "^1.1.3",
Expand Down
10 changes: 9 additions & 1 deletion packages/angular/cli/commands/generate-impl.ts
Expand Up @@ -52,10 +52,11 @@ export class GenerateCommand extends SchematicCommand {
return this.runSchematic({
collectionName,
schematicName,
schematicOptions: options,
schematicOptions: this.removeLocalOptions(options),
debug: options.debug,
dryRun: options.dryRun,
force: options.force,
interactive: options.interactive,
});
}

Expand Down Expand Up @@ -95,4 +96,11 @@ export class GenerateCommand extends SchematicCommand {
this.logger.info(terminal.cyan(` ng generate <schematic> --help`));
}
}

private removeLocalOptions(options: any): any {
const opts = Object.assign({}, options);
delete opts.interactive;

return opts;
}
}
5 changes: 5 additions & 0 deletions packages/angular/cli/commands/generate.json
Expand Up @@ -30,6 +30,11 @@
"default": false,
"aliases": ["f"],
"description": "Forces overwriting of files."
},
"interactive": {
"type": "boolean",
"default": "true",
"description": "Disables interactive inputs (i.e., prompts)."
}
},
"required": [
Expand Down
6 changes: 3 additions & 3 deletions packages/angular/cli/commands/new-impl.ts
Expand Up @@ -50,15 +50,14 @@ export class NewCommand extends SchematicCommand {
// Ensure skipGit has a boolean value.
options.skipGit = options.skipGit === undefined ? false : options.skipGit;

options = this.removeLocalOptions(options);

return this.runSchematic({
collectionName: collectionName,
schematicName: this.schematicName,
schematicOptions: options,
schematicOptions: this.removeLocalOptions(options),
debug: options.debug,
dryRun: options.dryRun,
force: options.force,
interactive: options.interactive,
});
}

Expand All @@ -72,6 +71,7 @@ export class NewCommand extends SchematicCommand {
const opts = Object.assign({}, options);
delete opts.verbose;
delete opts.collection;
delete opts.interactive;

return opts;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/angular/cli/commands/new.json
Expand Up @@ -33,6 +33,11 @@
"default": false,
"aliases": ["v"],
"description": "Adds more details to output logging."
},
"interactive": {
"type": "boolean",
"default": "true",
"description": "Disables interactive inputs (i.e., prompts)."
}
},
"required": []
Expand Down
45 changes: 45 additions & 0 deletions packages/angular/cli/models/schematic-command.ts
Expand Up @@ -37,6 +37,7 @@ import {
NodeWorkflow,
validateOptionsWithSchema,
} from '@angular-devkit/schematics/tools';
import * as inquirer from 'inquirer';
import { take } from 'rxjs/operators';
import { WorkspaceLoader } from '../models/workspace-loader';
import {
Expand All @@ -60,6 +61,7 @@ export interface RunSchematicOptions {
dryRun: boolean;
force: boolean;
showNothingDone?: boolean;
interactive?: boolean;
}

export interface GetOptionsOptions {
Expand Down Expand Up @@ -279,6 +281,49 @@ export abstract class SchematicCommand extends Command {
return undefined;
});

if (options.interactive !== false && process.stdout.isTTY) {
workflow.registry.usePromptProvider((definitions: Array<schema.PromptDefinition>) => {
const questions: inquirer.Questions = definitions.map(definition => {
const question: inquirer.Question = {
name: definition.id,
message: definition.message,
default: definition.default,
};

const validator = definition.validator;
if (validator) {
question.validate = input => validator(input);
}

switch (definition.type) {
case 'confirmation':
question.type = 'confirm';
break;
case 'list':
question.type = 'list';
question.choices = definition.items && definition.items.map(item => {
if (typeof item == 'string') {
return item;
} else {
return {
name: item.label,
value: item.value,
};
}
});
break;
default:
question.type = definition.type;
break;
}

return question;
});

return inquirer.prompt(questions);
});
}

workflow.reporter.subscribe((event: DryRunEvent) => {
nothingDone = false;

Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/package.json
Expand Up @@ -35,9 +35,9 @@
"@angular-devkit/schematics": "0.0.0",
"@schematics/angular": "0.0.0",
"@schematics/update": "0.0.0",
"inquirer": "^6.1.0",
"json-schema-traverse": "^0.4.1",
"opn": "^5.3.0",
"json-schema-traverse": "^0.4.1",
"rxjs": "^6.0.0",
"semver": "^5.1.0",
"symbol-observable": "^1.2.0",
Expand Down

0 comments on commit 489f0e9

Please sign in to comment.