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 , dirname , join , normalize , parseJsonAst } from '@angular-devkit/core' ;
8+ import { dirname , join , normalize } from '@angular-devkit/core' ;
99import {
1010 Rule ,
1111 SchematicsException ,
@@ -15,10 +15,7 @@ import {
1515} from '@angular-devkit/schematics' ;
1616import { Schema as UniversalOptions } from '@schematics/angular/universal/schema' ;
1717import { 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' ;
2219import { updateWorkspace } from '@schematics/angular/utility/workspace' ;
2320import * as ts from 'typescript' ;
2421
@@ -152,33 +149,14 @@ function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
152149 return ;
153150 }
154151
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' ] ) ;
168154 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+ ] ) ;
182160 }
183161 } ;
184162}
0 commit comments