From 267c103b0242a54b1e1feed48140bd134548c7a3 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 7 Feb 2020 13:10:27 -0500 Subject: [PATCH] fix(@angular/cli): fully use add command registry option when installing packages Fixes: #16766 (cherry picked from commit f4127b5b4356b7966a058b118335b7857922d613) --- packages/angular/cli/commands/add-impl.ts | 23 +++++++++++++++++-- .../angular/cli/models/schematic-command.ts | 2 ++ packages/angular/cli/tasks/install-package.ts | 2 ++ .../e2e/tests/commands/add/registry-option.ts | 18 +++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/legacy-cli/e2e/tests/commands/add/registry-option.ts diff --git a/packages/angular/cli/commands/add-impl.ts b/packages/angular/cli/commands/add-impl.ts index 78340db6b12c..0e6c45727d70 100644 --- a/packages/angular/cli/commands/add-impl.ts +++ b/packages/angular/cli/commands/add-impl.ts @@ -29,6 +29,14 @@ const npa = require('npm-package-arg'); export class AddCommand extends SchematicCommand { readonly allowPrivateSchematics = true; + async initialize(options: AddCommandSchema & Arguments) { + if (options.registry) { + return super.initialize({ ...options, packageRegistry: options.registry }); + } else { + return super.initialize(options); + } + } + async run(options: AddCommandSchema & Arguments) { if (!options.collection) { this.logger.fatal( @@ -156,7 +164,12 @@ export class AddCommand extends SchematicCommand { if (savePackage === false) { // Temporary packages are located in a different directory // Hence we need to resolve them using the temp path - const tempPath = installTempPackage(packageIdentifier.raw, this.logger, packageManager); + const tempPath = installTempPackage( + packageIdentifier.raw, + this.logger, + packageManager, + options.registry ? [`--registry="${options.registry}"`] : undefined, + ); const resolvedCollectionPath = require.resolve( join(collectionName, 'package.json'), { @@ -166,7 +179,13 @@ export class AddCommand extends SchematicCommand { collectionName = dirname(resolvedCollectionPath); } else { - installPackage(packageIdentifier.raw, this.logger, packageManager, savePackage); + installPackage( + packageIdentifier.raw, + this.logger, + packageManager, + savePackage, + options.registry ? [`--registry="${options.registry}"`] : undefined, + ); } return this.executeSchematic(collectionName, options['--']); diff --git a/packages/angular/cli/models/schematic-command.ts b/packages/angular/cli/models/schematic-command.ts index 4b5ab82e7d98..e2c7803cb968 100644 --- a/packages/angular/cli/models/schematic-command.ts +++ b/packages/angular/cli/models/schematic-command.ts @@ -53,6 +53,7 @@ export interface BaseSchematicSchema { force?: boolean; interactive?: boolean; defaults?: boolean; + packageRegistry?: string; } export interface RunSchematicOptions extends BaseSchematicSchema { @@ -250,6 +251,7 @@ export abstract class SchematicCommand< force, dryRun, packageManager: await getPackageManager(this.workspace.root), + packageRegistry: options.packageRegistry, root: normalize(this.workspace.root), registry: new schema.CoreSchemaRegistry(formats.standardFormats), resolvePaths: !!this.workspace.configFile diff --git a/packages/angular/cli/tasks/install-package.ts b/packages/angular/cli/tasks/install-package.ts index 8d9a6543e1d8..28799562937f 100644 --- a/packages/angular/cli/tasks/install-package.ts +++ b/packages/angular/cli/tasks/install-package.ts @@ -68,6 +68,7 @@ export function installTempPackage( packageName: string, logger: logging.Logger, packageManager: PackageManager = PackageManager.Npm, + extraArgs?: string[], ): string { const tempPath = mkdtempSync(join(realpathSync(tmpdir()), 'angular-cli-packages-')); @@ -100,6 +101,7 @@ export function installTempPackage( // Yarn will not append 'node_modules' to the path const prefixPath = packageManager === PackageManager.Yarn ? tempNodeModules : tempPath; const installArgs: string[] = [ + ...(extraArgs || []), `${packageManagerArgs.prefix}="${prefixPath}"`, packageManagerArgs.noLockfile, ]; diff --git a/tests/legacy-cli/e2e/tests/commands/add/registry-option.ts b/tests/legacy-cli/e2e/tests/commands/add/registry-option.ts new file mode 100644 index 000000000000..ea60f2504b19 --- /dev/null +++ b/tests/legacy-cli/e2e/tests/commands/add/registry-option.ts @@ -0,0 +1,18 @@ +import { expectFileToExist, rimraf, writeMultipleFiles } from '../../../utils/fs'; +import { ng } from '../../../utils/process'; +import { expectToFail } from '../../../utils/utils'; + +export default async function () { + // forcibly remove in case another test doesn't clean itself up + await rimraf('node_modules/@angular/material'); + + // Setup an invalid registry + await writeMultipleFiles({ + '.npmrc': 'registry=http://127.0.0.1:9999', + }); + + await expectToFail(() => ng('add', '@angular/pwa')); + + await ng('add', '--registry=http://localhost:4873', '@angular/pwa'); + await expectFileToExist('src/manifest.webmanifest'); +}