Skip to content

Commit 489f0e9

Browse files
clydinvikerman
authored andcommitted
feat(@angular/cli): initialize a console prompt provider for schematics
1 parent 516f52e commit 489f0e9

File tree

8 files changed

+255
-11
lines changed

8 files changed

+255
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@types/copy-webpack-plugin": "^4.4.1",
7070
"@types/express": "^4.16.0",
7171
"@types/glob": "^5.0.35",
72+
"@types/inquirer": "^0.0.42",
7273
"@types/istanbul": "^0.4.30",
7374
"@types/jasmine": "^2.8.8",
7475
"@types/loader-utils": "^1.1.3",

packages/angular/cli/commands/generate-impl.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ export class GenerateCommand extends SchematicCommand {
5252
return this.runSchematic({
5353
collectionName,
5454
schematicName,
55-
schematicOptions: options,
55+
schematicOptions: this.removeLocalOptions(options),
5656
debug: options.debug,
5757
dryRun: options.dryRun,
5858
force: options.force,
59+
interactive: options.interactive,
5960
});
6061
}
6162

@@ -95,4 +96,11 @@ export class GenerateCommand extends SchematicCommand {
9596
this.logger.info(terminal.cyan(` ng generate <schematic> --help`));
9697
}
9798
}
99+
100+
private removeLocalOptions(options: any): any {
101+
const opts = Object.assign({}, options);
102+
delete opts.interactive;
103+
104+
return opts;
105+
}
98106
}

packages/angular/cli/commands/generate.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"default": false,
3131
"aliases": ["f"],
3232
"description": "Forces overwriting of files."
33+
},
34+
"interactive": {
35+
"type": "boolean",
36+
"default": "true",
37+
"description": "Disables interactive inputs (i.e., prompts)."
3338
}
3439
},
3540
"required": [

packages/angular/cli/commands/new-impl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ export class NewCommand extends SchematicCommand {
5050
// Ensure skipGit has a boolean value.
5151
options.skipGit = options.skipGit === undefined ? false : options.skipGit;
5252

53-
options = this.removeLocalOptions(options);
54-
5553
return this.runSchematic({
5654
collectionName: collectionName,
5755
schematicName: this.schematicName,
58-
schematicOptions: options,
56+
schematicOptions: this.removeLocalOptions(options),
5957
debug: options.debug,
6058
dryRun: options.dryRun,
6159
force: options.force,
60+
interactive: options.interactive,
6261
});
6362
}
6463

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

7676
return opts;
7777
}

packages/angular/cli/commands/new.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
"default": false,
3434
"aliases": ["v"],
3535
"description": "Adds more details to output logging."
36+
},
37+
"interactive": {
38+
"type": "boolean",
39+
"default": "true",
40+
"description": "Disables interactive inputs (i.e., prompts)."
3641
}
3742
},
3843
"required": []

packages/angular/cli/models/schematic-command.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
NodeWorkflow,
3838
validateOptionsWithSchema,
3939
} from '@angular-devkit/schematics/tools';
40+
import * as inquirer from 'inquirer';
4041
import { take } from 'rxjs/operators';
4142
import { WorkspaceLoader } from '../models/workspace-loader';
4243
import {
@@ -60,6 +61,7 @@ export interface RunSchematicOptions {
6061
dryRun: boolean;
6162
force: boolean;
6263
showNothingDone?: boolean;
64+
interactive?: boolean;
6365
}
6466

6567
export interface GetOptionsOptions {
@@ -279,6 +281,49 @@ export abstract class SchematicCommand extends Command {
279281
return undefined;
280282
});
281283

284+
if (options.interactive !== false && process.stdout.isTTY) {
285+
workflow.registry.usePromptProvider((definitions: Array<schema.PromptDefinition>) => {
286+
const questions: inquirer.Questions = definitions.map(definition => {
287+
const question: inquirer.Question = {
288+
name: definition.id,
289+
message: definition.message,
290+
default: definition.default,
291+
};
292+
293+
const validator = definition.validator;
294+
if (validator) {
295+
question.validate = input => validator(input);
296+
}
297+
298+
switch (definition.type) {
299+
case 'confirmation':
300+
question.type = 'confirm';
301+
break;
302+
case 'list':
303+
question.type = 'list';
304+
question.choices = definition.items && definition.items.map(item => {
305+
if (typeof item == 'string') {
306+
return item;
307+
} else {
308+
return {
309+
name: item.label,
310+
value: item.value,
311+
};
312+
}
313+
});
314+
break;
315+
default:
316+
question.type = definition.type;
317+
break;
318+
}
319+
320+
return question;
321+
});
322+
323+
return inquirer.prompt(questions);
324+
});
325+
}
326+
282327
workflow.reporter.subscribe((event: DryRunEvent) => {
283328
nothingDone = false;
284329

packages/angular/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
"@angular-devkit/schematics": "0.0.0",
3636
"@schematics/angular": "0.0.0",
3737
"@schematics/update": "0.0.0",
38+
"inquirer": "^6.1.0",
3839
"json-schema-traverse": "^0.4.1",
3940
"opn": "^5.3.0",
40-
"json-schema-traverse": "^0.4.1",
4141
"rxjs": "^6.0.0",
4242
"semver": "^5.1.0",
4343
"symbol-observable": "^1.2.0",

0 commit comments

Comments
 (0)