|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import path from 'path'; |
| 10 | +import url from 'url'; |
| 11 | +import semver from 'semver'; |
| 12 | +import { PackageMap } from '../../../lib/packages.js'; |
| 13 | + |
| 14 | +/** Path to the current directory. */ |
| 15 | +const currentDir = path.dirname(url.fileURLToPath(import.meta.url)); |
| 16 | + |
| 17 | +/** Path to the project directory. */ |
| 18 | +const projectDir = path.join(currentDir, '../../../'); |
| 19 | + |
| 20 | +/** Describes a parsed `package.json` file. */ |
| 21 | +interface PackageJson { |
| 22 | + name?: string; |
| 23 | + peerDependencies?: Record<string, string>; |
| 24 | +} |
| 25 | + |
| 26 | +export async function checkPeerDependencies( |
| 27 | + newVersion: semver.SemVer, |
| 28 | + allPackages: PackageMap, |
| 29 | +): Promise<string[]> { |
| 30 | + const { major, minor } = newVersion; |
| 31 | + const isPrerelease = !!newVersion.prerelease[0]; |
| 32 | + const isMajor = minor === 0; |
| 33 | + |
| 34 | + let expectedFwPeerDep = `^${major}.0.0`; |
| 35 | + if (isMajor && isPrerelease) { |
| 36 | + expectedFwPeerDep = `^${major}.0.0-next.0`; |
| 37 | + } else if (isPrerelease) { |
| 38 | + expectedFwPeerDep = `^${major}.0.0 || ^${major}.${minor}.0-next.0`; |
| 39 | + } |
| 40 | + |
| 41 | + const failures: string[] = []; |
| 42 | + for (const pkgInfo of Object.values(allPackages)) { |
| 43 | + failures.push(...checkPackage(pkgInfo.packageJson, expectedFwPeerDep)); |
| 44 | + } |
| 45 | + |
| 46 | + return failures; |
| 47 | +} |
| 48 | + |
| 49 | +/** Checks the given package and collects errors for the peer dependency ranges. */ |
| 50 | +function checkPackage(pkgJson: PackageJson, expectedFwPeerDep: string): string[] { |
| 51 | + if (pkgJson.peerDependencies === undefined) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + const failures: string[] = []; |
| 56 | + |
| 57 | + for (const [depName, range] of Object.entries(pkgJson.peerDependencies)) { |
| 58 | + // Even though `ng-packagr` might not strictly follow the same release schedules |
| 59 | + // like official Angular packages, we generally expect it to match. It's better |
| 60 | + // flagging it than silently passing pre-checks. The caretaker can always forcibly |
| 61 | + // ignore this check. |
| 62 | + if (!depName.startsWith('@angular/') && depName !== 'ng-packagr') { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (range !== expectedFwPeerDep) { |
| 67 | + failures.push( |
| 68 | + `${pkgJson.name}: Unexpected peer dependency range for "${depName}". Expected: ${expectedFwPeerDep}`, |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return failures; |
| 74 | +} |
0 commit comments