5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import {
9
- JsonAstObject ,
10
- JsonParseMode ,
11
- parseJsonAst ,
12
- } from '@angular-devkit/core' ;
8
+ import { JsonAstObject } from '@angular-devkit/core' ;
13
9
import { Rule , Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
10
+ import { getWorkspacePath } from '../../utility/config' ;
14
11
import {
15
12
appendValueInAstArray ,
16
13
findPropertyInAstObject ,
17
14
insertPropertyInAstObjectInOrder ,
18
15
removePropertyInAstObject ,
19
16
} from '../../utility/json-utils' ;
17
+ import { Builders } from '../../utility/workspace-models' ;
18
+ import { getAllOptions , getTargets , getWorkspace } from './utils' ;
20
19
21
20
export const ANY_COMPONENT_STYLE_BUDGET = {
22
21
type : 'anyComponentStyle' ,
@@ -25,61 +24,19 @@ export const ANY_COMPONENT_STYLE_BUDGET = {
25
24
26
25
export function UpdateWorkspaceConfig ( ) : Rule {
27
26
return ( tree : Tree ) => {
28
- let workspaceConfigPath = 'angular.json' ;
29
- let angularConfigContent = tree . read ( workspaceConfigPath ) ;
30
-
31
- if ( ! angularConfigContent ) {
32
- workspaceConfigPath = '.angular.json' ;
33
- angularConfigContent = tree . read ( workspaceConfigPath ) ;
34
-
35
- if ( ! angularConfigContent ) {
36
- return ;
37
- }
38
- }
39
-
40
- const angularJson = parseJsonAst ( angularConfigContent . toString ( ) , JsonParseMode . Loose ) ;
41
- if ( angularJson . kind !== 'object' ) {
42
- return ;
27
+ const workspacePath = getWorkspacePath ( tree ) ;
28
+ const workspace = getWorkspace ( tree ) ;
29
+ const recorder = tree . beginUpdate ( workspacePath ) ;
30
+
31
+ for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
32
+ updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
33
+ updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
34
+ addAnyComponentStyleBudget ( recorder , target ) ;
43
35
}
44
36
45
- const projects = findPropertyInAstObject ( angularJson , 'projects' ) ;
46
- if ( ! projects || projects . kind !== 'object' ) {
47
- return ;
48
- }
49
-
50
- // For all projects
51
- const recorder = tree . beginUpdate ( workspaceConfigPath ) ;
52
- for ( const project of projects . properties ) {
53
- const projectConfig = project . value ;
54
- if ( projectConfig . kind !== 'object' ) {
55
- break ;
56
- }
57
-
58
- const architect = findPropertyInAstObject ( projectConfig , 'architect' ) ;
59
- if ( ! architect || architect . kind !== 'object' ) {
60
- break ;
61
- }
62
-
63
- const buildTarget = findPropertyInAstObject ( architect , 'build' ) ;
64
- if ( buildTarget && buildTarget . kind === 'object' ) {
65
- const builder = findPropertyInAstObject ( buildTarget , 'builder' ) ;
66
- // Projects who's build builder is not build-angular:browser
67
- if ( builder && builder . kind === 'string' && builder . value === '@angular-devkit/build-angular:browser' ) {
68
- updateStyleOrScriptOption ( 'styles' , recorder , buildTarget ) ;
69
- updateStyleOrScriptOption ( 'scripts' , recorder , buildTarget ) ;
70
- addAnyComponentStyleBudget ( recorder , buildTarget ) ;
71
- }
72
- }
73
-
74
- const testTarget = findPropertyInAstObject ( architect , 'test' ) ;
75
- if ( testTarget && testTarget . kind === 'object' ) {
76
- const builder = findPropertyInAstObject ( testTarget , 'builder' ) ;
77
- // Projects who's build builder is not build-angular:browser
78
- if ( builder && builder . kind === 'string' && builder . value === '@angular-devkit/build-angular:karma' ) {
79
- updateStyleOrScriptOption ( 'styles' , recorder , testTarget ) ;
80
- updateStyleOrScriptOption ( 'scripts' , recorder , testTarget ) ;
81
- }
82
- }
37
+ for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
38
+ updateStyleOrScriptOption ( 'styles' , recorder , target ) ;
39
+ updateStyleOrScriptOption ( 'scripts' , recorder , target ) ;
83
40
}
84
41
85
42
tree . commitUpdate ( recorder ) ;
@@ -88,23 +45,6 @@ export function UpdateWorkspaceConfig(): Rule {
88
45
} ;
89
46
}
90
47
91
- /**
92
- * Helper to retreive all the options in various configurations
93
- */
94
- function getAllOptions ( builderConfig : JsonAstObject , configurationsOnly = false ) : JsonAstObject [ ] {
95
- const options = [ ] ;
96
- const configurations = findPropertyInAstObject ( builderConfig , 'configurations' ) ;
97
- if ( configurations && configurations . kind === 'object' ) {
98
- options . push ( ...configurations . properties . map ( x => x . value ) ) ;
99
- }
100
-
101
- if ( ! configurationsOnly ) {
102
- options . push ( findPropertyInAstObject ( builderConfig , 'options' ) ) ;
103
- }
104
-
105
- return options . filter ( o => o && o . kind === 'object' ) as JsonAstObject [ ] ;
106
- }
107
-
108
48
function updateStyleOrScriptOption ( property : 'scripts' | 'styles' , recorder : UpdateRecorder , builderConfig : JsonAstObject ) {
109
49
const options = getAllOptions ( builderConfig ) ;
110
50
0 commit comments