diff --git a/scripts/ci/check-dependencies-versions-match.js b/scripts/ci/check-dependencies-versions-match.js index 55b9713d9f6..f43aed6ca3d 100644 --- a/scripts/ci/check-dependencies-versions-match.js +++ b/scripts/ci/check-dependencies-versions-match.js @@ -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'; @@ -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.} expectedDependencies * @param {Array.} packageJsons * @param {Object.} 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 } ); } ); @@ -64,16 +84,19 @@ function fixDependenciesVersions( expectedDependencies, packageJsons, pathMappin /** * @param {Object.} expectedDependencies + * @param {Function} isCkeditor5Package * @param {Array.} 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 ) ); @@ -100,16 +123,21 @@ function getWrongVersionErrorMsg( dependency, name, version, expectedDependencie /** * @param {Array.} packageJsons + * @param {Function} isCkeditor5Package * @return {Object.} 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; }, {} ); @@ -158,3 +186,11 @@ function getPackageJsons( directories ) { return [ packageJsons, nameToPathMappings ]; } + +/** + * @param {Object.} packageJson + * @returns {Object.} + */ +function getDepsAndDevDeps( packageJson ) { + return { ...packageJson.dependencies, ...( packageJson.devDependencies || {} ) }; +}