Skip to content

Commit 66fbc59

Browse files
hanslalexeagle
authored andcommitted
feat(@angular/cli): add long description and suboption option type
1 parent 37d1a43 commit 66fbc59

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

packages/angular/cli/commands/add.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "http://json-schema.org/schema",
33
"$id": "ng-cli://commands/add.json",
44
"description": "Add support for a library to your project.",
5-
"$longDescription": "",
5+
"$longDescription": "./add.md",
66

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

packages/angular/cli/commands/add.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for a library in your project, for example adding `@angular/pwa` which would configure
2+
your project for PWA support.

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

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ export class GenerateCommand<
4242

4343
this.description.suboptions[`${collectionName}:${name}`] = options;
4444
}
45+
46+
this.description.options.forEach(option => {
47+
if (option.name == 'schematic') {
48+
option.type = 'suboption';
49+
}
50+
});
4551
}
4652

4753
public async run(options: T) {
@@ -78,6 +84,7 @@ export class GenerateCommand<
7884
public async printHelp(options: T) {
7985
await super.printHelp(options);
8086

87+
this.logger.info('');
8188
if (Object.keys(this.description.suboptions || {}).length == 1) {
8289
this.logger.info(`\nTo see help for a schematic run:`);
8390
this.logger.info(terminal.cyan(` ng generate <schematic> --help`));

packages/angular/cli/models/interface.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ export enum OptionType {
7474
* An option description. This is exposed when using `ng --help-json`.
7575
*/
7676
export interface Option {
77+
/**
78+
* The name of the option.
79+
*/
7780
name: string;
81+
82+
/**
83+
* A short description of the option.
84+
*/
7885
description: string;
7986

80-
type: OptionType;
87+
type: OptionType | 'suboption';
8188
types?: OptionType[];
8289

8390
aliases: string[];
@@ -108,6 +115,12 @@ export enum CommandType {
108115
export interface CommandDescription {
109116
name: string;
110117
description: string;
118+
119+
/**
120+
* A long description of the option, in Markdown format.
121+
*/
122+
longDescription: string;
123+
111124
options: Option[];
112125

113126
aliases: string[];

packages/angular/cli/models/parser.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
*
88
*/
99
import { strings } from '@angular-devkit/core';
10-
import { Arguments, Option, Value } from './interface';
10+
import { Arguments, Option, OptionType, Value } from './interface';
1111

1212

13-
function _coerceType(str: string | undefined, type: string, v?: Value): Value | undefined {
13+
function _coerceType(str: string | undefined, type: OptionType, v?: Value): Value | undefined {
1414
switch (type) {
1515
case 'any':
1616
if (Array.isArray(v)) {
1717
return v.concat(str || '');
1818
}
1919

20-
return _coerceType(str, 'boolean', v) !== undefined ? _coerceType(str, 'boolean', v)
21-
: _coerceType(str, 'number', v) !== undefined ? _coerceType(str, 'number', v)
22-
: _coerceType(str, 'string', v);
20+
return _coerceType(str, OptionType.Boolean, v) !== undefined
21+
? _coerceType(str, OptionType.Boolean, v)
22+
: _coerceType(str, OptionType.Number, v) !== undefined
23+
? _coerceType(str, OptionType.Number, v)
24+
: _coerceType(str, OptionType.String, v);
2325

2426
case 'string':
2527
return str || '';
@@ -56,7 +58,13 @@ function _coerceType(str: string | undefined, type: string, v?: Value): Value |
5658
}
5759

5860
function _coerce(str: string | undefined, o: Option | null, v?: Value): Value | undefined {
59-
return _coerceType(str, o ? o.type : 'any', v);
61+
if (!o) {
62+
return _coerceType(str, OptionType.Any, v);
63+
} else if (o.type == 'suboption') {
64+
return _coerceType(str, OptionType.String, v);
65+
} else {
66+
return _coerceType(str, o.type, v);
67+
}
6068
}
6169

6270

packages/angular/cli/utilities/json-schema.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88
import { json } from '@angular-devkit/core';
99
import { ExportStringRef } from '@angular-devkit/schematics/tools';
10-
import { dirname } from 'path';
10+
import { readFileSync } from 'fs';
11+
import { dirname, resolve } from 'path';
1112
import {
1213
CommandConstructor,
1314
CommandDescription,
@@ -57,12 +58,18 @@ export async function parseJsonSchemaToCommandDescription(
5758
});
5859
}
5960

61+
let longDescription = '';
62+
if (typeof schema.$longDescription == 'string' && schema.$longDescription) {
63+
const ldPath = resolve(dirname(jsonPath), schema.$longDescription);
64+
longDescription = readFileSync(ldPath, 'utf-8');
65+
}
66+
6067
const scope = _getEnumFromValue(schema.$scope, CommandScope, CommandScope.Default);
6168
const type = _getEnumFromValue(schema.$type, CommandType, CommandType.Default);
6269
const description = '' + (schema.description === undefined ? '' : schema.description);
6370
const hidden = !!schema.$hidden;
6471

65-
return { name, description, hidden, type, options, aliases, scope, impl };
72+
return { name, description, longDescription, hidden, type, options, aliases, scope, impl };
6673
}
6774

6875
export async function parseJsonSchemaToOptions(

0 commit comments

Comments
 (0)