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 { JsonObject , logging } from '@angular-devkit/core' ;
8+ import { JsonObject , JsonParseMode , logging , parseJson } from '@angular-devkit/core' ;
99import {
1010 Rule ,
1111 SchematicContext ,
@@ -93,7 +93,7 @@ function _getNpmPackageJson(
9393 response . on ( 'data' , chunk => data += chunk ) ;
9494 response . on ( 'end' , ( ) => {
9595 try {
96- const json = JSON . parse ( data ) ;
96+ const json = parseJson ( data , JsonParseMode . Strict ) ;
9797 subject . next ( json as JsonObject ) ;
9898 subject . complete ( ) ;
9999 } catch ( err ) {
@@ -233,7 +233,10 @@ export function updatePackageJson(
233233 if ( ! packageJsonContent ) {
234234 throw new SchematicsException ( 'Could not find package.json.' ) ;
235235 }
236- const packageJson = JSON . parse ( packageJsonContent . toString ( ) ) ;
236+ const packageJson = parseJson ( packageJsonContent . toString ( ) , JsonParseMode . Strict ) ;
237+ if ( packageJson === null || typeof packageJson !== 'object' || Array . isArray ( packageJson ) ) {
238+ throw new SchematicsException ( 'Could not parse package.json.' ) ;
239+ }
237240 const packages : { [ name : string ] : string } = { } ;
238241 for ( const name of supportedPackages ) {
239242 packages [ name ] = version ;
@@ -251,17 +254,20 @@ export function updatePackageJson(
251254 if ( ! packageJsonContent ) {
252255 throw new SchematicsException ( 'Could not find package.json.' ) ;
253256 }
254- const packageJson = JSON . parse ( packageJsonContent . toString ( ) ) ;
257+ const packageJson = parseJson ( packageJsonContent . toString ( ) , JsonParseMode . Strict ) ;
258+ if ( packageJson === null || typeof packageJson !== 'object' || Array . isArray ( packageJson ) ) {
259+ throw new SchematicsException ( 'Could not parse package.json.' ) ;
260+ }
255261
256262 for ( const field of kPackageJsonDependencyFields ) {
257263 const deps = packageJson [ field ] ;
258- if ( ! deps ) {
264+ if ( ! deps || typeof deps !== 'object' || Array . isArray ( deps ) ) {
259265 continue ;
260266 }
261267
262- for ( const depName of Object . keys ( packageJson [ field ] ) ) {
268+ for ( const depName of Object . keys ( deps ) ) {
263269 if ( allVersions [ depName ] ) {
264- packageJson [ field ] [ depName ] = allVersions [ depName ] ;
270+ deps [ depName ] = allVersions [ depName ] ;
265271 }
266272 }
267273 }
0 commit comments