@@ -11,14 +11,9 @@ import {
1111 join ,
1212 normalize ,
1313 parseJson ,
14- parseJsonAst ,
1514} from '@angular-devkit/core' ;
1615import { Rule , Tree } from '@angular-devkit/schematics' ;
17- import {
18- findPropertyInAstObject ,
19- insertPropertyInAstObjectInOrder ,
20- removePropertyInAstObject ,
21- } from '../../utility/json-utils' ;
16+ import { JSONFile } from '../../utility/json-file' ;
2217
2318const browserslistContent = `# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
2419# For additional information regarding the format and rule options, please see:
@@ -106,57 +101,28 @@ export function updateES5Projects(): Rule {
106101}
107102
108103function updateTsConfig ( tree : Tree , tsConfigPath : string ) : void {
109- const buffer = tree . read ( tsConfigPath ) ;
110- if ( ! buffer ) {
111- return ;
112- }
113-
114- const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
115-
116- if ( tsCfgAst . kind !== 'object' ) {
104+ let tsConfigJson ;
105+ try {
106+ tsConfigJson = new JSONFile ( tree , tsConfigPath ) ;
107+ } catch {
117108 return ;
118109 }
119110
120- const configExtends = findPropertyInAstObject ( tsCfgAst , 'extends' ) ;
121- const isExtendedConfig = configExtends && configExtends . kind === 'string' ;
122-
123- const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
124- if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
111+ const compilerOptions = tsConfigJson . get ( [ 'compilerOptions' ] ) ;
112+ if ( ! compilerOptions || typeof compilerOptions !== 'object' ) {
125113 return ;
126114 }
127115
128- const recorder = tree . beginUpdate ( tsConfigPath ) ;
116+ const configExtends = tsConfigJson . get ( [ 'extends' ] ) ;
117+ const isExtended = configExtends && typeof configExtends === 'string' ;
129118
130- if ( isExtendedConfig ) {
131- removePropertyInAstObject ( recorder , compilerOptions , 'target' ) ;
132- removePropertyInAstObject ( recorder , compilerOptions , 'module' ) ;
133- removePropertyInAstObject ( recorder , compilerOptions , 'downlevelIteration' ) ;
119+ if ( isExtended ) {
120+ tsConfigJson . remove ( [ ' compilerOptions' , 'target' ] ) ;
121+ tsConfigJson . remove ( [ ' compilerOptions' , 'module' ] ) ;
122+ tsConfigJson . remove ( [ ' compilerOptions' , 'downlevelIteration' ] ) ;
134123 } else {
135- const downlevelIteration = findPropertyInAstObject ( compilerOptions , 'downlevelIteration' ) ;
136- if ( ! downlevelIteration ) {
137- insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'downlevelIteration' , true , 4 ) ;
138- } else if ( ! downlevelIteration . value ) {
139- const { start, end } = downlevelIteration ;
140- recorder . remove ( start . offset , end . offset - start . offset ) ;
141- recorder . insertLeft ( start . offset , 'true' ) ;
142- }
143- const scriptTarget = findPropertyInAstObject ( compilerOptions , 'target' ) ;
144- if ( ! scriptTarget ) {
145- insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'target' , 'es2015' , 4 ) ;
146- } else if ( scriptTarget . value !== 'es2015' ) {
147- const { start, end } = scriptTarget ;
148- recorder . remove ( start . offset , end . offset - start . offset ) ;
149- recorder . insertLeft ( start . offset , '"es2015"' ) ;
150- }
151- const scriptModule = findPropertyInAstObject ( compilerOptions , 'module' ) ;
152- if ( ! scriptModule ) {
153- insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'module' , 'esnext' , 4 ) ;
154- } else if ( scriptModule . value !== 'esnext' ) {
155- const { start, end } = scriptModule ;
156- recorder . remove ( start . offset , end . offset - start . offset ) ;
157- recorder . insertLeft ( start . offset , '"esnext"' ) ;
158- }
124+ tsConfigJson . modify ( [ 'compilerOptions' , 'target' ] , 'es2015' ) ;
125+ tsConfigJson . modify ( [ 'compilerOptions' , 'module' ] , 'esnext' ) ;
126+ tsConfigJson . modify ( [ 'compilerOptions' , 'downlevelIteration' ] , true ) ;
159127 }
160-
161- tree . commitUpdate ( recorder ) ;
162128}
0 commit comments