Skip to content

Commit

Permalink
fix(keys): scope regex match to within a dependency object (#181)
Browse files Browse the repository at this point in the history
* fix(keys): scope regex match to within a dependency object

* refactor
  • Loading branch information
danadajian authored May 25, 2023
1 parent 4356c36 commit 14a81a9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 31 deletions.
25 changes: 18 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions src/rules/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,30 @@ limitations under the License.
import * as core from '@actions/core';
import { readFileSync } from 'fs';
import { PackageJson } from 'type-fest';
import { getDependencies } from '../utils/get-dependencies';
import { DependencyType, getDependencies, getDependencyTypes } from '../utils/get-dependencies';

export const validateKeys = (packageJson: PackageJson, packageJsonPath: string) => {
const dependencies = getDependencies(packageJson);
const dependencyTypes = getDependencyTypes();
const stringifiedPackageJson = readFileSync(packageJsonPath).toString();
const stringifiedDependencyObjects = dependencyTypes.map(dependencyType =>
getStringifiedDependencyObject(stringifiedPackageJson, dependencyType)
);
Object.keys(dependencies).forEach(dependency => {
const stringifiedPackageJson = readFileSync(packageJsonPath).toString();
const regexMatches = stringifiedPackageJson.match(new RegExp(`"${dependency}"`, 'g'));
if (regexMatches && regexMatches.length > 1) {
core.setFailed(`Duplicate keys found in package.json: ${regexMatches}`);
}
stringifiedDependencyObjects.forEach(stringifiedDependencyObject => {
const regexMatches = stringifiedDependencyObject.match(new RegExp(`"${dependency}"`, 'g'));
if (regexMatches && regexMatches.length > 1) {
core.setFailed(`Duplicate keys found in package.json: ${regexMatches}`);
}
});
});
};

const getStringifiedDependencyObject = (
stringifiedPackageJson: string,
dependencyType: DependencyType
) => {
const dependenciesStartIndex = stringifiedPackageJson.indexOf(`"${dependencyType}"`);
const dependenciesEndIndex = stringifiedPackageJson.indexOf('}', dependenciesStartIndex);
return stringifiedPackageJson.substring(dependenciesStartIndex, dependenciesEndIndex + 1);
};
7 changes: 5 additions & 2 deletions src/utils/get-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ limitations under the License.
import * as core from '@actions/core';
import { PackageJson } from 'type-fest';

type Dependencies = keyof Pick<
export type DependencyType = keyof Pick<
PackageJson,
'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies'
>;

export const getDependencies = (packageJson: PackageJson): PackageJson.Dependency => {
const dependencyTypes = core.getMultilineInput('dependency-types') as Dependencies[];
const dependencyTypes = getDependencyTypes();
const packagesToIgnore = core.getMultilineInput('ignore-packages');
const dependencies: Record<string, string> = dependencyTypes.reduce(
(acc, dependencyType) => ({ ...acc, ...packageJson[dependencyType] }),
Expand All @@ -38,3 +38,6 @@ export const getDependencies = (packageJson: PackageJson): PackageJson.Dependenc
}
return filteredDependencies;
};

export const getDependencyTypes = () =>
core.getMultilineInput('dependency-types') as DependencyType[];
3 changes: 3 additions & 0 deletions test/fixtures/deduped-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
},
"devDependencies": {
"some-dependency-3": "2.0.0"
},
"scripts": {
"some-dependency": "some script"
}
}
9 changes: 0 additions & 9 deletions test/fixtures/duped-package2.json

This file was deleted.

6 changes: 0 additions & 6 deletions test/rules/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {validateKeys} from '../../src/rules/keys';
import * as core from '@actions/core';
import { getMultilineInput } from '@actions/core';
import dupedPackageJson from '../fixtures/duped-package.json';
import dupedPackageJson2 from '../fixtures/duped-package2.json';
import dedupedPackageJson from '../fixtures/deduped-package.json';

jest.mock('@actions/core');
Expand All @@ -14,11 +13,6 @@ describe('keys', () => {
expect(core.setFailed).toHaveBeenCalled();
});

it('should fail when package.json contains duplicate keys across dependencies and devDependencies', () => {
validateKeys(dupedPackageJson2, 'test/fixtures/duped-package2.json');
expect(core.setFailed).toHaveBeenCalled();
});

it('should not fail when package.json contains no duplicate keys', () => {
validateKeys(dedupedPackageJson, 'test/fixtures/deduped-package.json');
expect(core.setFailed).not.toHaveBeenCalled();
Expand Down

0 comments on commit 14a81a9

Please sign in to comment.