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 { JsonParseMode , parseJsonAst } from '@angular-devkit/core' ;
9
- import { Rule , Tree , chain } from '@angular-devkit/schematics' ;
8
+ import { Rule , chain } from '@angular-devkit/schematics' ;
10
9
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
11
10
import {
12
11
NodeDependencyType ,
13
12
addPackageJsonDependency ,
14
13
getPackageJsonDependency ,
15
14
} from '../../utility/dependencies' ;
16
- import {
17
- findPropertyInAstObject ,
18
- insertPropertyInAstObjectInOrder ,
19
- } from '../../utility/json-utils' ;
15
+ import { JSONFile } from '../../utility/json-file' ;
20
16
import { latestVersions } from '../../utility/latest-versions' ;
21
17
22
18
export function typeScriptHelpersRule ( ) : Rule {
@@ -42,38 +38,27 @@ export function typeScriptHelpersRule(): Rule {
42
38
}
43
39
44
40
function _updateTsConfig ( ) : Rule {
45
- return ( host : Tree ) => {
41
+ return ( host ) => {
46
42
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
- }
56
43
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 ;
60
49
}
61
50
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 ;
65
54
}
66
55
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 ;
74
60
}
75
- host . commitUpdate ( recorder ) ;
76
61
77
- return host ;
62
+ tsConfigJson . modify ( importHelpersPath , true ) ;
78
63
} ;
79
64
}
0 commit comments