6
6
CommandScope
7
7
} from '../models/command' ;
8
8
import { oneLine } from 'common-tags' ;
9
- import { Logger } from '@angular-devkit/core/src/logger ' ;
9
+ import { logging } from '@angular-devkit/core' ;
10
10
import { camelize } from '@angular-devkit/core/src/utils/strings' ;
11
11
12
12
const yargsParser = require ( 'yargs-parser' ) ;
@@ -19,12 +19,13 @@ export interface CommandMap {
19
19
* Run a command.
20
20
* @param commandMap Map of available commands.
21
21
* @param args Raw unparsed arguments.
22
+ * @param logger The logger to use.
22
23
* @param context Execution context.
23
24
*/
24
25
export async function runCommand ( commandMap : CommandMap ,
25
- args : string [ ] ,
26
- logger : Logger ,
27
- context : CommandContext ) : Promise < any > {
26
+ args : string [ ] ,
27
+ logger : logging . Logger ,
28
+ context : CommandContext ) : Promise < any > {
28
29
29
30
const rawOptions = yargsParser ( args , { alias : { help : [ 'h' ] } , boolean : [ 'help' ] } ) ;
30
31
let commandName = rawOptions . _ [ 0 ] ;
@@ -55,11 +56,10 @@ export async function runCommand(commandMap: CommandMap,
55
56
56
57
const command = new Cmd ( context , logger ) ;
57
58
58
- let options = parseOptions ( yargsParser , args , command . options ) ;
59
- options = mapArguments ( options , command . arguments ) ;
59
+ args = await command . initializeRaw ( args ) ;
60
+ let options = parseOptions ( args , command . options , command . arguments ) ;
60
61
await command . initialize ( options ) ;
61
- options = parseOptions ( yargsParser , args , command . options ) ;
62
- options = mapArguments ( options , command . arguments ) ;
62
+ options = parseOptions ( args , command . options , command . arguments ) ;
63
63
if ( commandName === 'help' ) {
64
64
options . commandMap = commandMap ;
65
65
}
@@ -74,9 +74,13 @@ export async function runCommand(commandMap: CommandMap,
74
74
}
75
75
}
76
76
77
- function parseOptions ( parser : Function ,
78
- args : string [ ] ,
79
- cmdOpts : Option [ ] ) : any {
77
+ export function parseOptions < T = any > (
78
+ args : string [ ] ,
79
+ cmdOpts : Option [ ] ,
80
+ commandArguments : string [ ] ,
81
+ ) : T {
82
+ const parser = yargsParser ;
83
+
80
84
const aliases = cmdOpts . concat ( )
81
85
. filter ( o => o . aliases && o . aliases . length > 0 )
82
86
. reduce ( ( aliases : any , opt : Option ) => {
@@ -131,23 +135,17 @@ function parseOptions(parser: Function,
131
135
. filter ( key => key . indexOf ( '-' ) !== - 1 )
132
136
. forEach ( key => delete parsedOptions [ key ] ) ;
133
137
134
- return parsedOptions ;
135
- }
136
-
137
- // Map arguments to options.
138
- function mapArguments ( options : any , args : string [ ] ) : any {
139
- const optsWithMappedArgs = { ...options } ;
140
- optsWithMappedArgs . _ . forEach ( ( value : string , index : number ) => {
138
+ parsedOptions . _ . forEach ( ( value : string , index : number ) => {
141
139
// Remove the starting "<" and trailing ">".
142
- const arg = args [ index ] ;
140
+ const arg = commandArguments [ index ] ;
143
141
if ( arg ) {
144
- optsWithMappedArgs [ arg ] = value ;
142
+ parsedOptions [ arg ] = value ;
145
143
}
146
144
} ) ;
147
145
148
- delete optsWithMappedArgs . _ ;
146
+ delete parsedOptions . _ ;
149
147
150
- return optsWithMappedArgs ;
148
+ return parsedOptions ;
151
149
}
152
150
153
151
// Find a command.
0 commit comments