|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +const chalk = require("chalk"); |
| 5 | + |
| 6 | +const debug = require("debug")("check-package-versions"); |
| 7 | + |
| 8 | +function main() { |
| 9 | + const packages = readPackages(); |
| 10 | + const errors = []; |
| 11 | + |
| 12 | + for (const candidateName in packages) { |
| 13 | + for (const dependencyName in packages) { |
| 14 | + errors.push( |
| 15 | + ...checkDependencyVersionRange( |
| 16 | + packages[candidateName], |
| 17 | + packages[dependencyName] |
| 18 | + ) |
| 19 | + ); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + for (const error of errors) { |
| 24 | + console.error(chalk.red("Error:"), error); |
| 25 | + } |
| 26 | + const doneColor = errors.length > 0 ? chalk.red : chalk.green; |
| 27 | + console.error( |
| 28 | + doneColor("Done."), |
| 29 | + `Found ${errors.length} errors for @truffle namespace version dependencies.` |
| 30 | + ); |
| 31 | + |
| 32 | + if (errors.length > 0) { |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function getPackageDirectoryNames() { |
| 38 | + return fs.readdirSync(path.join(__dirname, "..", "packages")); |
| 39 | +} |
| 40 | + |
| 41 | +function readPackages() { |
| 42 | + const packages = getPackageDirectoryNames(); |
| 43 | + const packageSpecs = {}; |
| 44 | + for (const packageName of packages) { |
| 45 | + const packageFilePath = path.join( |
| 46 | + path.resolve(__dirname, "..", "packages", packageName), |
| 47 | + "package.json" |
| 48 | + ); |
| 49 | + const rawJson = fs.readFileSync(packageFilePath, { encoding: "utf8" }); |
| 50 | + packageSpecs[packageName] = JSON.parse(rawJson); |
| 51 | + } |
| 52 | + |
| 53 | + return packageSpecs; |
| 54 | +} |
| 55 | + |
| 56 | +const depTypes = [ |
| 57 | + "dependencies", |
| 58 | + "devDependencies", |
| 59 | + "peerDependencies", |
| 60 | + "optionalDependencies" |
| 61 | +]; |
| 62 | + |
| 63 | +function* checkDependencyVersionRange(candidate, dependency) { |
| 64 | + const name = dependency.name; |
| 65 | + const version = dependency.version; |
| 66 | + |
| 67 | + for (const depType of depTypes) { |
| 68 | + const deps = candidate[depType]; |
| 69 | + if (deps && Object.keys(deps).includes(name)) { |
| 70 | + const rawRange = deps[name]; |
| 71 | + |
| 72 | + if (rawRange !== `^${version}`) { |
| 73 | + yield `Package "${candidate.name}" depends on "${name}@${rawRange}", but range has not been updated for version ${version}`; |
| 74 | + } else { |
| 75 | + debug( |
| 76 | + `${candidate.name} requires ${name}@${version} (${rawRange} is up-to-date for version ${version})` |
| 77 | + ); |
| 78 | + } |
| 79 | + } else { |
| 80 | + debug(`${candidate.name} does not require ${name}`); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +if (require.main === module) { |
| 86 | + main(); |
| 87 | +} |
0 commit comments