Skip to content

Commit

Permalink
Merge pull request #15026 from ckeditor/ck/14953
Browse files Browse the repository at this point in the history
Internal: Added `devDependencies` to `check-dependencies-versions-match.js` script. Closes #14953.
  • Loading branch information
pomek authored Sep 20, 2023
2 parents c217e4d + bb8f328 commit fa35555
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions scripts/ci/check-dependencies-versions-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { globSync } = require( 'glob' );
const fs = require( 'fs-extra' );
const upath = require( 'upath' );
const { execSync } = require( 'child_process' );
const isCKEditor5PackageFactory = require( '../release/utils/isckeditor5packagefactory' );

const versionsCache = {};
const shouldFix = process.argv[ 2 ] === '--fix';
Expand All @@ -32,29 +33,48 @@ const [ packageJsons, pathMappings ] = getPackageJsons( [
'external/ckeditor5-commercial/package.json'
] );

const expectedDependencies = getExpectedDepsVersions( packageJsons );
main().catch( err => {
console.error( err );

if ( shouldFix ) {
fixDependenciesVersions( expectedDependencies, packageJsons, pathMappings );
} else {
checkDependenciesMatch( expectedDependencies, packageJsons );
process.exit( 1 );
} );

async function main() {
const isCkeditor5Package = await isCKEditor5PackageFactory();
const expectedDependencies = getExpectedDepsVersions( packageJsons, isCkeditor5Package );

if ( shouldFix ) {
fixDependenciesVersions( expectedDependencies, packageJsons, pathMappings, isCkeditor5Package );
} else {
checkDependenciesMatch( expectedDependencies, packageJsons, isCkeditor5Package );
}
}

/**
* @param {Object.<String, String>} expectedDependencies
* @param {Array.<Object>} packageJsons
* @param {Object.<String, String>} pathMappings
* @param {Function} isCkeditor5Package
*/
function fixDependenciesVersions( expectedDependencies, packageJsons, pathMappings ) {
function fixDependenciesVersions( expectedDependencies, packageJsons, pathMappings, isCkeditor5Package ) {
packageJsons
.filter( packageJson => packageJson.dependencies )
.forEach( packageJson => {
Object.entries( packageJson.dependencies )
.forEach( ( [ dependency, version ] ) => {
if ( version !== expectedDependencies[ dependency ] ) {
packageJson.dependencies[ dependency ] = expectedDependencies[ dependency ];
}
} );
for ( const [ dependency, version ] of Object.entries( packageJson.dependencies ) ) {
if ( !isCkeditor5Package( dependency ) || version === expectedDependencies[ dependency ] ) {
continue;
}

packageJson.dependencies[ dependency ] = expectedDependencies[ dependency ];
}

for ( const [ dependency, version ] of Object.entries( packageJson.devDependencies ) ) {
if ( !isCkeditor5Package( dependency ) || version === expectedDependencies[ dependency ] ) {
continue;
}

packageJson.devDependencies[ dependency ] = expectedDependencies[ dependency ];
}

fs.writeJsonSync( pathMappings[ packageJson.name ], packageJson, { spaces: 2 } );
} );
Expand All @@ -64,16 +84,19 @@ function fixDependenciesVersions( expectedDependencies, packageJsons, pathMappin

/**
* @param {Object.<String, String>} expectedDependencies
* @param {Function} isCkeditor5Package
* @param {Array.<Object>} packageJsons
*/
function checkDependenciesMatch( expectedDependencies, packageJsons ) {
function checkDependenciesMatch( expectedDependencies, packageJsons, isCkeditor5Package ) {
const errors = packageJsons
.filter( packageJson => packageJson.dependencies )
.flatMap( packageJson => Object.entries( packageJson.dependencies )
.flatMap( packageJson => Object.entries( getDepsAndDevDeps( packageJson ) )
.map( ( [ dependency, version ] ) => {
if ( version !== expectedDependencies[ dependency ] ) {
return getWrongVersionErrorMsg( dependency, packageJson.name, version, expectedDependencies );
if ( !isCkeditor5Package( dependency ) || version === expectedDependencies[ dependency ] ) {
return '';
}

return getWrongVersionErrorMsg( dependency, packageJson.name, version, expectedDependencies );
} )
.filter( Boolean )
);
Expand All @@ -100,16 +123,21 @@ function getWrongVersionErrorMsg( dependency, name, version, expectedDependencie

/**
* @param {Array.<Object>} packageJsons
* @param {Function} isCkeditor5Package
* @return {Object.<String, String>} expectedDependencies
*/
function getExpectedDepsVersions( packageJsons ) {
function getExpectedDepsVersions( packageJsons, isCkeditor5Package ) {
return packageJsons
.map( packageJson => packageJson.dependencies )
.map( packageJson => getDepsAndDevDeps( packageJson ) )
.filter( Boolean )
.reduce( ( expectedDependencies, dependencies ) => {
Object.entries( dependencies ).forEach( ( [ dependency, version ] ) => {
for ( const [ dependency, version ] of Object.entries( dependencies ) ) {
if ( !isCkeditor5Package( dependency ) ) {
continue;
}

expectedDependencies[ dependency ] = getNewestVersion( dependency, version, expectedDependencies[ dependency ] );
} );
}

return expectedDependencies;
}, {} );
Expand Down Expand Up @@ -158,3 +186,11 @@ function getPackageJsons( directories ) {

return [ packageJsons, nameToPathMappings ];
}

/**
* @param {Object.<String, String>} packageJson
* @returns {Object.<String, String>}
*/
function getDepsAndDevDeps( packageJson ) {
return { ...packageJson.dependencies, ...( packageJson.devDependencies || {} ) };
}

0 comments on commit fa35555

Please sign in to comment.