diff --git a/package.json b/package.json index 8710a2157..88b33bd6c 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "cwtree": "node scripts/check-working-tree", "cylock": "node scripts/check-yarn-lock", "deploy:site": "node site/deploy-site", + "fix-versions": "node scripts/fix-versions", "globalize": "node scripts/globalize", "lint": "npm-run-all lint:*", "lint:packages": "node scripts/monorun --parallel lint", diff --git a/scripts/fix-versions.js b/scripts/fix-versions.js new file mode 100644 index 000000000..be0cf8f4b --- /dev/null +++ b/scripts/fix-versions.js @@ -0,0 +1,63 @@ +const { execSync } = require('child_process'); +const { writeFileSync } = require('fs'); +const { join } = require('path'); +const semver = require('semver'); + +const allPackages = JSON.parse(execSync( + 'npx lerna ls --all --json', + {cwd: join(__dirname, '..'), stdio: ['pipe', 'pipe', 'ignore']} +).toString().trim()); + +const allPackagesDict = {}; + +allPackages.forEach(pkg => { + pkg.json = require(join(pkg.location, 'package.json')); + allPackagesDict[pkg.name] = pkg; +}); + +allPackages.forEach(pkg => { + function updateMismatched(depKind, [depName, depRange]) { + const dep = allPackagesDict[depName]; + if (dep) { + const depVersion = dep.version; + if (!semver.satisfies(depVersion, depRange)) { + pkg.json[depKind][depName] = `^${depVersion}`; + pkg.updated = true; + console.warn([ + `range specifier for ${depName} was set to ^${depVersion} in`, + `${join(pkg.location, 'package.json')} based on "version" in`, + `${join(dep.location, 'package.json')}` + ].join(' ')); + } + } + } + + if (pkg.json.dependencies) { + Object.entries(pkg.json.dependencies).forEach( + updateMismatched.bind({}, 'dependencies') + ); + } + if (pkg.json.devDependencies) { + Object.entries(pkg.json.devDependencies).forEach( + updateMismatched.bind({}, 'devDependencies') + ); + } +}); + +let updated; +allPackages.forEach(pkg => { + if (pkg.updated) { + updated = true; + writeFileSync( + join(pkg.location, 'package.json'), + JSON.stringify(pkg.json, null, 2) + ); + } +}); + +if (updated) { + execSync( + 'yarn reboot:full && yarn cylock', + {cwd: join(__dirname, '..'), stdio: 'inherit'} + ); +}