55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.io/license
77 */
8- import { JsonAstObject , JsonParseMode , parseJsonAst } from '@angular-devkit/core' ;
8+ import { JsonAstObject } from '@angular-devkit/core' ;
99import { Rule , Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
1010import {
1111 findPropertyInAstObject ,
1212 insertPropertyInAstObjectInOrder ,
1313 removePropertyInAstObject ,
1414} from '../../utility/json-utils' ;
1515import { Builders } from '../../utility/workspace-models' ;
16- import { getAllOptions , getTargets , getWorkspace } from './utils' ;
16+ import { getAllOptions , getTargets , getWorkspace , readJsonFileAsAstObject } from './utils' ;
1717
1818
1919/**
@@ -52,7 +52,7 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
5252 }
5353
5454 const tsConfigPath = tsConfigOption . value ;
55- let tsConfigAst = getTsConfigAst ( tree , tsConfigPath ) ;
55+ let tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ;
5656 if ( ! tsConfigAst ) {
5757 continue ;
5858 }
@@ -77,7 +77,7 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
7777 if ( builderName !== Builders . Karma ) {
7878 // Note: we need to re-read the tsconfig after very commit because
7979 // otherwise the updates will be out of sync since we are ammending the same node.
80- tsConfigAst = getTsConfigAst ( tree , tsConfigPath ) as JsonAstObject ;
80+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ;
8181 const files = findPropertyInAstObject ( tsConfigAst , 'files' ) ;
8282 const include = findPropertyInAstObject ( tsConfigAst , 'include' ) ;
8383
@@ -93,32 +93,17 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
9393 tree . commitUpdate ( recorder ) ;
9494
9595 if ( builderName === Builders . Browser ) {
96- tsConfigAst = getTsConfigAst ( tree , tsConfigPath ) as JsonAstObject ;
96+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ;
9797 recorder = tree . beginUpdate ( tsConfigPath ) ;
9898 insertPropertyInAstObjectInOrder ( recorder , tsConfigAst , 'include' , [ `${ rootSrc } **/*.d.ts` ] , 2 ) ;
9999 tree . commitUpdate ( recorder ) ;
100100 }
101101
102- tsConfigAst = getTsConfigAst ( tree , tsConfigPath ) as JsonAstObject ;
102+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ;
103103 recorder = tree . beginUpdate ( tsConfigPath ) ;
104104 removePropertyInAstObject ( recorder , tsConfigAst , 'exclude' ) ;
105105 tree . commitUpdate ( recorder ) ;
106106 }
107107 }
108108 }
109109}
110-
111- function getTsConfigAst ( tree : Tree , path : string ) : JsonAstObject | undefined {
112- const configBuffer = tree . read ( path ) ;
113- if ( ! configBuffer ) {
114- return undefined ;
115- }
116-
117- const content = configBuffer . toString ( ) ;
118- const tsConfigAst = parseJsonAst ( content , JsonParseMode . Loose ) ;
119- if ( ! tsConfigAst || tsConfigAst . kind !== 'object' ) {
120- return undefined ;
121- }
122-
123- return tsConfigAst ;
124- }
0 commit comments