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

Add optionalDependencies option to --dep-type #606

Merged
merged 1 commit into from
May 23, 2023
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
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`, `peerDependencies` (optional), `resolutions`) (default: `dependencies`, `devDependencies`, `resolutions`) (option can be repeated). |
| `--dep-type` | Type of dependency to check (`dependencies`, `devDependencies`, `optionalDependencies` (optional), `peerDependencies` (optional), `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
40 changes: 40 additions & 0 deletions lib/dependency-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function calculateVersionsForEachDependency(
return dependenciesToVersionsSeen;
}

// eslint-disable-next-line complexity
function recordDependencyVersionsForPackageJson(
dependenciesToVersionsSeen: DependenciesToVersionsSeen,
package_: Package,
Expand Down Expand Up @@ -110,6 +111,24 @@ function recordDependencyVersionsForPackageJson(
}
}

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

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

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

if (
package_.packageJson.peerDependencies &&
package_.packageJson.peerDependencies[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',
'optionalDependencies' = 'optionalDependencies',
'peerDependencies' = 'peerDependencies',
'resolutions' = 'resolutions',
// TODO: `optionalDependencies`
}

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 @@ -82,6 +82,10 @@ export const FIXTURE_PATH_PEER_DEPENDENCIES = join(
FIXTURE_PATH,
'peer-dependencies'
);
export const FIXTURE_PATH_OPTIONAL_DEPENDENCIES = join(
FIXTURE_PATH,
'optional-dependencies'
);

export const FIXTURE_PATH_ALL_VERSION_TYPES = join(
FIXTURE_PATH,
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/optional-dependencies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"workspaces": [
"*"
],
"devDependencies": {
"foo": "^1.2.0"
},
"optionalDependencies": {
"foo": "^2.0.0",
"bar": "^1.0.0"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/optional-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"
}
}
140 changes: 139 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_OPTIONAL_DEPENDENCIES,
FIXTURE_PATH_PEER_DEPENDENCIES,
FIXTURE_PATH_RESOLUTIONS,
FIXTURE_PATH_VALID,
Expand Down Expand Up @@ -253,6 +254,143 @@ describe('CDVC', function () {
});
});

describe('optional dependencies', function () {
it('behaves correctly', function () {
const cdvc = new CDVC(FIXTURE_PATH_OPTIONAL_DEPENDENCIES, {
depType: ['optionalDependencies'],
});
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('mock fs', function () {
beforeEach(function () {
// Create a mock workspace filesystem for temporary usage in this test because changes will be written to some files.
mockFs({
'package.json': JSON.stringify({
workspaces: ['*'],
devDependencies: {
foo: '^1.2.0',
},
optionalDependencies: {
foo: '^1.0.0',
bar: '^1.0.0',
},
}),
package1: {
'package.json': JSON.stringify({
name: 'package1',
dependencies: {
foo: '^2.0.0',
bar: '^1.0.0',
},
}),
},
});
});

afterEach(function () {
mockFs.restore();
});

it('fixes the fixable inconsistencies', function () {
const cdvc = new CDVC('.', {
fix: true,
depType: [
DEPENDENCY_TYPE.optionalDependencies,
DEPENDENCY_TYPE.devDependencies,
DEPENDENCY_TYPE.dependencies,
],
});

// Read in package.json files.
const packageJsonRootContents = readFileSync('package.json', 'utf8');
const packageJson1Contents = readFileSync(
'package1/package.json',
'utf8'
);
const packageJsonRoot = JSON.parse(
packageJsonRootContents
) as PackageJson;
const packageJson1 = JSON.parse(packageJson1Contents) as PackageJson;

expect(
packageJsonRoot.devDependencies &&
packageJsonRoot.devDependencies['foo']
).toStrictEqual('^2.0.0');

expect(
packageJsonRoot.optionalDependencies &&
packageJsonRoot.optionalDependencies['foo']
).toStrictEqual('^2.0.0');

expect(
packageJson1.dependencies && packageJson1.dependencies['foo']
).toStrictEqual('^2.0.0');

expect(cdvc.getDependencies()).toStrictEqual([
{
name: 'bar',
isFixable: false,
isMismatching: false,
versions: [
{
version: '^1.0.0',
packages: ['', 'package1'],
},
],
},
{
name: 'foo',
isFixable: true,
isMismatching: true,
versions: [
{
version: '^1.0.0',
packages: [''],
},
{
version: '^1.2.0',
packages: [''],
},
{
version: '^2.0.0',
packages: ['package1'],
},
],
},
]);
});
});
});

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