Skip to content

Commit 5405a9b

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular/cli): favor non deprecated packages during update
Prior to this change during update deprecated packages that satisfied the version range constrain where being favored over the non-deprecated versions if the version of the deprecated version is greater. Ex: if `14.3.1` is deprecated and `14.3.0` is not the former was being installed. With this change we now change the logic to favor non deprecated version of the package and only use the deprecated package when no satisfying version is found. This fix is needed as in some cases a package which cannot be unpublished from NPM will gave to be to be deprecated, if the version is for a reason or another broken. (cherry picked from commit 7e64b15)
1 parent 7252439 commit 5405a9b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/angular/cli/src/commands/update/schematic/index.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,26 @@ function _buildPackageInfo(
561561
const content = JSON.parse(packageContent.toString()) as JsonSchemaForNpmPackageJsonFiles;
562562
installedVersion = content.version;
563563
}
564+
565+
const packageVersionsNonDeprecated: string[] = [];
566+
const packageVersionsDeprecated: string[] = [];
567+
568+
for (const [version, { deprecated }] of Object.entries(npmPackageJson.versions)) {
569+
if (deprecated) {
570+
packageVersionsDeprecated.push(version);
571+
} else {
572+
packageVersionsNonDeprecated.push(version);
573+
}
574+
}
575+
576+
const findSatisfyingVersion = (targetVersion: VersionRange): VersionRange | undefined =>
577+
((semver.maxSatisfying(packageVersionsNonDeprecated, targetVersion) ??
578+
semver.maxSatisfying(packageVersionsDeprecated, targetVersion)) as VersionRange | null) ??
579+
undefined;
580+
564581
if (!installedVersion) {
565582
// Find the version from NPM that fits the range to max.
566-
installedVersion = semver.maxSatisfying(Object.keys(npmPackageJson.versions), packageJsonRange);
583+
installedVersion = findSatisfyingVersion(packageJsonRange);
567584
}
568585

569586
if (!installedVersion) {
@@ -586,10 +603,7 @@ function _buildPackageInfo(
586603
} else if (targetVersion == 'next') {
587604
targetVersion = npmPackageJson['dist-tags']['latest'] as VersionRange;
588605
} else {
589-
targetVersion = semver.maxSatisfying(
590-
Object.keys(npmPackageJson.versions),
591-
targetVersion,
592-
) as VersionRange;
606+
targetVersion = findSatisfyingVersion(targetVersion);
593607
}
594608
}
595609

packages/angular/cli/src/utilities/package-metadata.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export interface NgPackageManifestProperties {
4747
};
4848
}
4949

50-
export interface PackageManifest extends Manifest, NgPackageManifestProperties {}
50+
export interface PackageManifest extends Manifest, NgPackageManifestProperties {
51+
deprecated?: boolean;
52+
}
5153

5254
interface PackageManagerOptions extends Record<string, unknown> {
5355
forceAuth?: Record<string, unknown>;

0 commit comments

Comments
 (0)