diff --git a/packages/angular/cli/src/commands/update/cli.ts b/packages/angular/cli/src/commands/update/cli.ts index 152c6d3b8632..0f21999b1532 100644 --- a/packages/angular/cli/src/commands/update/cli.ts +++ b/packages/angular/cli/src/commands/update/cli.ts @@ -75,9 +75,7 @@ export class UpdateCommandModule extends CommandModule { array: true, }) .option('force', { - description: - 'Ignore peer dependency version mismatches. ' + - `Passes the '--force' flag to the package manager when installing packages.`, + description: 'Ignore peer dependency version mismatches.', type: 'boolean', default: false, }) @@ -227,7 +225,7 @@ export class UpdateCommandModule extends CommandModule { const workflow = new NodeWorkflow(this.context.root, { packageManager: packageManager.name, - packageManagerForce: options.force, + packageManagerForce: this.packageManagerForce(options.verbose), // __dirname -> favor @schematics/update from this package // Otherwise, use packages from the active workspace (migrations) resolvePaths: [__dirname, this.context.root], @@ -693,26 +691,8 @@ export class UpdateCommandModule extends CommandModule { }); } catch {} - let forceInstall = false; - // npm 7+ can fail due to it incorrectly resolving peer dependencies that have valid SemVer - // ranges during an update. Update will set correct versions of dependencies within the - // package.json file. The force option is set to workaround these errors. - // Example error: - // npm ERR! Conflicting peer dependency: @angular/compiler-cli@14.0.0-rc.0 - // npm ERR! node_modules/@angular/compiler-cli - // npm ERR! peer @angular/compiler-cli@"^14.0.0 || ^14.0.0-rc" from @angular-devkit/build-angular@14.0.0-rc.0 - // npm ERR! node_modules/@angular-devkit/build-angular - // npm ERR! dev @angular-devkit/build-angular@"~14.0.0-rc.0" from the root project - if ( - this.context.packageManager.name === PackageManager.Npm && - this.context.packageManager.version && - semver.gte(this.context.packageManager.version, '7.0.0', { includePrerelease: true }) - ) { - logVerbose('NPM 7+ detected -- enabling force option for package installation'); - forceInstall = true; - } const installationSuccess = await this.context.packageManager.installAll( - forceInstall ? ['--force'] : [], + this.packageManagerForce(options.verbose) ? ['--force'] : [], this.context.root, ); @@ -992,6 +972,33 @@ export class UpdateCommandModule extends CommandModule { return status ?? 0; } + + private packageManagerForce(verbose: boolean): boolean { + // npm 7+ can fail due to it incorrectly resolving peer dependencies that have valid SemVer + // ranges during an update. Update will set correct versions of dependencies within the + // package.json file. The force option is set to workaround these errors. + // Example error: + // npm ERR! Conflicting peer dependency: @angular/compiler-cli@14.0.0-rc.0 + // npm ERR! node_modules/@angular/compiler-cli + // npm ERR! peer @angular/compiler-cli@"^14.0.0 || ^14.0.0-rc" from @angular-devkit/build-angular@14.0.0-rc.0 + // npm ERR! node_modules/@angular-devkit/build-angular + // npm ERR! dev @angular-devkit/build-angular@"~14.0.0-rc.0" from the root project + if ( + this.context.packageManager.name === PackageManager.Npm && + this.context.packageManager.version && + semver.gte(this.context.packageManager.version, '7.0.0', { includePrerelease: true }) + ) { + if (verbose) { + this.context.logger.info( + 'NPM 7+ detected -- enabling force option for package installation', + ); + } + + return true; + } + + return false; + } } /**