@@ -26,12 +26,16 @@ import {
26
26
} from '@angular-devkit/schematics' ;
27
27
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
28
28
import { AppConfig , CliConfig } from '../../utility/config' ;
29
- import { latestVersions } from '../../utility/latest-versions' ;
30
29
import {
31
- appendPropertyInAstObject ,
30
+ NodeDependency ,
31
+ NodeDependencyType ,
32
+ addPackageJsonDependency ,
33
+ } from '../../utility/dependencies' ;
34
+ import {
32
35
appendValueInAstArray ,
33
36
findPropertyInAstObject ,
34
- } from './json-utils' ;
37
+ } from '../../utility/json-utils' ;
38
+ import { latestVersions } from '../../utility/latest-versions' ;
35
39
36
40
const defaults = {
37
41
appRoot : 'src' ,
@@ -649,49 +653,13 @@ function updateSpecTsConfig(config: CliConfig): Rule {
649
653
650
654
function updatePackageJson ( config : CliConfig ) {
651
655
return ( host : Tree , context : SchematicContext ) => {
652
- const pkgPath = '/package.json' ;
653
- const buffer = host . read ( pkgPath ) ;
654
- if ( buffer == null ) {
655
- throw new SchematicsException ( 'Could not read package.json' ) ;
656
- }
657
- const pkgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Strict ) ;
658
-
659
- if ( pkgAst . kind != 'object' ) {
660
- throw new SchematicsException ( 'Error reading package.json' ) ;
661
- }
662
-
663
- const devDependenciesNode = findPropertyInAstObject ( pkgAst , 'devDependencies' ) ;
664
- if ( devDependenciesNode && devDependenciesNode . kind != 'object' ) {
665
- throw new SchematicsException ( 'Error reading package.json; devDependency is not an object.' ) ;
666
- }
667
-
668
- const recorder = host . beginUpdate ( pkgPath ) ;
669
- const depName = '@angular-devkit/build-angular' ;
670
- if ( ! devDependenciesNode ) {
671
- // Haven't found the devDependencies key, add it to the root of the package.json.
672
- appendPropertyInAstObject ( recorder , pkgAst , 'devDependencies' , {
673
- [ depName ] : latestVersions . DevkitBuildAngular ,
674
- } ) ;
675
- } else {
676
- // Check if there's a build-angular key.
677
- const buildAngularNode = findPropertyInAstObject ( devDependenciesNode , depName ) ;
678
-
679
- if ( ! buildAngularNode ) {
680
- // No build-angular package, add it.
681
- appendPropertyInAstObject (
682
- recorder ,
683
- devDependenciesNode ,
684
- depName ,
685
- latestVersions . DevkitBuildAngular ,
686
- ) ;
687
- } else {
688
- const { end, start } = buildAngularNode ;
689
- recorder . remove ( start . offset , end . offset - start . offset ) ;
690
- recorder . insertRight ( start . offset , JSON . stringify ( latestVersions . DevkitBuildAngular ) ) ;
691
- }
692
- }
693
-
694
- host . commitUpdate ( recorder ) ;
656
+ const dependency : NodeDependency = {
657
+ type : NodeDependencyType . Dev ,
658
+ name : '@angular-devkit/build-angular' ,
659
+ version : latestVersions . DevkitBuildAngular ,
660
+ overwrite : true ,
661
+ } ;
662
+ addPackageJsonDependency ( host , dependency ) ;
695
663
696
664
context . addTask ( new NodePackageInstallTask ( {
697
665
packageManager : config . packageManager === 'default' ? undefined : config . packageManager ,
0 commit comments