Mwalker has submitted this change and it was merged. Change subject: Only unify top-level package.json and let npm resolve any conflicts found. ......................................................................
Only unify top-level package.json and let npm resolve any conflicts found. Instead of trying to use semver to unify different package versions, just use the `and` operator (in semver, a space) to combine the versions and let npm figure it out. Similarly, don't try to unify *all* the package.json files; just do the top-level ones and let `npm dedupe` handle the recursion. Change-Id: I968afa24edd00c5574317c974749cd54a792ac3f --- M unify-package-json.js 1 file changed, 39 insertions(+), 58 deletions(-) Approvals: Mwalker: Verified; Looks good to me, approved diff --git a/unify-package-json.js b/unify-package-json.js index 998cb1e..6791543 100755 --- a/unify-package-json.js +++ b/unify-package-json.js @@ -14,6 +14,7 @@ var child_process = require( 'child_process' ), semver = require( 'semver' ), fs = require( 'fs' ), + path = require( 'path' ), util = require( 'util' ); try { @@ -23,14 +24,13 @@ } function findPackageJson() { - return new Promise( function( resolve, reject ) { - child_process.exec( - 'find . -path ./node_modules -prune -o -name package.json -print', - function( error, stdout, stderr ) { - files = stdout.trim().split( '\n' ); - resolve( files ); - } - ) + var readdir = Promise.promisify(fs.readdir, fs); + return readdir( __dirname ).then( function( files ) { + return files.map( function( f ) { + return path.join( __dirname, f, "package.json" ); + } ).filter( function ( f ) { + return fs.existsSync( f ); + } ); } ); } @@ -45,64 +45,45 @@ function buildDependencies( packageObjs ) { var glodeps, optdeps, glodevdeps; var iterate = function iterate( key, arrayObj ) { - var unifiedDeps = {}; + var unifiedDeps = new Map(); arrayObj.forEach( function( el ) { var name = el.name, - packdeps = el[key], - pkg, glover, pkgver; + packdeps = el[key] || {}; - for ( pkg in packdeps ) { - if ( !packdeps.hasOwnProperty( pkg ) ) { - continue; + Object.keys(packdeps).forEach( function( pkg ) { + var pkgver = packdeps[pkg]; + if ( !unifiedDeps.has( pkg ) ) { + unifiedDeps.set( pkg, new Map() ); } - - if ( pkg in unifiedDeps ) { - // Check to see if the versions are compatible - glover = unifiedDeps[pkg][0]; - pkgver = packdeps[pkg]; - - // Strict equality is nice; they need the same thing - if ( glover === pkgver ) { - continue; - } - - // If either is an approximate version (major / minor match) strip - // the tidle so semver can process and then see if the other requirement matches - if ( glover[0] === '~' && semver.satisfies( glover.substr( 1 ), pkgver ) ) { - continue; - } - if ( pkgver[0] === '~' && semver.satisfies( pkgver.substr( 1 ), glover ) ) { - continue; - } - - // TODO: Handle greater than / less than. - - console.error( - util.format( - 'Could not reconcile common dependency on %s. %s requires %s where %s requires %s.', - pkg, - unifiedDeps[pkg][1], glover, - name, pkgver - ) - ); - - } else { - // Not already used, just add it - unifiedDeps[pkg] = [packdeps[pkg], name]; + var vermap = unifiedDeps.get( pkg ); + if ( !vermap.has( pkgver ) ) { + vermap.set( pkgver, new Set() ); } + vermap.get( pkgver ).add( name ); + } ); + } ); + // Go through and combine versions for a given package with the + // "and" operator (which for semver is a space) and let npm figure + // out how to satisfy the conflict. But emit diagnostics on + // stderr to help us debug this if necessary. + var result = {}; + unifiedDeps.forEach( function( vermap, pkg ) { + result[ pkg ] = Array.from( vermap.keys() ).join( ' ' ); + var who = Array.from( vermap.values() ); + if ( who.length > 1 ) { + console.warn( + util.format( + '* Could not reconcile common dependency on %s. (%s)', + pkg, + who.map( function( s ) { + return Array.from( s.values() ).join(' '); + }).join(', ') + ) + ); } } ); - - // Clean the metadata we left - for ( dep in unifiedDeps ) { - if ( !unifiedDeps.hasOwnProperty( dep ) ) { - continue; - } - unifiedDeps[dep] = unifiedDeps[dep][0]; - } - - return unifiedDeps; + return result; }; console.info( 'Unifying runtime dependencies' ); -- To view, visit https://gerrit.wikimedia.org/r/138622 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I968afa24edd00c5574317c974749cd54a792ac3f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/ocg-collection Gerrit-Branch: wmf-deploy Gerrit-Owner: Cscott <canan...@wikimedia.org> Gerrit-Reviewer: Mwalker <mwal...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits