Navigation Menu

Skip to content

Commit

Permalink
fix(@angular/cli): favor non deprecated packages during update
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
alan-agius4 authored and clydin committed Sep 1, 2022
1 parent 7252439 commit 5405a9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
24 changes: 19 additions & 5 deletions packages/angular/cli/src/commands/update/schematic/index.ts
Expand Up @@ -561,9 +561,26 @@ function _buildPackageInfo(
const content = JSON.parse(packageContent.toString()) as JsonSchemaForNpmPackageJsonFiles;
installedVersion = content.version;
}

const packageVersionsNonDeprecated: string[] = [];
const packageVersionsDeprecated: string[] = [];

for (const [version, { deprecated }] of Object.entries(npmPackageJson.versions)) {
if (deprecated) {
packageVersionsDeprecated.push(version);
} else {
packageVersionsNonDeprecated.push(version);
}
}

const findSatisfyingVersion = (targetVersion: VersionRange): VersionRange | undefined =>
((semver.maxSatisfying(packageVersionsNonDeprecated, targetVersion) ??
semver.maxSatisfying(packageVersionsDeprecated, targetVersion)) as VersionRange | null) ??
undefined;

if (!installedVersion) {
// Find the version from NPM that fits the range to max.
installedVersion = semver.maxSatisfying(Object.keys(npmPackageJson.versions), packageJsonRange);
installedVersion = findSatisfyingVersion(packageJsonRange);
}

if (!installedVersion) {
Expand All @@ -586,10 +603,7 @@ function _buildPackageInfo(
} else if (targetVersion == 'next') {
targetVersion = npmPackageJson['dist-tags']['latest'] as VersionRange;
} else {
targetVersion = semver.maxSatisfying(
Object.keys(npmPackageJson.versions),
targetVersion,
) as VersionRange;
targetVersion = findSatisfyingVersion(targetVersion);
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/angular/cli/src/utilities/package-metadata.ts
Expand Up @@ -47,7 +47,9 @@ export interface NgPackageManifestProperties {
};
}

export interface PackageManifest extends Manifest, NgPackageManifestProperties {}
export interface PackageManifest extends Manifest, NgPackageManifestProperties {
deprecated?: boolean;
}

interface PackageManagerOptions extends Record<string, unknown> {
forceAuth?: Record<string, unknown>;
Expand Down

0 comments on commit 5405a9b

Please sign in to comment.