From 771a975e8bf0177240870ba04f25137d0ad973d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Mon, 24 Feb 2020 17:49:16 +0100 Subject: [PATCH] Scripts: Ensure packages-update works with missing dependencies (#20408) --- packages/scripts/CHANGELOG.md | 4 +++ packages/scripts/scripts/packages-update.js | 30 ++++++++++----------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index 3334ccfe8676d5..7471b1cea78397 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -1,5 +1,9 @@ ## Master +### Bug Fixes + +- Ensure `packages-update` work when `dependencies` or `devDependencies` are missing in the `package.json` file ([#20408](https://github.com/WordPress/gutenberg/pull/20408)). + ## 7.1.0 (2020-02-10) ### Bug Fixes diff --git a/packages/scripts/scripts/packages-update.js b/packages/scripts/scripts/packages-update.js index 6ebde42cc7b618..5bf8ce531d5822 100644 --- a/packages/scripts/scripts/packages-update.js +++ b/packages/scripts/scripts/packages-update.js @@ -15,9 +15,9 @@ function readJSONFile( fileName ) { return JSON.parse( data ); } -function getWordPressPackages( packageJSON ) { - return Object.keys( packageJSON.dependencies ) - .concat( Object.keys( packageJSON.devDependencies ) ) +function getWordPressPackages( { dependencies = {}, devDependencies = {} } ) { + return Object.keys( dependencies ) + .concat( Object.keys( devDependencies ) ) .filter( ( packageName ) => packageName.startsWith( WORDPRESS_PACKAGES_PREFIX ) ); @@ -26,19 +26,17 @@ function getWordPressPackages( packageJSON ) { function getPackageVersionDiff( initialPackageJSON, finalPackageJSON ) { const diff = [ 'dependencies', 'devDependencies' ].reduce( ( result, keyPackageJSON ) => { - return Object.keys( finalPackageJSON[ keyPackageJSON ] ).reduce( - ( _result, dependency ) => { - const initial = - initialPackageJSON[ keyPackageJSON ][ dependency ]; - const final = - finalPackageJSON[ keyPackageJSON ][ dependency ]; - if ( initial !== final ) { - _result.push( { dependency, initial, final } ); - } - return _result; - }, - result - ); + return Object.keys( + finalPackageJSON[ keyPackageJSON ] || {} + ).reduce( ( _result, dependency ) => { + const initial = + initialPackageJSON[ keyPackageJSON ][ dependency ]; + const final = finalPackageJSON[ keyPackageJSON ][ dependency ]; + if ( initial !== final ) { + _result.push( { dependency, initial, final } ); + } + return _result; + }, result ); }, [] );