Skip to content

Commit ce1ec04

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 3040fde commit ce1ec04

File tree

1 file changed

+32
-4
lines changed
  • packages/angular/cli/src/commands/update/schematic

1 file changed

+32
-4
lines changed

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,39 @@ function _usageMessage(
422422
const packageGroups = new Map<string, string>();
423423
const packagesToUpdate = [...infoMap.entries()]
424424
.map(([name, info]) => {
425-
const tag = options.next
425+
let tag = options.next
426426
? info.npmPackageJson['dist-tags']['next']
427427
? 'next'
428428
: 'latest'
429429
: 'latest';
430-
const version = info.npmPackageJson['dist-tags'][tag];
431-
const target = info.npmPackageJson.versions[version];
430+
let version = info.npmPackageJson['dist-tags'][tag];
431+
let target = info.npmPackageJson.versions[version];
432+
433+
const versionDiff = semver.diff(info.installed.version, version);
434+
if (
435+
versionDiff !== 'patch' &&
436+
versionDiff !== 'minor' &&
437+
/^@(?:angular|nguniversal)\//.test(name)
438+
) {
439+
const installedMajorVersion = semver.parse(info.installed.version)?.major;
440+
const toInstallMajorVersion = semver.parse(version)?.major;
441+
if (
442+
installedMajorVersion !== undefined &&
443+
toInstallMajorVersion !== undefined &&
444+
installedMajorVersion < toInstallMajorVersion - 1
445+
) {
446+
const nextMajorVersion = `${installedMajorVersion + 1}.`;
447+
const nextMajorVersions = Object.keys(info.npmPackageJson.versions)
448+
.filter((v) => v.startsWith(nextMajorVersion))
449+
.sort((a, b) => (a > b ? -1 : 1));
450+
451+
if (nextMajorVersions.length) {
452+
version = nextMajorVersions[0];
453+
target = info.npmPackageJson.versions[version];
454+
tag = '';
455+
}
456+
}
457+
}
432458

433459
return {
434460
name,
@@ -462,7 +488,9 @@ function _usageMessage(
462488
}
463489

464490
let command = `ng update ${name}`;
465-
if (tag == 'next') {
491+
if (!tag) {
492+
command += `@${semver.parse(version)?.major || version}`;
493+
} else if (tag == 'next') {
466494
command += ' --next';
467495
}
468496

0 commit comments

Comments
 (0)