Skip to content

Commit

Permalink
fix(@angular/cli): verify package specifier when adding a package
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin committed Nov 2, 2019
1 parent e70a2b0 commit 8fcbdb9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/angular/cli/commands/add-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { colors } from '../utilities/color';
import { getPackageManager } from '../utilities/package-manager';
import {
NgAddSaveDepedency,
PackageIdentifier,
PackageManifest,
fetchPackageManifest,
fetchPackageMetadata,
Expand Down Expand Up @@ -50,10 +51,26 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
}

if (packageIdentifier.registry && this.isPackageInstalled(packageIdentifier.name)) {
// Already installed so just run schematic
this.logger.info('Skipping installation: Package already installed');
let validVersion = false;
const installedVersion = await this.findProjectVersion(packageIdentifier.name);
if (installedVersion) {
if (packageIdentifier.type === 'range') {
validVersion = satisfies(installedVersion, packageIdentifier.fetchSpec);
} else if (packageIdentifier.type === 'version') {
const v1 = valid(packageIdentifier.fetchSpec);
const v2 = valid(installedVersion);
validVersion = v1 !== null && v1 === v2;
} else if (!packageIdentifier.rawSpec) {
validVersion = true;
}
}

return this.executeSchematic(packageIdentifier.name, options['--']);
if (validVersion) {
// Already installed so just run schematic
this.logger.info('Skipping installation: Package already installed');

return this.executeSchematic(packageIdentifier.name, options['--']);
}
}

const packageManager = await getPackageManager(this.workspace.root);
Expand Down
20 changes: 20 additions & 0 deletions tests/legacy-cli/e2e/tests/commands/add/add-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@ export default async function () {

await ng('add', '@angular/material');
await expectFileToMatch('package.json', /@angular\/material/);

const output1 = await ng('add', '@angular/material');
if (!output1.stdout.includes('Skipping installation: Package already installed')) {
throw new Error('Installation was not skipped');
}

const output2 = await ng('add', '@angular/material@latest');
if (output2.stdout.includes('Skipping installation: Package already installed')) {
throw new Error('Installation should not have been skipped');
}

const output3 = await ng('add', '@angular/[email protected]');
if (output3.stdout.includes('Skipping installation: Package already installed')) {
throw new Error('Installation should not have been skipped');
}

const output4 = await ng('add', '@angular/material@8');
if (!output4.stdout.includes('Skipping installation: Package already installed')) {
throw new Error('Installation was not skipped');
}
}

0 comments on commit 8fcbdb9

Please sign in to comment.