Skip to content

Commit 9b32066

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
1 parent 9907f74 commit 9b32066

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

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

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

429455
return {
430456
name,
@@ -442,10 +468,9 @@ function _usageMessage(
442468
})
443469
.map(({ name, info, version, tag, target }) => {
444470
// Look for packageGroup.
445-
if (target['ng-update'] && target['ng-update']['packageGroup']) {
446-
const packageGroup = target['ng-update']['packageGroup'];
447-
const packageGroupName =
448-
target['ng-update']['packageGroupName'] || target['ng-update']['packageGroup'][0];
471+
const packageGroup = target['ng-update']?.['packageGroup'];
472+
if (packageGroup) {
473+
const packageGroupName = packageGroup?.[0];
449474
if (packageGroupName) {
450475
if (packageGroups.has(name)) {
451476
return null;
@@ -458,7 +483,9 @@ function _usageMessage(
458483
}
459484

460485
let command = `ng update ${name}`;
461-
if (tag == 'next') {
486+
if (!tag) {
487+
command += `@${semver.parse(version)?.major || version}`;
488+
} else if (tag == 'next') {
462489
command += ' --next';
463490
}
464491

tests/legacy-cli/e2e/tests/update/update-multiple-versions.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,32 @@ export default async function () {
99
await createProjectFromAsset('9.0-project', true, true);
1010
await setRegistry(false);
1111
await installWorkspacePackages();
12-
1312
await setRegistry(true);
13+
1414
const extraArgs = ['--force'];
1515
if (isPrereleaseCli()) {
1616
extraArgs.push('--next');
1717
}
1818

19-
const { message } = await expectToFail(() =>
20-
ng('update', '@angular/cli', '--force', ...extraArgs),
21-
);
19+
// Update Angular from v9 to 10
20+
const { stdout } = await ng('update', ...extraArgs);
21+
if (!/@angular\/core\s+9\.\d\.\d+ -> 10\.\d\.\d+\s+ng update @angular\/core@10/.test(stdout)) {
22+
// @angular/core 9.x.x -> 10.x.x ng update @angular/core@11
23+
throw new Error(
24+
`Output didn't match "@angular/core 9.x.x -> 10.x.x ng update @angular/core@10". OUTPUT: \n` +
25+
stdout,
26+
);
27+
}
28+
29+
const { message } = await expectToFail(() => ng('update', '@angular/cli', ...extraArgs));
2230
if (
2331
!message.includes(
2432
`Updating multiple major versions of '@angular/cli' at once is not supported`,
2533
)
2634
) {
27-
console.error(message);
2835
throw new Error(
29-
`Expected error message to include "Updating multiple major versions of '@angular/cli' at once is not supported" but didn't.`,
36+
`Expected error message to include "Updating multiple major versions of '@angular/cli' at once is not supported" but didn't. OUTPUT: \n` +
37+
message,
3038
);
3139
}
3240
} finally {

0 commit comments

Comments
 (0)