Skip to content

Commit

Permalink
fix(@angular/cli): update ng update output for Angular packages
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
alan-agius4 committed Nov 24, 2021
1 parent d4ddb3d commit 776d121
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions packages/schematics/update/update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,39 @@ function _usageMessage(
const packageGroups = new Map<string, string>();
const packagesToUpdate = [...infoMap.entries()]
.map(([name, info]) => {
const tag = options.next
? (info.npmPackageJson['dist-tags']['next'] ? 'next' : 'latest') : 'latest';
const version = info.npmPackageJson['dist-tags'][tag];
const target = info.npmPackageJson.versions[version];
let tag = options.next
? info.npmPackageJson['dist-tags']['next']
? 'next'
: 'latest'
: 'latest';
let version = info.npmPackageJson['dist-tags'][tag];
let target = info.npmPackageJson.versions[version];

const versionDiff = semver.diff(info.installed.version, version);
if (
versionDiff !== 'patch' &&
versionDiff !== 'minor' &&
/^@(?:angular|nguniversal)\//.test(name)
) {
const installedMajorVersion = semver.parse(info.installed.version)?.major;
const toInstallMajorVersion = semver.parse(version)?.major;
if (
installedMajorVersion !== undefined &&
toInstallMajorVersion !== undefined &&
installedMajorVersion < toInstallMajorVersion - 1
) {
const nextMajorVersion = `${installedMajorVersion + 1}.`;
const nextMajorVersions = Object.keys(info.npmPackageJson.versions)
.filter((v) => v.startsWith(nextMajorVersion))
.sort((a, b) => (a > b ? -1 : 1));

if (nextMajorVersions.length) {
version = nextMajorVersions[0];
target = info.npmPackageJson.versions[version];
tag = '';
}
}
}

return {
name,
Expand Down Expand Up @@ -496,7 +525,9 @@ function _usageMessage(
}

let command = `ng update ${name}`;
if (tag == 'next') {
if (!tag) {
command += `@${semver.parse(version)?.major || version}`;
} else if (tag == 'next') {
command += ' --next';
}

Expand Down

0 comments on commit 776d121

Please sign in to comment.