Skip to content

Commit

Permalink
feat: add peerDependencies option to --dep-type
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed May 23, 2023
1 parent 5cf0673 commit 2eb28ae
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ These options are available on the CLI and as parameters to the [Node API](#node

| Name | Description |
| :-- | :-- |
| `--dep-type` | Type of dependency to check (`dependencies`, `devDependencies`, `resolutions`) (default: `dependencies`, `devDependencies`, `resolutions`) (option can be repeated). |
| `--dep-type` | Type of dependency to check (`dependencies`, `devDependencies`, `peerDependencies`, `resolutions`) (default: `dependencies`, `devDependencies`, `resolutions`) (option can be repeated). |
| `--fix` | Whether to autofix inconsistencies (using latest version present). |
| `--ignore-dep` | Dependency to ignore mismatches for (option can be repeated). |
| `--ignore-dep-pattern` | RegExp of dependency names to ignore mismatches for (option can be repeated). |
Expand Down
36 changes: 36 additions & 0 deletions lib/dependency-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ function recordDependencyVersionsForPackageJson(
}
}

if (
depType.includes(DEPENDENCY_TYPE.peerDependencies) &&
package_.packageJson.peerDependencies
) {
for (const [dependency, dependencyVersion] of Object.entries(
package_.packageJson.peerDependencies
)) {
if (dependencyVersion) {
recordDependencyVersion(
dependenciesToVersionsSeen,
dependency,
dependencyVersion,
package_
);
}
}
}

if (
depType.includes(DEPENDENCY_TYPE.resolutions) &&
package_.packageJson.resolutions
Expand Down Expand Up @@ -388,6 +406,24 @@ export function fixVersionsMismatching(
isFixed = true;
}

if (
package_.packageJson.peerDependencies &&
package_.packageJson.peerDependencies[mismatchingVersion.dependency] &&
package_.packageJson.peerDependencies[mismatchingVersion.dependency] !==
fixedVersion
) {
if (!dryrun) {
writeDependencyVersion(
package_.pathPackageJson,
package_.packageJsonEndsInNewline,
DEPENDENCY_TYPE.peerDependencies,
mismatchingVersion.dependency,
fixedVersion
);
}
isFixed = true;
}

if (
package_.packageJson.resolutions &&
package_.packageJson.resolutions[mismatchingVersion.dependency] &&
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export type Dependencies = Record<
export enum DEPENDENCY_TYPE {
'dependencies' = 'dependencies',
'devDependencies' = 'devDependencies',
'peerDependencies' = 'peerDependencies',
'resolutions' = 'resolutions',
// TODO: `optionalDependencies`
// TODO: `peerDependencies`
}

export type Options = {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export const FIXTURE_PATH_NESTED_WORKSPACES = join(
'nested-workspaces'
);
export const FIXTURE_PATH_RESOLUTIONS = join(FIXTURE_PATH, 'resolutions');
export const FIXTURE_PATH_PEER_DEPENDENCIES = join(
FIXTURE_PATH,
'peer-dependencies'
);

export const FIXTURE_PATH_ALL_VERSION_TYPES = join(
FIXTURE_PATH,
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/peer-dependencies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"workspaces": [
"*"
],
"devDependencies": {
"foo": "^1.2.0"
},
"peerDependencies": {
"foo": "^2.0.0",
"bar": "^1.0.0"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/peer-dependencies/package1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "package1",
"dependencies": {
"foo": "1.3.0",
"bar": "^1.0.0"
}
}
40 changes: 39 additions & 1 deletion test/lib/cdvc-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
FIXTURE_PATH_INCONSISTENT_VERSIONS,
FIXTURE_PATH_PEER_DEPENDENCIES,
FIXTURE_PATH_RESOLUTIONS,
FIXTURE_PATH_VALID,
} from '../fixtures/index.js';
Expand Down Expand Up @@ -251,6 +252,43 @@ describe('CDVC', function () {
});
});

describe('peer dependencies', function () {
it('behaves correctly', function () {
const cdvc = new CDVC(FIXTURE_PATH_PEER_DEPENDENCIES, {
depType: ['peerDependencies'],
});
const dependencies = cdvc.getDependencies();

expect(cdvc.hasMismatchingDependencies).toBe(false);
expect(cdvc.hasMismatchingDependenciesFixable).toBe(false);
expect(cdvc.hasMismatchingDependenciesNotFixable).toBe(false);
expect(dependencies).toStrictEqual([
{
isFixable: false,
isMismatching: false,
name: 'bar',
versions: [
{
packages: [''],
version: '^1.0.0',
},
],
},
{
isFixable: false,
isMismatching: false,
name: 'foo',
versions: [
{
packages: [''],
version: '^2.0.0',
},
],
},
]);
});
});

describe('resolutions', () => {
it('behaves correctly', function () {
const cdvc = new CDVC(FIXTURE_PATH_RESOLUTIONS, {
Expand Down Expand Up @@ -297,7 +335,7 @@ describe('CDVC', function () {
depType: ['fake'],
})
).toThrowErrorMatchingInlineSnapshot(
'"Invalid depType provided. Choices are: dependencies, devDependencies, resolutions."'
'"Invalid depType provided. Choices are: dependencies, devDependencies, peerDependencies, resolutions."'
);
});
});
Expand Down

0 comments on commit 2eb28ae

Please sign in to comment.