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