Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several package install fixes (ng add/update) #16880

Merged
merged 3 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions etc/api/angular_devkit/schematics/tools/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export declare class NodeWorkflow extends workflow.BaseWorkflow {
dryRun?: boolean;
root?: Path;
packageManager?: string;
packageRegistry?: string;
registry?: schema.CoreSchemaRegistry;
resolvePaths?: string[];
});
Expand Down
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
8 changes: 5 additions & 3 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 @@ -97,10 +98,11 @@ export function installTempPackage(
// setup prefix/global modules path
const packageManagerArgs = getPackageManagerArguments(packageManager);
const tempNodeModules = join(tempPath, 'node_modules');
// Yarn will not append 'node_modules' to the path
const prefixPath = packageManager === PackageManager.Yarn ? tempNodeModules : tempPath;
const installArgs: string[] = [
packageManagerArgs.prefix,
// Yarn will no append 'node_modules' to the path
packageManager === PackageManager.Yarn ? tempNodeModules : tempPath,
...(extraArgs || []),
`${packageManagerArgs.prefix}="${prefixPath}"`,
packageManagerArgs.noLockfile,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { Observable } from 'rxjs';
import { TaskExecutor, UnsuccessfulWorkflowExecution } from '../../src';
import { NodePackageTaskFactoryOptions, NodePackageTaskOptions } from './options';

type PackageManagerProfile = {
interface PackageManagerProfile {
quietArgument?: string;
commands: {
installAll?: string;
installPackage: string;
},
};
};
}

const packageManagers: { [name: string]: PackageManagerProfile } = {
'npm': {
Expand Down Expand Up @@ -99,6 +99,10 @@ export default function(
args.push(taskPackageManagerProfile.quietArgument);
}

if (factoryOptions.registry) {
args.push(`--registry="${factoryOptions.registry}"`);
}

return new Observable(obs => {
const spinner = ora({
text: 'Installing packages...',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface NodePackageTaskFactoryOptions {
rootDirectory?: string;
packageManager?: string;
allowPackageManagerOverride?: boolean;
registry?: string;
}

export interface NodePackageTaskOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class NodeWorkflow extends workflow.BaseWorkflow {
dryRun?: boolean;
root?: Path;
packageManager?: string;
packageRegistry?: string;
registry?: schema.CoreSchemaRegistry;
resolvePaths?: string[],
},
Expand All @@ -44,6 +45,7 @@ export class NodeWorkflow extends workflow.BaseWorkflow {
allowPackageManagerOverride: true,
packageManager: options.packageManager,
rootDirectory: options.root && getSystemPath(options.root),
registry: options.packageRegistry,
},
);
engineHost.registerTaskExecutor(
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');
}