6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Architect , Target } from '@angular-devkit/architect' ;
10
- import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node' ;
11
- import { json } from '@angular-devkit/core' ;
12
9
import { Argv } from 'yargs' ;
13
- import { isPackageNameSafeForAnalytics } from '../analytics/analytics ' ;
10
+ import { ArchitectBaseCommandModule } from './architect-base-command-module ' ;
14
11
import {
15
- CommandModule ,
16
12
CommandModuleError ,
17
13
CommandModuleImplementation ,
18
- CommandScope ,
19
14
Options ,
20
15
OtherOptions ,
21
16
} from './command-module' ;
22
- import { getArchitectTargetOptions } from './utilities/architect' ;
23
17
24
18
export interface ArchitectCommandArgs {
25
19
configuration ?: string ;
26
20
project ?: string ;
27
21
}
28
22
29
23
export abstract class ArchitectCommandModule
30
- extends CommandModule < ArchitectCommandArgs >
24
+ extends ArchitectBaseCommandModule < ArchitectCommandArgs >
31
25
implements CommandModuleImplementation < ArchitectCommandArgs >
32
26
{
33
- static override scope = CommandScope . In ;
34
27
abstract readonly multiTarget : boolean ;
35
- readonly missingErrorTarget : string | undefined ;
36
- protected override shouldReportAnalytics = false ;
37
28
38
29
async builder ( argv : Argv ) : Promise < Argv < ArchitectCommandArgs > > {
39
30
const localYargs : Argv < ArchitectCommandArgs > = argv
@@ -52,35 +43,32 @@ export abstract class ArchitectCommandModule
52
43
} )
53
44
. strict ( ) ;
54
45
55
- const targetSpecifier = this . makeTargetSpecifier ( ) ;
56
- if ( ! targetSpecifier . project ) {
46
+ const project = this . getArchitectProject ( ) ;
47
+ if ( ! project ) {
57
48
return localYargs ;
58
49
}
59
50
60
- const schemaOptions = await getArchitectTargetOptions ( this . context , targetSpecifier ) ;
51
+ const target = this . getArchitectTarget ( ) ;
52
+ const schemaOptions = await this . getArchitectTargetOptions ( {
53
+ project,
54
+ target,
55
+ } ) ;
61
56
62
57
return this . addSchemaOptionsToCommand ( localYargs , schemaOptions ) ;
63
58
}
64
59
65
- async run ( options : Options < ArchitectCommandArgs > ) : Promise < number | void > {
60
+ async run ( options : Options < ArchitectCommandArgs > & OtherOptions ) : Promise < number | void > {
66
61
const { logger, workspace } = this . context ;
67
62
if ( ! workspace ) {
68
63
logger . fatal ( 'A workspace is required for this command.' ) ;
69
64
70
65
return 1 ;
71
66
}
72
67
73
- const registry = new json . schema . CoreSchemaRegistry ( ) ;
74
- registry . addPostTransform ( json . schema . transforms . addUndefinedDefaults ) ;
75
- registry . useXDeprecatedProvider ( ( msg ) => this . context . logger . warn ( msg ) ) ;
76
-
77
- const architectHost = new WorkspaceNodeModulesArchitectHost ( workspace , workspace . basePath ) ;
78
- const architect = new Architect ( architectHost , registry ) ;
79
-
80
- const targetSpec = this . makeTargetSpecifier ( options ) ;
81
- if ( ! targetSpec . project ) {
82
- const target = this . getArchitectTarget ( ) ;
68
+ const target = this . getArchitectTarget ( ) ;
69
+ const { configuration = '' , project, ...architectOptions } = options ;
83
70
71
+ if ( ! project ) {
84
72
// This runs each target sequentially.
85
73
// Running them in parallel would jumble the log messages.
86
74
let result = 0 ;
@@ -92,12 +80,12 @@ export abstract class ArchitectCommandModule
92
80
}
93
81
94
82
for ( const project of projectNames ) {
95
- result |= await this . runSingleTarget ( { ... targetSpec , project } , options , architect ) ;
83
+ result |= await this . runSingleTarget ( { configuration , target , project } , architectOptions ) ;
96
84
}
97
85
98
86
return result ;
99
87
} else {
100
- return await this . runSingleTarget ( targetSpec , options , architect ) ;
88
+ return await this . runSingleTarget ( { configuration , target , project } , architectOptions ) ;
101
89
}
102
90
}
103
91
@@ -128,14 +116,6 @@ export abstract class ArchitectCommandModule
128
116
return this . command ?. split ( ' ' , 1 ) [ 0 ] ;
129
117
}
130
118
131
- private makeTargetSpecifier ( options ?: Options < ArchitectCommandArgs > ) : Target {
132
- return {
133
- project : options ?. project ?? this . getArchitectProject ( ) ?? '' ,
134
- target : this . getArchitectTarget ( ) ,
135
- configuration : options ?. configuration ?? '' ,
136
- } ;
137
- }
138
-
139
119
private getProjectNamesByTarget ( target : string ) : string [ ] | undefined {
140
120
const workspace = this . context . workspace ;
141
121
if ( ! workspace ) {
@@ -174,59 +154,4 @@ export abstract class ArchitectCommandModule
174
154
175
155
return undefined ;
176
156
}
177
-
178
- private async runSingleTarget (
179
- target : Target ,
180
- options : Options < ArchitectCommandArgs > & OtherOptions ,
181
- architect : Architect ,
182
- ) : Promise < number > {
183
- // Remove options
184
- const { configuration, project, ...extraOptions } = options ;
185
- const architectHost = await this . getArchitectHost ( ) ;
186
-
187
- let builderName : string ;
188
- try {
189
- builderName = await architectHost . getBuilderNameForTarget ( target ) ;
190
- } catch ( e ) {
191
- throw new CommandModuleError ( this . missingErrorTarget ?? e . message ) ;
192
- }
193
-
194
- await this . reportAnalytics ( {
195
- ...( await architectHost . getOptionsForTarget ( target ) ) ,
196
- ...extraOptions ,
197
- } ) ;
198
-
199
- const { logger } = this . context ;
200
-
201
- const run = await architect . scheduleTarget ( target , extraOptions as json . JsonObject , {
202
- logger,
203
- analytics : isPackageNameSafeForAnalytics ( builderName ) ? await this . getAnalytics ( ) : undefined ,
204
- } ) ;
205
-
206
- const { error, success } = await run . output . toPromise ( ) ;
207
- await run . stop ( ) ;
208
-
209
- if ( error ) {
210
- logger . error ( error ) ;
211
- }
212
-
213
- return success ? 0 : 1 ;
214
- }
215
-
216
- private _architectHost : WorkspaceNodeModulesArchitectHost | undefined ;
217
- private getArchitectHost ( ) : WorkspaceNodeModulesArchitectHost {
218
- if ( this . _architectHost ) {
219
- return this . _architectHost ;
220
- }
221
-
222
- const { workspace } = this . context ;
223
- if ( ! workspace ) {
224
- throw new CommandModuleError ( 'A workspace is required for this command.' ) ;
225
- }
226
-
227
- return ( this . _architectHost = new WorkspaceNodeModulesArchitectHost (
228
- workspace ,
229
- workspace . basePath ,
230
- ) ) ;
231
- }
232
157
}
0 commit comments