Skip to content

Commit de7ec84

Browse files
hanslalexeagle
authored andcommittedSep 6, 2018
feat(@angular/cli): add --helpJson (or --help-json) too all commands
And other refactors. The interface for the JSON is available in command.ts (the CommandDescription).
1 parent fd7bcd2 commit de7ec84

40 files changed

+998
-997
lines changed
 

‎packages/angular/cli/commands/add-impl.ts

+17-40
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,24 @@
99
// tslint:disable:no-global-tslint-disable no-any
1010
import { tags, terminal } from '@angular-devkit/core';
1111
import { NodePackageDoesNotSupportSchematics } from '@angular-devkit/schematics/tools';
12-
import { parseOptions } from '../models/command-runner';
13-
import { SchematicCommand } from '../models/schematic-command';
12+
import { Arguments } from '../models/interface';
13+
import { BaseSchematicOptions, SchematicCommand } from '../models/schematic-command';
1414
import { NpmInstall } from '../tasks/npm-install';
1515
import { getPackageManager } from '../utilities/config';
1616

17+
export interface AddCommandOptions extends BaseSchematicOptions {
18+
collection: string;
19+
help?: boolean;
20+
help_json?: boolean;
21+
}
1722

18-
export class AddCommand extends SchematicCommand {
23+
export class AddCommand<
24+
T extends AddCommandOptions = AddCommandOptions,
25+
> extends SchematicCommand<T> {
1926
readonly allowPrivateSchematics = true;
2027

21-
private async _parseSchematicOptions(collectionName: string): Promise<any> {
22-
const schematicOptions = await this.getOptions({
23-
schematicName: 'ng-add',
24-
collectionName,
25-
});
26-
this.addOptions(schematicOptions);
27-
28-
return parseOptions(this._rawArgs, this.options);
29-
}
30-
31-
validate(options: any) {
32-
const collectionName = options._[0];
33-
34-
if (!collectionName) {
35-
this.logger.fatal(
36-
`The "ng add" command requires a name argument to be specified eg. `
37-
+ `${terminal.yellow('ng add [name] ')}. For more details, use "ng help".`,
38-
);
39-
40-
return false;
41-
}
42-
43-
return true;
44-
}
45-
46-
async run(options: any) {
47-
const firstArg = options._[0];
48-
49-
if (!firstArg) {
28+
async run(options: AddCommandOptions & Arguments) {
29+
if (!options.collection) {
5030
this.logger.fatal(
5131
`The "ng add" command requires a name argument to be specified eg. `
5232
+ `${terminal.yellow('ng add [name] ')}. For more details, use "ng help".`,
@@ -59,16 +39,16 @@ export class AddCommand extends SchematicCommand {
5939

6040
const npmInstall: NpmInstall = require('../tasks/npm-install').default;
6141

62-
const packageName = firstArg.startsWith('@')
63-
? firstArg.split('/', 2).join('/')
64-
: firstArg.split('/', 1)[0];
42+
const packageName = options.collection.startsWith('@')
43+
? options.collection.split('/', 2).join('/')
44+
: options.collection.split('/', 1)[0];
6545

6646
// Remove the tag/version from the package name.
6747
const collectionName = (
6848
packageName.startsWith('@')
6949
? packageName.split('@', 2).join('@')
7050
: packageName.split('@', 1).join('@')
71-
) + firstArg.slice(packageName.length);
51+
) + options.collection.slice(packageName.length);
7252

7353
// We don't actually add the package to package.json, that would be the work of the package
7454
// itself.
@@ -79,11 +59,8 @@ export class AddCommand extends SchematicCommand {
7959
this.project.root,
8060
);
8161

82-
// Reparse the options with the new schematic accessible.
83-
options = await this._parseSchematicOptions(collectionName);
84-
8562
const runOptions = {
86-
schematicOptions: options,
63+
schematicOptions: options['--'] || [],
8764
workingDir: this.project.root,
8865
collectionName,
8966
schematicName: 'ng-add',
+19-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
{
22
"$schema": "http://json-schema.org/schema",
3-
"id": "AddCommandOptions",
3+
"$id": "ng-cli://commands/add.json",
44
"description": "Add support for a library to your project.",
55
"$longDescription": "",
66

77
"$scope": "in",
88
"$impl": "./add-impl#AddCommand",
99

1010
"type": "object",
11-
"properties": {
12-
"collection": {
13-
"type": "string",
14-
"description": "The package to be added.",
15-
"$default": {
16-
"$source": "argv",
17-
"index": 0
18-
}
11+
"allOf": [
12+
{
13+
"properties": {
14+
"collection": {
15+
"type": "string",
16+
"description": "The package to be added.",
17+
"$default": {
18+
"$source": "argv",
19+
"index": 0
20+
}
21+
}
22+
},
23+
"required": [
24+
]
25+
},
26+
{
27+
"$ref": "./definitions.json#/definitions/base"
1928
}
20-
},
21-
"required": [
2229
]
23-
}
30+
}

‎packages/angular/cli/commands/build-impl.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@ import { Version } from '../upgrade/version';
1212
export class BuildCommand extends ArchitectCommand {
1313
public readonly target = 'build';
1414

15-
public validate(options: ArchitectCommandOptions) {
15+
public async run(options: ArchitectCommandOptions) {
1616
// Check Angular and TypeScript versions.
1717
Version.assertCompatibleAngularVersion(this.project.root);
1818
Version.assertTypescriptVersion(this.project.root);
1919

20-
return super.validate(options);
21-
}
22-
23-
public async run(options: ArchitectCommandOptions) {
2420
return this.runArchitectTarget(options);
2521
}
2622
}
+5-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json-schema.org/schema",
3-
"id": "BuildCommandOptions",
3+
"$id": "ng-cli://commands/build.json",
44
"description": "Builds your app and places it into the output path (dist/ by default).",
55
"$longDescription": "",
66

@@ -9,26 +9,8 @@
99
"$type": "architect",
1010
"$impl": "./build-impl#BuildCommand",
1111

12-
"type": "object",
13-
"properties": {
14-
"project": {
15-
"type": "string",
16-
"description": "The name of the project to build.",
17-
"$default": {
18-
"$source": "argv",
19-
"index": 0
20-
}
21-
},
22-
"configuration": {
23-
"description": "Specify the configuration to use.",
24-
"type": "string",
25-
"aliases": ["c"]
26-
},
27-
"prod": {
28-
"description": "Flag to set configuration to 'production'.",
29-
"type": "boolean"
30-
}
31-
},
32-
"required": [
12+
"allOf": [
13+
{ "$ref": "./definitions.json#/definitions/architect" },
14+
{ "$ref": "./definitions.json#/definitions/base" }
3315
]
34-
}
16+
}

‎packages/angular/cli/commands/config-impl.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
tags,
1818
} from '@angular-devkit/core';
1919
import { writeFileSync } from 'fs';
20-
import { Command } from '../models/command';
20+
import { BaseCommandOptions, Command } from '../models/command';
2121
import {
2222
getWorkspace,
2323
getWorkspaceRaw,
@@ -26,7 +26,7 @@ import {
2626
} from '../utilities/config';
2727

2828

29-
export interface ConfigOptions {
29+
export interface ConfigOptions extends BaseCommandOptions {
3030
jsonPath: string;
3131
value?: string;
3232
global?: boolean;
@@ -178,8 +178,8 @@ function normalizeValue(value: string, path: string): JsonValue {
178178
return value;
179179
}
180180

181-
export class ConfigCommand extends Command {
182-
public run(options: ConfigOptions) {
181+
export class ConfigCommand<T extends ConfigOptions = ConfigOptions> extends Command<T> {
182+
public async run(options: T) {
183183
const level = options.global ? 'global' : 'local';
184184

185185
let config =
@@ -210,7 +210,7 @@ export class ConfigCommand extends Command {
210210
}
211211
}
212212

213-
private get(config: experimental.workspace.WorkspaceSchema, options: ConfigOptions) {
213+
private get(config: experimental.workspace.WorkspaceSchema, options: T) {
214214
let value;
215215
if (options.jsonPath) {
216216
value = getValueFromPath(config as {} as JsonObject, options.jsonPath);

‎packages/angular/cli/commands/config.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json-schema.org/schema",
3-
"id": "ConfigCommandOptions",
3+
"$id": "ng-cli://commands/config.json",
44
"description": "Get/set configuration values.",
55
"$longDescription": "",
66

@@ -36,4 +36,4 @@
3636
},
3737
"required": [
3838
]
39-
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"$id": "ng-cli://commands/definitions.json",
4+
5+
"definitions": {
6+
"architect": {
7+
"properties": {
8+
"project": {
9+
"type": "string",
10+
"description": "The name of the project to build.",
11+
"$default": {
12+
"$source": "argv",
13+
"index": 0
14+
}
15+
},
16+
"configuration": {
17+
"description": "Specify the configuration to use.",
18+
"type": "string",
19+
"aliases": [
20+
"c"
21+
]
22+
},
23+
"prod": {
24+
"description": "Flag to set configuration to 'production'.",
25+
"type": "boolean"
26+
}
27+
}
28+
},
29+
"base": {
30+
"type": "object",
31+
"properties": {
32+
"help": {
33+
"type": "boolean",
34+
"description": "Shows a help message."
35+
},
36+
"helpJson": {
37+
"type": "boolean",
38+
"description": "Shows the metadata associated with each flags, in JSON format."
39+
}
40+
}
41+
},
42+
"schematic": {
43+
"properties": {
44+
"dryRun": {
45+
"type": "boolean",
46+
"default": false,
47+
"aliases": [ "d" ],
48+
"description": "Run through without making any changes."
49+
},
50+
"force": {
51+
"type": "boolean",
52+
"default": false,
53+
"aliases": [ "f" ],
54+
"description": "Forces overwriting of files."
55+
},
56+
"interactive": {
57+
"type": "boolean",
58+
"default": "true",
59+
"description": "Disables interactive inputs (i.e., prompts)."
60+
}
61+
}
62+
}
63+
}
64+
}

‎packages/angular/cli/commands/doc-impl.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,23 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Command } from '../models/command';
9+
import { BaseCommandOptions, Command } from '../models/command';
1010
const opn = require('opn');
1111

12-
export interface Options {
12+
export interface DocCommandOptions extends BaseCommandOptions {
1313
keyword: string;
1414
search?: boolean;
1515
}
1616

17-
export class DocCommand extends Command {
18-
public validate(options: Options) {
19-
if (!options.keyword) {
20-
this.logger.error(`keyword argument is required.`);
21-
22-
return false;
23-
}
24-
25-
return true;
26-
}
27-
28-
public async run(options: Options) {
17+
export class DocCommand<T extends DocCommandOptions = DocCommandOptions> extends Command<T> {
18+
public async run(options: T) {
2919
let searchUrl = `https://angular.io/api?query=${options.keyword}`;
3020
if (options.search) {
3121
searchUrl = `https://www.google.com/search?q=site%3Aangular.io+${options.keyword}`;
3222
}
3323

34-
return opn(searchUrl);
24+
return opn(searchUrl, {
25+
wait: false,
26+
});
3527
}
3628
}

‎packages/angular/cli/commands/doc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json-schema.org/schema",
3-
"id": "DocCommandOptions",
3+
"$id": "ng-cli://commands/doc.json",
44
"description": "Opens the official Angular API documentation for a given keyword.",
55
"$longDescription": "",
66

@@ -27,4 +27,4 @@
2727
},
2828
"required": [
2929
]
30-
}
30+
}
+6-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/schema",
3-
"id": "E2eCommandOptions",
4-
"description": "",
3+
"$id": "ng-cli://commands/e2e.json",
4+
"description": "Runs the end-to-end tests.",
55
"$longDescription": "",
66

77
"$aliases": [ "e" ],
@@ -10,17 +10,8 @@
1010
"$impl": "./e2e-impl#E2eCommand",
1111

1212
"type": "object",
13-
"properties": {
14-
"configuration": {
15-
"description": "Specify the configuration to use.",
16-
"type": "string",
17-
"aliases": ["c"]
18-
},
19-
"prod": {
20-
"description": "Flag to set configuration to 'production'.",
21-
"type": "boolean"
22-
}
23-
},
24-
"required": [
13+
"allOf": [
14+
{ "$ref": "./definitions.json#/definitions/architect" },
15+
{ "$ref": "./definitions.json#/definitions/base" }
2516
]
26-
}
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.