|
| 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { createRequire } from 'node:module'; |
| 10 | + |
| 11 | +/** |
| 12 | + * A custom error class to represent missing dependency errors. |
| 13 | + * This is used to avoid printing a stack trace for this expected error. |
| 14 | + */ |
| 15 | +export class MissingDependenciesError extends Error { |
| 16 | + constructor(message: string) { |
| 17 | + super(message); |
| 18 | + this.name = 'MissingDependenciesError'; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +type Resolver = (packageName: string) => string; |
| 23 | + |
| 24 | +export class DependencyChecker { |
| 25 | + private readonly resolver: Resolver; |
| 26 | + private readonly missingDependencies = new Set<string>(); |
| 27 | + |
| 28 | + constructor(projectSourceRoot: string) { |
| 29 | + this.resolver = createRequire(projectSourceRoot + '/').resolve; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Checks if a package is installed. |
| 34 | + * @param packageName The name of the package to check. |
| 35 | + * @returns True if the package is found, false otherwise. |
| 36 | + */ |
| 37 | + private isInstalled(packageName: string): boolean { |
| 38 | + try { |
| 39 | + this.resolver(packageName); |
| 40 | + |
| 41 | + return true; |
| 42 | + } catch { |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Verifies that a package is installed and adds it to a list of missing |
| 49 | + * dependencies if it is not. |
| 50 | + * @param packageName The name of the package to check. |
| 51 | + */ |
| 52 | + check(packageName: string): void { |
| 53 | + if (!this.isInstalled(packageName)) { |
| 54 | + this.missingDependencies.add(packageName); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Verifies that at least one of a list of packages is installed. If none are |
| 60 | + * installed, a custom error message is added to the list of errors. |
| 61 | + * @param packageNames An array of package names to check. |
| 62 | + * @param customErrorMessage The error message to use if none of the packages are found. |
| 63 | + */ |
| 64 | + checkAny(packageNames: string[], customErrorMessage: string): void { |
| 65 | + if (packageNames.every((name) => !this.isInstalled(name))) { |
| 66 | + // This is a custom error, so we add it directly. |
| 67 | + // Using a Set avoids duplicate custom messages. |
| 68 | + this.missingDependencies.add(customErrorMessage); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Throws a `MissingDependenciesError` if any dependencies were found to be missing. |
| 74 | + * The error message is a formatted list of all missing packages. |
| 75 | + */ |
| 76 | + report(): void { |
| 77 | + if (this.missingDependencies.size === 0) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + let message = 'The following packages are required but were not found:\n'; |
| 82 | + for (const name of this.missingDependencies) { |
| 83 | + message += ` - ${name}\n`; |
| 84 | + } |
| 85 | + message += 'Please install the missing packages and rerun the test command.'; |
| 86 | + |
| 87 | + throw new MissingDependenciesError(message); |
| 88 | + } |
| 89 | +} |
0 commit comments