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 { JsonObject , logging } from '@angular-devkit/core' ;
8
+ import { JsonObject , JsonParseMode , logging , parseJson } from '@angular-devkit/core' ;
9
9
import {
10
10
Rule ,
11
11
SchematicContext ,
@@ -93,7 +93,7 @@ function _getNpmPackageJson(
93
93
response . on ( 'data' , chunk => data += chunk ) ;
94
94
response . on ( 'end' , ( ) => {
95
95
try {
96
- const json = JSON . parse ( data ) ;
96
+ const json = parseJson ( data , JsonParseMode . Strict ) ;
97
97
subject . next ( json as JsonObject ) ;
98
98
subject . complete ( ) ;
99
99
} catch ( err ) {
@@ -233,7 +233,10 @@ export function updatePackageJson(
233
233
if ( ! packageJsonContent ) {
234
234
throw new SchematicsException ( 'Could not find package.json.' ) ;
235
235
}
236
- const packageJson = JSON . parse ( packageJsonContent . toString ( ) ) ;
236
+ const packageJson = parseJson ( packageJsonContent . toString ( ) , JsonParseMode . Loose ) ;
237
+ if ( packageJson === null || typeof packageJson !== 'object' || Array . isArray ( packageJson ) ) {
238
+ throw new SchematicsException ( 'Could not parse package.json.' ) ;
239
+ }
237
240
const packages : { [ name : string ] : string } = { } ;
238
241
for ( const name of supportedPackages ) {
239
242
packages [ name ] = version ;
@@ -251,17 +254,20 @@ export function updatePackageJson(
251
254
if ( ! packageJsonContent ) {
252
255
throw new SchematicsException ( 'Could not find package.json.' ) ;
253
256
}
254
- const packageJson = JSON . parse ( packageJsonContent . toString ( ) ) ;
257
+ const packageJson = parseJson ( packageJsonContent . toString ( ) , JsonParseMode . Loose ) ;
258
+ if ( packageJson === null || typeof packageJson !== 'object' || Array . isArray ( packageJson ) ) {
259
+ throw new SchematicsException ( 'Could not parse package.json.' ) ;
260
+ }
255
261
256
262
for ( const field of kPackageJsonDependencyFields ) {
257
263
const deps = packageJson [ field ] ;
258
- if ( ! deps ) {
264
+ if ( ! deps || typeof deps !== 'object' || Array . isArray ( deps ) ) {
259
265
continue ;
260
266
}
261
267
262
- for ( const depName of Object . keys ( packageJson [ field ] ) ) {
268
+ for ( const depName of Object . keys ( deps ) ) {
263
269
if ( allVersions [ depName ] ) {
264
- packageJson [ field ] [ depName ] = allVersions [ depName ] ;
270
+ deps [ depName ] = allVersions [ depName ] ;
265
271
}
266
272
}
267
273
}
0 commit comments