9
9
// tslint:disable:no-global-tslint-disable no-any
10
10
import { tags , terminal } from '@angular-devkit/core' ;
11
11
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' ;
14
14
import { NpmInstall } from '../tasks/npm-install' ;
15
15
import { getPackageManager } from '../utilities/config' ;
16
16
17
+ export interface AddCommandOptions extends BaseSchematicOptions {
18
+ collection : string ;
19
+ help ?: boolean ;
20
+ help_json ?: boolean ;
21
+ }
17
22
18
- export class AddCommand extends SchematicCommand {
23
+ export class AddCommand <
24
+ T extends AddCommandOptions = AddCommandOptions ,
25
+ > extends SchematicCommand < T > {
19
26
readonly allowPrivateSchematics = true ;
20
27
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 ) {
50
30
this . logger . fatal (
51
31
`The "ng add" command requires a name argument to be specified eg. `
52
32
+ `${ terminal . yellow ( 'ng add [name] ' ) } . For more details, use "ng help".` ,
@@ -59,16 +39,16 @@ export class AddCommand extends SchematicCommand {
59
39
60
40
const npmInstall : NpmInstall = require ( '../tasks/npm-install' ) . default ;
61
41
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 ] ;
65
45
66
46
// Remove the tag/version from the package name.
67
47
const collectionName = (
68
48
packageName . startsWith ( '@' )
69
49
? packageName . split ( '@' , 2 ) . join ( '@' )
70
50
: packageName . split ( '@' , 1 ) . join ( '@' )
71
- ) + firstArg . slice ( packageName . length ) ;
51
+ ) + options . collection . slice ( packageName . length ) ;
72
52
73
53
// We don't actually add the package to package.json, that would be the work of the package
74
54
// itself.
@@ -79,11 +59,8 @@ export class AddCommand extends SchematicCommand {
79
59
this . project . root ,
80
60
) ;
81
61
82
- // Reparse the options with the new schematic accessible.
83
- options = await this . _parseSchematicOptions ( collectionName ) ;
84
-
85
62
const runOptions = {
86
- schematicOptions : options ,
63
+ schematicOptions : options [ '--' ] || [ ] ,
87
64
workingDir : this . project . root ,
88
65
collectionName,
89
66
schematicName : 'ng-add' ,
0 commit comments