Skip to content

Commit 776d121

Browse files
committed
fix(@angular/cli): update ng update output for Angular packages
With #21986 we now error when updating `@angular/` and `@nguniversal/` packages across multiple major versions. With this change we update `ng update` output to show the correct instructions. Before ``` $ ng update --next We analyzed your package.json, there are some packages to update: Name Version Command to update -------------------------------------------------------------------------------- @angular/cli 12.2.12 -> 13.0.0-rc.2 ng update @angular/cli --next @angular/core 11.2.14 -> 13.0.0-rc.2 ng update @angular/core --next ``` Now ``` $ ng update --next We analyzed your package.json, there are some packages to update: Name Version Command to update -------------------------------------------------------------------------------- @angular/cli 12.2.12 -> 13.0.0-rc.2 ng update @angular/cli --next @angular/core 11.2.14 -> 12.2.9 ng update @angular/core@12 ``` Closes #19381 (cherry picked from commit 9b32066)
1 parent d4ddb3d commit 776d121

File tree

1 file changed

+36
-5
lines changed
  • packages/schematics/update/update

1 file changed

+36
-5
lines changed

packages/schematics/update/update/index.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,39 @@ function _usageMessage(
459459
const packageGroups = new Map<string, string>();
460460
const packagesToUpdate = [...infoMap.entries()]
461461
.map(([name, info]) => {
462-
const tag = options.next
463-
? (info.npmPackageJson['dist-tags']['next'] ? 'next' : 'latest') : 'latest';
464-
const version = info.npmPackageJson['dist-tags'][tag];
465-
const target = info.npmPackageJson.versions[version];
462+
let tag = options.next
463+
? info.npmPackageJson['dist-tags']['next']
464+
? 'next'
465+
: 'latest'
466+
: 'latest';
467+
let version = info.npmPackageJson['dist-tags'][tag];
468+
let target = info.npmPackageJson.versions[version];
469+
470+
const versionDiff = semver.diff(info.installed.version, version);
471+
if (
472+
versionDiff !== 'patch' &&
473+
versionDiff !== 'minor' &&
474+
/^@(?:angular|nguniversal)\//.test(name)
475+
) {
476+
const installedMajorVersion = semver.parse(info.installed.version)?.major;
477+
const toInstallMajorVersion = semver.parse(version)?.major;
478+
if (
479+
installedMajorVersion !== undefined &&
480+
toInstallMajorVersion !== undefined &&
481+
installedMajorVersion < toInstallMajorVersion - 1
482+
) {
483+
const nextMajorVersion = `${installedMajorVersion + 1}.`;
484+
const nextMajorVersions = Object.keys(info.npmPackageJson.versions)
485+
.filter((v) => v.startsWith(nextMajorVersion))
486+
.sort((a, b) => (a > b ? -1 : 1));
487+
488+
if (nextMajorVersions.length) {
489+
version = nextMajorVersions[0];
490+
target = info.npmPackageJson.versions[version];
491+
tag = '';
492+
}
493+
}
494+
}
466495

467496
return {
468497
name,
@@ -496,7 +525,9 @@ function _usageMessage(
496525
}
497526

498527
let command = `ng update ${name}`;
499-
if (tag == 'next') {
528+
if (!tag) {
529+
command += `@${semver.parse(version)?.major || version}`;
530+
} else if (tag == 'next') {
500531
command += ' --next';
501532
}
502533

0 commit comments

Comments
 (0)