6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import {
9
+ JsonAstObject ,
9
10
JsonParseMode ,
10
11
isJsonObject ,
11
12
join ,
@@ -17,6 +18,7 @@ import { Rule, Tree } from '@angular-devkit/schematics';
17
18
import {
18
19
findPropertyInAstObject ,
19
20
insertPropertyInAstObjectInOrder ,
21
+ removePropertyInAstObject ,
20
22
} from '../../utility/json-utils' ;
21
23
22
24
// tslint:disable-next-line:max-line-length
@@ -40,24 +42,12 @@ not IE 9-11 # For IE 9-11 support, remove 'not'.`;
40
42
export function updateES5Projects ( ) : Rule {
41
43
return ( host : Tree ) => {
42
44
const tsConfigPath = '/tsconfig.json' ;
43
- const buffer = host . read ( tsConfigPath ) ;
44
- if ( ! buffer ) {
45
- return host ;
46
- }
47
-
48
- const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
49
-
50
- if ( tsCfgAst . kind !== 'object' ) {
51
- return host ;
52
- }
53
-
54
- const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
55
- if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
45
+ const compilerOptions = getCompilerOptionsAstObject ( host , tsConfigPath ) ;
46
+ if ( ! compilerOptions ) {
56
47
return host ;
57
48
}
58
49
59
50
const recorder = host . beginUpdate ( tsConfigPath ) ;
60
-
61
51
const scriptTarget = findPropertyInAstObject ( compilerOptions , 'target' ) ;
62
52
if ( ! scriptTarget ) {
63
53
insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'target' , 'es2015' , 4 ) ;
@@ -78,11 +68,11 @@ export function updateES5Projects(): Rule {
78
68
79
69
host . commitUpdate ( recorder ) ;
80
70
81
- return updateBrowserlist ;
71
+ return updateProjects ;
82
72
} ;
83
73
}
84
74
85
- function updateBrowserlist ( ) : Rule {
75
+ function updateProjects ( ) : Rule {
86
76
return ( tree ) => {
87
77
const angularConfigContent = tree . read ( 'angular.json' ) || tree . read ( '.angular.json' ) ;
88
78
@@ -110,6 +100,34 @@ function updateBrowserlist(): Rule {
110
100
continue ;
111
101
}
112
102
103
+ // Older projects app and spec ts configs had script and module set in them.
104
+ const tsConfigs = [ ] ;
105
+ const architect = project . architect ;
106
+ if ( isJsonObject ( architect )
107
+ && isJsonObject ( architect . build )
108
+ && isJsonObject ( architect . build . options )
109
+ && typeof architect . build . options . tsConfig === 'string' ) {
110
+ tsConfigs . push ( architect . build . options . tsConfig ) ;
111
+ }
112
+
113
+ if ( isJsonObject ( architect )
114
+ && isJsonObject ( architect . test )
115
+ && isJsonObject ( architect . test . options )
116
+ && typeof architect . test . options . tsConfig === 'string' ) {
117
+ tsConfigs . push ( architect . test . options . tsConfig ) ;
118
+ }
119
+
120
+ for ( const tsConfig of tsConfigs ) {
121
+ const compilerOptions = getCompilerOptionsAstObject ( tree , tsConfig ) ;
122
+ if ( ! compilerOptions ) {
123
+ continue ;
124
+ }
125
+ const recorder = tree . beginUpdate ( tsConfig ) ;
126
+ removePropertyInAstObject ( recorder , compilerOptions , 'target' ) ;
127
+ removePropertyInAstObject ( recorder , compilerOptions , 'module' ) ;
128
+ tree . commitUpdate ( recorder ) ;
129
+ }
130
+
113
131
const browserslistPath = join ( normalize ( project . root ) , 'browserslist' ) ;
114
132
if ( typeof project . sourceRoot === 'string' ) {
115
133
// Move the CLI 7 style browserlist to root if it's there.
@@ -143,3 +161,23 @@ function updateBrowserlist(): Rule {
143
161
return tree ;
144
162
} ;
145
163
}
164
+
165
+ function getCompilerOptionsAstObject ( host : Tree , tsConfigPath : string ) : JsonAstObject | undefined {
166
+ const buffer = host . read ( tsConfigPath ) ;
167
+ if ( ! buffer ) {
168
+ return ;
169
+ }
170
+
171
+ const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
172
+
173
+ if ( tsCfgAst . kind !== 'object' ) {
174
+ return ;
175
+ }
176
+
177
+ const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
178
+ if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
179
+ return ;
180
+ }
181
+
182
+ return compilerOptions ;
183
+ }
0 commit comments