Skip to content

Commit

Permalink
feat: add scripts validation to keys rule (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jan 22, 2024
1 parent 2b35c40 commit 5495169
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
18 changes: 13 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25796,7 +25796,7 @@ const validateKeys = (packageJson, packageJsonPath) => {
const dependencies = (0, get_dependencies_1.getDependencies)(packageJson);
const dependencyTypes = (0, get_dependencies_1.getDependencyTypes)();
const stringifiedPackageJson = (0, fs_1.readFileSync)(packageJsonPath).toString();
const stringifiedDependencyObjects = dependencyTypes.map(dependencyType => getStringifiedDependencyObject(stringifiedPackageJson, dependencyType));
const stringifiedDependencyObjects = dependencyTypes.map(dependencyType => getStringifiedPackageJsonObject(dependencyType, stringifiedPackageJson));
Object.keys(dependencies).forEach(dependency => {
stringifiedDependencyObjects.forEach(stringifiedDependencyObject => {
const regexMatches = stringifiedDependencyObject.match(new RegExp(`"${dependency}"`, 'g'));
Expand All @@ -25805,12 +25805,20 @@ const validateKeys = (packageJson, packageJsonPath) => {
}
});
});
if (packageJson.scripts) {
Object.keys(packageJson.scripts).forEach(script => {
const regexMatches = getStringifiedPackageJsonObject('scripts', stringifiedPackageJson).match(new RegExp(`"${script}"`, 'g'));
if (regexMatches && regexMatches.length > 1) {
core.setFailed(`Duplicate keys found in package.json: ${regexMatches}`);
}
});
}
};
exports.validateKeys = validateKeys;
const getStringifiedDependencyObject = (stringifiedPackageJson, dependencyType) => {
const dependenciesStartIndex = stringifiedPackageJson.indexOf(`"${dependencyType}"`);
const dependenciesEndIndex = stringifiedPackageJson.indexOf('}', dependenciesStartIndex);
return stringifiedPackageJson.substring(dependenciesStartIndex, dependenciesEndIndex + 1);
const getStringifiedPackageJsonObject = (field, stringifiedPackageJson) => {
const startIndex = stringifiedPackageJson.indexOf(`"${field}"`);
const endIndex = stringifiedPackageJson.indexOf('}', startIndex);
return stringifiedPackageJson.substring(startIndex, endIndex + 1);
};


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

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions src/rules/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ limitations under the License.
import * as core from '@actions/core';
import { readFileSync } from 'fs';
import { PackageJson } from 'type-fest';
import { DependencyType, getDependencies, getDependencyTypes } from '../utils/get-dependencies';
import { 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)
getStringifiedPackageJsonObject(dependencyType, stringifiedPackageJson)
);
Object.keys(dependencies).forEach(dependency => {
stringifiedDependencyObjects.forEach(stringifiedDependencyObject => {
Expand All @@ -31,13 +31,20 @@ export const validateKeys = (packageJson: PackageJson, packageJsonPath: string)
}
});
});
if (packageJson.scripts) {
Object.keys(packageJson.scripts).forEach(script => {
const regexMatches = getStringifiedPackageJsonObject('scripts', stringifiedPackageJson).match(
new RegExp(`"${script}"`, '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);
const getStringifiedPackageJsonObject = (field: string, stringifiedPackageJson: string) => {
const startIndex = stringifiedPackageJson.indexOf(`"${field}"`);
const endIndex = stringifiedPackageJson.indexOf('}', startIndex);
return stringifiedPackageJson.substring(startIndex, endIndex + 1);
};
9 changes: 9 additions & 0 deletions test/fixtures/duped-scripts-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"some-dependency": "1.0.0"
},
"scripts": {
"some-script": "echo Hey",
"some-script": "echo Ho"
}
}
8 changes: 7 additions & 1 deletion test/rules/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 dupedScriptsPackageJson from '../fixtures/duped-scripts-package.json';
import dedupedPackageJson from '../fixtures/deduped-package.json';

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

it('should not fail when package.json contains no duplicate keys', () => {
it('should fail when package.json contains duplicate keys in scripts', () => {
validateKeys(dupedScriptsPackageJson, 'test/fixtures/duped-scripts-package.json');
expect(core.setFailed).toHaveBeenCalled();
});

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

0 comments on commit 5495169

Please sign in to comment.