Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions packages/angular/build/src/utils/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,43 @@ import { createRequire } from 'node:module';
import { SemVer, satisfies } from 'semver';

export function assertCompatibleAngularVersion(projectRoot: string): void | never {
let angularCliPkgJson;
let angularPkgJson;

// Create a custom require function for ESM compliance.
// NOTE: The trailing slash is significant.
const projectRequire = createRequire(projectRoot + '/');

try {
const angularPackagePath = projectRequire.resolve('@angular/core/package.json');

angularPkgJson = projectRequire(angularPackagePath);
angularPkgJson = projectRequire('@angular/core/package.json');
} catch {
console.error('You seem to not be depending on "@angular/core". This is an error.');
console.error(
'Error: It appears that "@angular/core" is missing as a dependency. Please ensure it is included in your project.',
);

process.exit(2);
}

if (!(angularPkgJson && angularPkgJson['version'])) {
if (!angularPkgJson?.['version']) {
console.error(
'Cannot determine versions of "@angular/core".\n' +
'This likely means your local installation is broken. Please reinstall your packages.',
'Error: Unable to determine the versions of "@angular/core".\n' +
'This likely indicates a corrupted local installation. Please try reinstalling your packages.',
);

process.exit(2);
}

try {
const angularCliPkgPath = projectRequire.resolve('@angular/cli/package.json');
angularCliPkgJson = projectRequire(angularCliPkgPath);
if (!(angularCliPkgJson && angularCliPkgJson['version'])) {
return;
}
} catch {
// Not using @angular-devkit/build-angular with @angular/cli is ok too.
// In this case we don't provide as many version checks.
return;
}

if (angularCliPkgJson['version'] === '0.0.0' || angularPkgJson['version'] === '0.0.0') {
// Internal CLI testing version or integration testing in the angular/angular
// repository with the generated development @angular/core npm package which is versioned "0.0.0".
const supportedAngularSemver = '0.0.0-ANGULAR-FW-PEER-DEP';
if (supportedAngularSemver.startsWith('0.0.0')) {
// Internal CLI testing version.
return;
}

const supportedAngularSemver = '0.0.0-ANGULAR-FW-PEER-DEP';
const angularVersion = new SemVer(angularPkgJson['version']);

if (!satisfies(angularVersion, supportedAngularSemver, { includePrerelease: true })) {
console.error(
`This version of CLI is only compatible with Angular versions ${supportedAngularSemver},\n` +
`but Angular version ${angularVersion} was found instead.\n` +
`Error: The current version of "@angular/build" supports Angular versions ${supportedAngularSemver},\n` +
`but detected Angular version ${angularVersion} instead.\n` +
'Please visit the link below to find instructions on how to update Angular.\nhttps://update.angular.dev/',
);

Expand Down