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 { JsonParseMode , parseJsonAst } from '@angular-devkit/core' ;
9- import { Rule , Tree , chain } from '@angular-devkit/schematics' ;
8+ import { Rule , chain } from '@angular-devkit/schematics' ;
109import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
1110import {
1211 NodeDependencyType ,
1312 addPackageJsonDependency ,
1413 getPackageJsonDependency ,
1514} from '../../utility/dependencies' ;
16- import {
17- findPropertyInAstObject ,
18- insertPropertyInAstObjectInOrder ,
19- } from '../../utility/json-utils' ;
15+ import { JSONFile } from '../../utility/json-file' ;
2016import { latestVersions } from '../../utility/latest-versions' ;
2117
2218export function typeScriptHelpersRule ( ) : Rule {
@@ -42,38 +38,27 @@ export function typeScriptHelpersRule(): Rule {
4238}
4339
4440function _updateTsConfig ( ) : Rule {
45- return ( host : Tree ) => {
41+ return ( host ) => {
4642 const tsConfigPath = '/tsconfig.json' ;
47- const buffer = host . read ( tsConfigPath ) ;
48- if ( ! buffer ) {
49- return host ;
50- }
51-
52- const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
53- if ( tsCfgAst . kind !== 'object' ) {
54- return host ;
55- }
5643
57- const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
58- if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
59- return host ;
44+ let tsConfigJson ;
45+ try {
46+ tsConfigJson = new JSONFile ( host , tsConfigPath ) ;
47+ } catch {
48+ return ;
6049 }
6150
62- const importHelpers = findPropertyInAstObject ( compilerOptions , 'importHelpers' ) ;
63- if ( importHelpers && importHelpers . value === true ) {
64- return host ;
51+ const compilerOptions = tsConfigJson . get ( [ ' compilerOptions' ] ) ;
52+ if ( ! compilerOptions || typeof compilerOptions !== 'object' ) {
53+ return ;
6554 }
6655
67- const recorder = host . beginUpdate ( tsConfigPath ) ;
68- if ( importHelpers ) {
69- const { start, end } = importHelpers ;
70- recorder . remove ( start . offset , end . offset - start . offset ) ;
71- recorder . insertLeft ( start . offset , 'true' ) ;
72- } else {
73- insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'importHelpers' , true , 4 ) ;
56+ const importHelpersPath = [ 'compilerOptions' , 'importHelpers' ] ;
57+ const importHelpers = tsConfigJson . get ( importHelpersPath ) ;
58+ if ( importHelpers === true ) {
59+ return ;
7460 }
75- host . commitUpdate ( recorder ) ;
7661
77- return host ;
62+ tsConfigJson . modify ( importHelpersPath , true ) ;
7863 } ;
7964}
0 commit comments