Skip to content

Commit

Permalink
fix(@schematics/update): handle peerDependenciesMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva authored and vikerman committed Oct 28, 2019
1 parent 2fc322f commit 6b9507a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/schematics/update/update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,22 @@ function _validateForwardPeerDependencies(
name: string,
infoMap: Map<string, PackageInfo>,
peers: {[name: string]: string},
peersMeta: { [name: string]: { optional?: boolean }},
logger: logging.LoggerApi,
next: boolean,
): boolean {
let validationFailed = false;
for (const [peer, range] of Object.entries(peers)) {
logger.debug(`Checking forward peer ${peer}...`);
const maybePeerInfo = infoMap.get(peer);
const isOptional = peersMeta[peer] && !!peersMeta[peer].optional;
if (!maybePeerInfo) {
logger.warn([
`Package ${JSON.stringify(name)} has a missing peer dependency of`,
`${JSON.stringify(peer)} @ ${JSON.stringify(range)}.`,
].join(' '));
if (!isOptional) {
logger.warn([
`Package ${JSON.stringify(name)} has a missing peer dependency of`,
`${JSON.stringify(peer)} @ ${JSON.stringify(range)}.`,
].join(' '));
}

continue;
}
Expand Down Expand Up @@ -211,8 +215,9 @@ function _validateUpdatePackages(
const pkgLogger = logger.createChild(name);
logger.debug(`${name}...`);

const peers = target.packageJson.peerDependencies || {};
peerErrors = _validateForwardPeerDependencies(name, infoMap, peers, pkgLogger, next) || peerErrors;
const { peerDependencies = {}, peerDependenciesMeta = {} } = target.packageJson;
peerErrors = _validateForwardPeerDependencies(name, infoMap, peerDependencies,
peerDependenciesMeta, pkgLogger, next) || peerErrors;
peerErrors
= _validateReversePeerDependencies(name, target.version, infoMap, pkgLogger, next)
|| peerErrors;
Expand Down
33 changes: 33 additions & 0 deletions packages/schematics/update/update/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,37 @@ describe('@schematics/update', () => {
}),
).toPromise().then(done, done.fail);
}, 45000);

it('validates peer dependencies', done => {
const content = virtualFs.fileBufferToString(host.sync.read(normalize('/package.json')));
const packageJson = JSON.parse(content);
const dependencies = packageJson['dependencies'];
// TODO: when we start using a local npm registry for test packages, add a package that includes
// a optional peer dependency and a non-optional one for this test. Use it instead of
// @angular-devkit/build-angular, whose optional peerdep is @angular/localize and non-optional
// are typescript and @angular/compiler-cli.
dependencies['@angular-devkit/build-angular'] = '0.900.0-next.1';
host.sync.write(
normalize('/package.json'),
virtualFs.stringToFileBuffer(JSON.stringify(packageJson)),
);

const messages: string[] = [];
schematicRunner.logger.subscribe(x => messages.push(x.message));
const hasPeerdepMsg = (dep: string) =>
messages.some(str => str.includes(`missing peer dependency of "${dep}"`));

schematicRunner.runSchematicAsync('update', {
packages: ['@angular-devkit/build-angular'],
next: true,
}, appTree).pipe(
map(() => {
expect(hasPeerdepMsg('@angular/compiler-cli'))
.toBeTruthy(`Should show @angular/compiler-cli message.`);
expect(hasPeerdepMsg('typescript')).toBeTruthy(`Should show typescript message.`);
expect(hasPeerdepMsg('@angular/localize'))
.toBeFalsy(`Should not show @angular/localize message.`);
}),
).toPromise().then(done, done.fail);
}, 45000);
});

0 comments on commit 6b9507a

Please sign in to comment.