Skip to content

Commit

Permalink
feat: Add support for ignoring peer dependencies (via cli option or c…
Browse files Browse the repository at this point in the history
…onfig stored in package.json)
  • Loading branch information
christopherthielen committed Nov 28, 2021
1 parent e2f0fee commit 5994c9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
41 changes: 25 additions & 16 deletions src/checkPeerDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ function getAllNestedPeerDependencies(options: CliOptions): Dependency[] {
return { ...dep, installedVersion, semverSatisfies, isYalc };
}

return gatheredDependencies.map(applySemverInformation);
function applyIgnoreInformation (dep: Dependency): Dependency {
const isIgnored = options.ignore.includes(dep.name)
return {...dep, isIgnored}
}

return gatheredDependencies.map(applySemverInformation).map(applyIgnoreInformation);
}

let recursiveCount = 0;

const reportPeerDependencyStatus = (dep: Dependency, byDepender: boolean, showSatisfiedDep: boolean, showOptionalDep: boolean) => {
const isProblem = (dep: Dependency) => !dep.semverSatisfies && !dep.isIgnored && !dep.isYalc && !dep.isPeerOptionalDependency;

const reportPeerDependencyStatus = (dep: Dependency, byDepender: boolean, showSatisfiedDep: boolean, verbose: boolean) => {
const message = byDepender ?
`${dep.depender.name}@${dep.depender.version} requires ${dep.name} ${dep.version}` :
`${dep.name} ${dep.version} is required by ${dep.depender.name}@${dep.depender.version}`;
Expand All @@ -35,14 +42,18 @@ const reportPeerDependencyStatus = (dep: Dependency, byDepender: boolean, showSa
} else if (dep.isYalc) {
console.log(` ☑️ ${message} (${dep.installedVersion} is installed via yalc)`);
} else if (dep.installedVersion && dep.isPeerOptionalDependency) {
if (showOptionalDep) {
console.log(` ☑️ ${message}) OPTIONAL (${dep.installedVersion} is installed)`);
if (verbose) {
console.log(` ☑️ ${message}) OPTIONAL (${dep.installedVersion} is installed)`);
}
} else if (dep.isIgnored) {
if (verbose) {
console.log(` ☑️ ${message} IGNORED (${dep.name} is not installed)`);
}
} else if (dep.installedVersion) {
console.log(` ❌ ${message}) (${dep.installedVersion} is installed)`);
} else if (dep.isPeerOptionalDependency) {
if (showOptionalDep) {
console.log(` ☑️ ${message} OPTIONAL (${dep.name} is not installed)`);
if (verbose) {
console.log(` ☑️ ${message} OPTIONAL (${dep.name} is not installed)`);
}
} else {
console.log(` ❌ ${message} (${dep.name} is not installed)`);
Expand Down Expand Up @@ -82,16 +93,16 @@ function installPeerDependencies(commandLines: any[], options: CliOptions, nosol
console.log();
});

const newUnsatisfiedDeps = getAllNestedPeerDependencies(options)
.filter(dep => !dep.semverSatisfies)
const newProblems = getAllNestedPeerDependencies(options)
.filter(dep => isProblem(dep))
.filter(dep => !nosolution.some(x => isSameDep(x.problem, dep)));

if (nosolution.length === 0 && newUnsatisfiedDeps.length === 0) {
if (nosolution.length === 0 && newProblems.length === 0) {
console.log('All peer dependencies are met');
}

if (newUnsatisfiedDeps.length > 0) {
console.log(`Found ${newUnsatisfiedDeps.length} new unmet peerDependencies...`);
if (newProblems.length > 0) {
console.log(`Found ${newProblems.length} new unmet peerDependencies...`);
if (++recursiveCount < 5) {
return checkPeerDependencies(packageManager, options);
} else {
Expand All @@ -110,19 +121,17 @@ function report(options: CliOptions, allNestedPeerDependencies: Dependency[]) {
}

allNestedPeerDependencies.forEach(dep => {
const isUnsatisfied = (dep: Dependency) => !dep.semverSatisfies && !dep.isYalc;
const relatedPeerDeps = allNestedPeerDependencies.filter(other => other.name === dep.name && other !== dep);
const showIfSatisfied = options.verbose || relatedPeerDeps.some(isUnsatisfied);
const showOptionalDep = options.verbose;
reportPeerDependencyStatus(dep, options.orderBy === 'depender', showIfSatisfied, showOptionalDep);
const showIfSatisfied = options.verbose || relatedPeerDeps.some(dep => isProblem(dep));
reportPeerDependencyStatus(dep, options.orderBy === 'depender', showIfSatisfied, options.verbose);
});
}

export function checkPeerDependencies(packageManager: string, options: CliOptions) {
const allNestedPeerDependencies = getAllNestedPeerDependencies(options);
report(options, allNestedPeerDependencies);

const problems = allNestedPeerDependencies.filter(dep => !dep.semverSatisfies && !dep.isYalc && !dep.isPeerOptionalDependency);
const problems = allNestedPeerDependencies.filter(dep => isProblem(dep));

if (!problems.length) {
console.log(' ✅ All peer dependencies are met');
Expand Down
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const options = yarrrrgs
default: false,
description: 'Prints every peer dependency, even those that are met',
})
.option('ignore', {
string: true,
array: true,
default: [],
description: 'package name to ignore (may specify multiple)',
})
.option('runOnlyOnRootDependencies', {
boolean: true,
default: false,
Expand Down Expand Up @@ -63,6 +69,7 @@ export interface CliOptions {
verbose: boolean;
debug: boolean;
npm: boolean;
ignore: string[];
runOnlyOnRootDependencies: boolean;
orderBy: 'depender' | 'dependee';
findSolutions: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/packageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Dependency {
installedVersion?: string | undefined;
semverSatisfies?: boolean;
isYalc?: boolean;
isIgnored?: boolean;
}

interface PackageMeta {
Expand Down Expand Up @@ -184,7 +185,6 @@ export function getInstalledVersion(dep: Dependency): string | undefined {
return isYalc ? `${packageJson.version}-yalc` : packageJson.version;
}


export function isSameDep(a: Dependency, b: Dependency) {
const keys: Array<keyof Dependency> = [ "name", "version", "installedVersion", "semverSatisfies", "isYalc", "isPeerDevDependency", ];
return keys.every(key => a[key] === b[key]) &&
Expand Down

0 comments on commit 5994c9c

Please sign in to comment.