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 , dirname , join , normalize , parseJsonAst } from '@angular-devkit/core' ;
8
+ import { dirname , join , normalize } from '@angular-devkit/core' ;
9
9
import {
10
10
Rule ,
11
11
SchematicsException ,
@@ -15,10 +15,7 @@ import {
15
15
} from '@angular-devkit/schematics' ;
16
16
import { Schema as UniversalOptions } from '@schematics/angular/universal/schema' ;
17
17
import { NodeDependencyType , addPackageJsonDependency } from '@schematics/angular/utility/dependencies' ;
18
- import {
19
- appendValueInAstArray ,
20
- findPropertyInAstObject ,
21
- } from '@schematics/angular/utility/json-utils' ;
18
+ import { JSONFile } from '@schematics/angular/utility/json-file' ;
22
19
import { updateWorkspace } from '@schematics/angular/utility/workspace' ;
23
20
import * as ts from 'typescript' ;
24
21
@@ -152,33 +149,14 @@ function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
152
149
return ;
153
150
}
154
151
155
- const configBuffer = host . read ( tsConfigPath ) ;
156
- if ( ! configBuffer ) {
157
- throw new SchematicsException ( `Could not find (${ tsConfigPath } )` ) ;
158
- }
159
-
160
- const content = configBuffer . toString ( ) ;
161
- const tsConfigAst = parseJsonAst ( content , JsonParseMode . Loose ) ;
162
- if ( ! tsConfigAst || tsConfigAst . kind !== 'object' ) {
163
- throw new SchematicsException ( `Invalid JSON AST Object (${ tsConfigPath } )` ) ;
164
- }
165
-
166
- const filesAstNode = findPropertyInAstObject ( tsConfigAst , 'files' ) ;
167
-
152
+ const tsConfig = new JSONFile ( host , tsConfigPath ) ;
153
+ const filesAstNode = tsConfig . get ( [ 'files' ] ) ;
168
154
const serverFilePath = stripTsExtension ( options . serverFileName ) + '.ts' ;
169
- if (
170
- filesAstNode &&
171
- filesAstNode . kind === 'array' &&
172
- ! filesAstNode . elements . some ( ( { text } ) => text === serverFilePath ) ) {
173
- const recorder = host . beginUpdate ( tsConfigPath ) ;
174
-
175
- appendValueInAstArray (
176
- recorder ,
177
- filesAstNode ,
178
- stripTsExtension ( options . serverFileName ) + '.ts' ,
179
- ) ;
180
-
181
- host . commitUpdate ( recorder ) ;
155
+ if ( Array . isArray ( filesAstNode ) && ! filesAstNode . some ( ( { text } ) => text === serverFilePath ) ) {
156
+ tsConfig . modify ( [ 'files' ] , [
157
+ ...filesAstNode ,
158
+ serverFilePath
159
+ ] ) ;
182
160
}
183
161
} ;
184
162
}
0 commit comments