@@ -27,7 +27,12 @@ import {
27
27
fetchPackageManifest ,
28
28
fetchPackageMetadata ,
29
29
} from '../utilities/package-metadata' ;
30
- import { PackageTreeNode , findNodeDependencies , readPackageTree } from '../utilities/package-tree' ;
30
+ import {
31
+ PackageTreeNode ,
32
+ findPackageJson ,
33
+ getProjectDependencies ,
34
+ readPackageJson ,
35
+ } from '../utilities/package-tree' ;
31
36
import { Schema as UpdateCommandSchema } from './update' ;
32
37
33
38
const npa = require ( 'npm-package-arg' ) as ( selector : string ) => PackageIdentifier ;
@@ -377,15 +382,14 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
377
382
378
383
this . logger . info ( 'Collecting installed dependencies...' ) ;
379
384
380
- const packageTree = await readPackageTree ( this . workspace . root ) ;
381
- const rootDependencies = findNodeDependencies ( packageTree ) ;
385
+ const rootDependencies = await getProjectDependencies ( this . workspace . root ) ;
382
386
383
- this . logger . info ( `Found ${ Object . keys ( rootDependencies ) . length } dependencies.` ) ;
387
+ this . logger . info ( `Found ${ rootDependencies . size } dependencies.` ) ;
384
388
385
389
if ( options . all ) {
386
390
// 'all' option and a zero length packages have already been checked.
387
391
// Add all direct dependencies to be updated
388
- for ( const dep of Object . keys ( rootDependencies ) ) {
392
+ for ( const dep of rootDependencies . keys ( ) ) {
389
393
const packageIdentifier = npa ( dep ) ;
390
394
if ( options . next ) {
391
395
packageIdentifier . fetchSpec = 'next' ;
@@ -400,7 +404,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
400
404
next : options . next || false ,
401
405
verbose : options . verbose || false ,
402
406
packageManager : this . packageManager ,
403
- packages : options . all ? Object . keys ( rootDependencies ) : [ ] ,
407
+ packages : options . all ? rootDependencies . keys ( ) : [ ] ,
404
408
} ) ;
405
409
406
410
return success ? 0 : 1 ;
@@ -424,8 +428,9 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
424
428
}
425
429
426
430
const packageName = packages [ 0 ] . name ;
427
- const packageDependency = rootDependencies [ packageName ] ;
428
- let packageNode = packageDependency && packageDependency . node ;
431
+ const packageDependency = rootDependencies . get ( packageName ) ;
432
+ let packagePath = packageDependency ?. path ;
433
+ let packageNode = packageDependency ?. package ;
429
434
if ( packageDependency && ! packageNode ) {
430
435
this . logger . error ( 'Package found in package.json but is not installed.' ) ;
431
436
@@ -434,19 +439,20 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
434
439
// Allow running migrations on transitively installed dependencies
435
440
// There can technically be nested multiple versions
436
441
// TODO: If multiple, this should find all versions and ask which one to use
437
- const child = packageTree . children . find ( c => c . name === packageName ) ;
438
- if ( child ) {
439
- packageNode = child ;
442
+ const packageJson = findPackageJson ( this . workspace . root , packageName ) ;
443
+ if ( packageJson ) {
444
+ packagePath = path . dirname ( packageJson ) ;
445
+ packageNode = await readPackageJson ( packagePath ) ;
440
446
}
441
447
}
442
448
443
- if ( ! packageNode ) {
449
+ if ( ! packageNode || ! packagePath ) {
444
450
this . logger . error ( 'Package is not installed.' ) ;
445
451
446
452
return 1 ;
447
453
}
448
454
449
- const updateMetadata = packageNode . package [ 'ng-update' ] ;
455
+ const updateMetadata = packageNode [ 'ng-update' ] ;
450
456
let migrations = updateMetadata && updateMetadata . migrations ;
451
457
if ( migrations === undefined ) {
452
458
this . logger . error ( 'Package does not provide migrations.' ) ;
@@ -477,14 +483,14 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
477
483
}
478
484
479
485
// Check if it is a package-local location
480
- const localMigrations = path . join ( packageNode . path , migrations ) ;
486
+ const localMigrations = path . join ( packagePath , migrations ) ;
481
487
if ( fs . existsSync ( localMigrations ) ) {
482
488
migrations = localMigrations ;
483
489
} else {
484
490
// Try to resolve from package location.
485
491
// This avoids issues with package hoisting.
486
492
try {
487
- migrations = require . resolve ( migrations , { paths : [ packageNode . path ] } ) ;
493
+ migrations = require . resolve ( migrations , { paths : [ packagePath ] } ) ;
488
494
} catch ( e ) {
489
495
if ( e . code === 'MODULE_NOT_FOUND' ) {
490
496
this . logger . error ( 'Migrations for package were not found.' ) ;
@@ -513,7 +519,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
513
519
}
514
520
515
521
const migrationRange = new semver . Range (
516
- '>' + from + ' <=' + ( options . to || packageNode . package . version ) ,
522
+ '>' + from + ' <=' + ( options . to || packageNode . version ) ,
517
523
) ;
518
524
519
525
success = await this . executeMigrations (
@@ -529,7 +535,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
529
535
packageName === '@angular/core'
530
536
&& options . from
531
537
&& + options . from . split ( '.' ) [ 0 ] < 9
532
- && ( options . to || packageNode . package . version ) . split ( '.' ) [ 0 ] === '9'
538
+ && ( options . to || packageNode . version ) . split ( '.' ) [ 0 ] === '9'
533
539
) {
534
540
this . logger . info ( NG_VERSION_9_POST_MSG ) ;
535
541
}
@@ -547,8 +553,8 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
547
553
548
554
// Validate packages actually are part of the workspace
549
555
for ( const pkg of packages ) {
550
- const node = rootDependencies [ pkg . name ] && rootDependencies [ pkg . name ] . node ;
551
- if ( ! node ) {
556
+ const node = rootDependencies . get ( pkg . name ) ;
557
+ if ( ! node ?. package ) {
552
558
this . logger . error ( `Package '${ pkg . name } ' is not a dependency.` ) ;
553
559
554
560
return 1 ;
@@ -627,7 +633,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
627
633
return 1 ;
628
634
}
629
635
630
- if ( manifest . version === node . package . version ) {
636
+ if ( manifest . version === node . package ? .version ) {
631
637
this . logger . info ( `Package '${ packageName } ' is already up to date.` ) ;
632
638
continue ;
633
639
}
0 commit comments