Skip to content

Commit

Permalink
fix(@angular/cli): fully use add command registry option when install…
Browse files Browse the repository at this point in the history
…ing packages

Fixes: #16766
(cherry picked from commit f4127b5)
  • Loading branch information
clydin authored and dgp1130 committed Feb 11, 2020
1 parent ba34983 commit 267c103
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/angular/cli/commands/add-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const npa = require('npm-package-arg');
export class AddCommand extends SchematicCommand<AddCommandSchema> {
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(
Expand Down Expand Up @@ -156,7 +164,12 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
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'),
{
Expand All @@ -166,7 +179,13 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {

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['--']);
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface BaseSchematicSchema {
force?: boolean;
interactive?: boolean;
defaults?: boolean;
packageRegistry?: string;
}

export interface RunSchematicOptions extends BaseSchematicSchema {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/cli/tasks/install-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-'));

Expand Down Expand Up @@ -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,
];
Expand Down
18 changes: 18 additions & 0 deletions tests/legacy-cli/e2e/tests/commands/add/registry-option.ts
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 267c103

Please sign in to comment.