|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Check versions of in-repo packages to make sure they match with the package.json version. |
| 4 | +// This is to prevent inadvertent updates of dependency versions for packages which are symlinked |
| 5 | +// by lerna. The dependency versions of these packages should only ever be updated in the release branch. |
| 6 | + |
| 7 | +const execa = require('execa'); |
| 8 | +const path = require('path'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Create a function which can run lerna commands |
| 12 | + * @param lernaPath {string} path to lerna binary |
| 13 | + * @returns {function(string, string[], Object.<string, string>): Promise<string>} |
| 14 | + */ |
| 15 | +function getLernaRunner(lernaPath) { |
| 16 | + return async (command, args = [], options = {}) => { |
| 17 | + const { stdout } = await execa(lernaPath, [command, ...args], options); |
| 18 | + return stdout; |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Get information on the modules in this repo that are managed by lerna. |
| 24 | + * @param lerna {function} |
| 25 | + * @returns {Promise<{path: *, name: *, deps: *, version: *}[]>} |
| 26 | + */ |
| 27 | +async function getLernaManagedModules(lerna) { |
| 28 | + const depGraph = JSON.parse(await lerna('list', ['--loglevel', 'silent', '--graph', '--all'])); |
| 29 | + const managedModules = JSON.parse(await lerna('list', ['--loglevel', 'silent', '--json', '--all'])); |
| 30 | + const managedModuleNames = managedModules.map(({ name }) => name); |
| 31 | + return Object.entries(depGraph).map(([name, deps]) => { |
| 32 | + const mod = managedModules.find((mod) => mod.name === name); |
| 33 | + return { |
| 34 | + name, |
| 35 | + deps: deps.filter((d) => managedModuleNames.includes(d)), |
| 36 | + path: mod.location, |
| 37 | + version: mod.version, |
| 38 | + }; |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Build a dictionary from package name to the expected version of that package. |
| 44 | + * @param modules |
| 45 | + * @returns {Object.<string, string>} |
| 46 | + */ |
| 47 | +function getExpectedVersions(modules) { |
| 48 | + return Object.values(modules).reduce((acc, mod) => { |
| 49 | + return Object.assign(acc, { [mod.name]: mod.version }); |
| 50 | + }, {}); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * For the module at `modPath`, get the version of the dependency `depName`. |
| 55 | + * If the version is prefixed with a carat or tilde, it will be stripped. |
| 56 | + * @param modPath {string} |
| 57 | + * @param depName {string} |
| 58 | + * @returns {string | undefined} |
| 59 | + */ |
| 60 | +function getDependencyVersion(modPath, depName) { |
| 61 | + const packageJsonPath = path.join(modPath, 'package.json'); |
| 62 | + const { |
| 63 | + dependencies = {}, |
| 64 | + devDependencies = {}, |
| 65 | + optionalDependencies = {}, |
| 66 | + peerDependencies = {}, |
| 67 | + } = require(packageJsonPath); |
| 68 | + |
| 69 | + const deps = { ...dependencies, ...devDependencies, ...optionalDependencies, ...peerDependencies }; |
| 70 | + if (deps[depName]) { |
| 71 | + const matches = deps[depName].match(/^([^~])?(.*)$/); |
| 72 | + return matches[matches.length - 1]; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function main() { |
| 77 | + const { stdout: lernaBinary } = await execa('yarn', ['bin', 'lerna'], { cwd: process.cwd() }); |
| 78 | + |
| 79 | + const lerna = getLernaRunner(lernaBinary); |
| 80 | + |
| 81 | + const modules = await getLernaManagedModules(lerna); |
| 82 | + const expectedVersions = getExpectedVersions(modules); |
| 83 | + |
| 84 | + let exitCode = 0; |
| 85 | + |
| 86 | + for (const mod of modules) { |
| 87 | + for (const dep of mod.deps) { |
| 88 | + const depVersion = getDependencyVersion(mod.path, dep); |
| 89 | + if (depVersion && depVersion !== expectedVersions[dep]) { |
| 90 | + // Handle pre-release versions by checking if the base version matches |
| 91 | + const baseDepVersion = depVersion.split('-')[0]; |
| 92 | + const baseExpectedVersion = expectedVersions[dep].split('-')[0]; |
| 93 | + |
| 94 | + if (baseDepVersion !== baseExpectedVersion) { |
| 95 | + console.log( |
| 96 | + `error: expected lerna-managed module ${mod.name} to depend on package ${dep} using version ${expectedVersions[dep]}, but found version ${depVersion} instead` |
| 97 | + ); |
| 98 | + exitCode = 1; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return exitCode; |
| 105 | +} |
| 106 | + |
| 107 | +main() |
| 108 | + .then(process.exit) |
| 109 | + .catch((e) => console.error(e)); |
0 commit comments