Skip to content

Commit 03da128

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 chnage 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 d0924da commit 03da128

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

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

461490
return {
462491
name,
@@ -490,7 +519,9 @@ function _usageMessage(
490519
}
491520

492521
let command = `ng update ${name}`;
493-
if (tag == 'next') {
522+
if (!tag) {
523+
command += `@${semver.parse(version)?.major || version}`;
524+
} else if (tag == 'next') {
494525
command += ' --next';
495526
}
496527

0 commit comments

Comments
 (0)