Skip to content

Commit 68ac4ab

Browse files
committed
Fix Grunt task upgrading certificate dependency
Because an exact version is pinned for `composer/ca-bundle`, the `composer update` command cannot update the dependency to the latest version. The command would not work for an individual dependency anyway due to the fact that there is no `composer.lock` file generated.
1 parent c15303c commit 68ac4ab

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

Gruntfile.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var webpackConfig = require( './webpack.config' );
55
var installChanged = require( 'install-changed' );
66
var json2php = require( 'json2php' );
7+
const fs = require("fs");
78

89
module.exports = function(grunt) {
910
var path = require('path'),
@@ -1584,10 +1585,69 @@ module.exports = function(grunt) {
15841585
'usebanner'
15851586
] );
15861587

1587-
grunt.registerTask( 'certificates:update', 'Updates the Composer package responsible for root certificate updates.', function() {
1588+
grunt.registerTask( 'certificates:upgrade-package', 'Upgrades the package responsible for supplying the certificate authority certificate store bundled with WordPress.', function() {
15881589
var done = this.async();
15891590
var flags = this.flags;
1590-
var args = [ 'update' ];
1591+
var spawn = require( 'child_process' ).spawnSync;
1592+
var fs = require( 'fs' );
1593+
1594+
// Ensure that `composer update` has been run and the dependency is installed.
1595+
if ( ! fs.existsSync( 'vendor' ) || ! fs.existsSync( 'vendor/composer' ) || ! fs.existsSync( 'vendor/composer/ca-bundle' ) ) {
1596+
grunt.log.error( 'composer/ca-bundle dependency is missing. Please run `composer update` before attempting to upgrade the certificate bundle.' );
1597+
done( false );
1598+
return;
1599+
}
1600+
1601+
/*
1602+
* Because the `composer/ca-bundle` is pinned to an exact version to ensure upgrades are applied intentionally,
1603+
* the `composer update` command will not upgrade the dependency. Instead, `composer require` must be called,
1604+
* but the specific version being upgraded to must be known and passed to the command.
1605+
*/
1606+
var outdatedResult = spawn( 'composer', [ 'outdated', 'composer/ca-bundle', '--format=json' ] );
1607+
1608+
if ( outdatedResult.status !== 0 ) {
1609+
grunt.log.error( 'Failed to get the package information for composer/ca-bundle.' );
1610+
done( false );
1611+
return;
1612+
}
1613+
1614+
var packageInfo;
1615+
try {
1616+
var stdout = outdatedResult.stdout.toString().trim();
1617+
if ( ! stdout ) {
1618+
grunt.log.writeln( 'The latest version is already installed.' );
1619+
done( true );
1620+
return;
1621+
}
1622+
packageInfo = JSON.parse( stdout );
1623+
} catch ( e ) {
1624+
grunt.log.error( 'Failed to parse the package information for composer/ca-bundle.' );
1625+
done( false );
1626+
return;
1627+
}
1628+
1629+
// Check for the version information needed to perform the necessary comparisons.
1630+
if ( ! packageInfo.versions || ! packageInfo.versions[0] || ! packageInfo.latest ) {
1631+
grunt.log.error( 'Could not determine version information for composer/ca-bundle.' );
1632+
done( false );
1633+
return;
1634+
}
1635+
1636+
var currentVersion = packageInfo.versions[0];
1637+
var latestVersion = packageInfo.latest;
1638+
1639+
// Compare versions to ensure we actually need to update
1640+
if ( currentVersion === latestVersion ) {
1641+
grunt.log.writeln( 'The latest version is already installed: ' + latestVersion + '.' );
1642+
done( true );
1643+
return;
1644+
}
1645+
1646+
grunt.log.writeln( 'Installed version: ' + currentVersion );
1647+
grunt.log.writeln( 'New version found: ' + latestVersion );
1648+
1649+
// Upgrade to the latest version and change the pinned version in composer.json.
1650+
var args = [ 'require', 'composer/ca-bundle:' + latestVersion, '--dev' ];
15911651

15921652
grunt.util.spawn( {
15931653
cmd: 'composer',
@@ -1597,6 +1657,7 @@ module.exports = function(grunt) {
15971657
if ( flags.error && error ) {
15981658
done( false );
15991659
} else {
1660+
grunt.log.writeln( 'Successfully updated composer/ca-bundle to ' + latestVersion );
16001661
done( true );
16011662
}
16021663
} );
@@ -1607,7 +1668,7 @@ module.exports = function(grunt) {
16071668
] );
16081669

16091670
grunt.registerTask( 'certificates:upgrade', [
1610-
'certificates:update',
1671+
'certificates:upgrade-package',
16111672
'copy:certificates',
16121673
'build:certificates'
16131674
] );

0 commit comments

Comments
 (0)