Skip to content

Commit f4127b5

Browse files
clydindgp1130
authored andcommitted
fix(@angular/cli): fully use add command registry option when installing packages
Fixes: #16766
1 parent 33656f7 commit f4127b5

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

packages/angular/cli/commands/add-impl.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ const npa = require('npm-package-arg');
2929
export class AddCommand extends SchematicCommand<AddCommandSchema> {
3030
readonly allowPrivateSchematics = true;
3131

32+
async initialize(options: AddCommandSchema & Arguments) {
33+
if (options.registry) {
34+
return super.initialize({ ...options, packageRegistry: options.registry });
35+
} else {
36+
return super.initialize(options);
37+
}
38+
}
39+
3240
async run(options: AddCommandSchema & Arguments) {
3341
if (!options.collection) {
3442
this.logger.fatal(
@@ -156,7 +164,12 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
156164
if (savePackage === false) {
157165
// Temporary packages are located in a different directory
158166
// Hence we need to resolve them using the temp path
159-
const tempPath = installTempPackage(packageIdentifier.raw, this.logger, packageManager);
167+
const tempPath = installTempPackage(
168+
packageIdentifier.raw,
169+
this.logger,
170+
packageManager,
171+
options.registry ? [`--registry="${options.registry}"`] : undefined,
172+
);
160173
const resolvedCollectionPath = require.resolve(
161174
join(collectionName, 'package.json'),
162175
{
@@ -166,7 +179,13 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
166179

167180
collectionName = dirname(resolvedCollectionPath);
168181
} else {
169-
installPackage(packageIdentifier.raw, this.logger, packageManager, savePackage);
182+
installPackage(
183+
packageIdentifier.raw,
184+
this.logger,
185+
packageManager,
186+
savePackage,
187+
options.registry ? [`--registry="${options.registry}"`] : undefined,
188+
);
170189
}
171190

172191
return this.executeSchematic(collectionName, options['--']);

packages/angular/cli/models/schematic-command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface BaseSchematicSchema {
5353
force?: boolean;
5454
interactive?: boolean;
5555
defaults?: boolean;
56+
packageRegistry?: string;
5657
}
5758

5859
export interface RunSchematicOptions extends BaseSchematicSchema {
@@ -250,6 +251,7 @@ export abstract class SchematicCommand<
250251
force,
251252
dryRun,
252253
packageManager: await getPackageManager(this.workspace.root),
254+
packageRegistry: options.packageRegistry,
253255
root: normalize(this.workspace.root),
254256
registry: new schema.CoreSchemaRegistry(formats.standardFormats),
255257
resolvePaths: !!this.workspace.configFile

packages/angular/cli/tasks/install-package.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export function installTempPackage(
6868
packageName: string,
6969
logger: logging.Logger,
7070
packageManager: PackageManager = PackageManager.Npm,
71+
extraArgs?: string[],
7172
): string {
7273
const tempPath = mkdtempSync(join(realpathSync(tmpdir()), 'angular-cli-packages-'));
7374

@@ -100,6 +101,7 @@ export function installTempPackage(
100101
// Yarn will not append 'node_modules' to the path
101102
const prefixPath = packageManager === PackageManager.Yarn ? tempNodeModules : tempPath;
102103
const installArgs: string[] = [
104+
...(extraArgs || []),
103105
`${packageManagerArgs.prefix}="${prefixPath}"`,
104106
packageManagerArgs.noLockfile,
105107
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expectFileToExist, rimraf, writeMultipleFiles } from '../../../utils/fs';
2+
import { ng } from '../../../utils/process';
3+
import { expectToFail } from '../../../utils/utils';
4+
5+
export default async function () {
6+
// forcibly remove in case another test doesn't clean itself up
7+
await rimraf('node_modules/@angular/material');
8+
9+
// Setup an invalid registry
10+
await writeMultipleFiles({
11+
'.npmrc': 'registry=http://127.0.0.1:9999',
12+
});
13+
14+
await expectToFail(() => ng('add', '@angular/pwa'));
15+
16+
await ng('add', '--registry=http://localhost:4873', '@angular/pwa');
17+
await expectFileToExist('src/manifest.webmanifest');
18+
}

0 commit comments

Comments
 (0)