Skip to content

Commit

Permalink
feat: add ignore-packages input (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jul 8, 2022
1 parent 22673f5 commit 7f7df5b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 14 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ steps:
dependency-types: devDependencies
```

Specify `ignore-packages` to skip validation entirely for certain packages. Optionally provide a newline separated list of package names here.
```yaml
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: ExpediaGroup/package-json-validator@v1
with:
rules: ranges
ignore-packages: package-to-ignore
```

### Other Usages
You may also enforce multiple rules (and pass additional inputs) like this:
```yaml
Expand All @@ -96,6 +108,9 @@ steps:
dependency-types: |
dependencies
devDependencies
ignore-packages: |
package-to-ignore
another-package-to-ignore
```

# Contact
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ inputs:
description: "Line-separated list of dependency types to validate within your package.json."
required: false
default: "dependencies"
ignore-packages:
description: "Line-separated list of dependencies to skip validation for."
required: false
runs:
using: "node12"
main: "dist/index.js"
Expand Down
6 changes: 5 additions & 1 deletion 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.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"^.+\\.(j|t)sx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"testPathIgnorePatterns": [
"lib"
],
"clearMocks": true
}
}
11 changes: 9 additions & 2 deletions src/utils/get-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ type Dependencies = keyof Pick<

export const getDependencies = (packageJson: PackageJson): PackageJson.Dependency => {
const dependencyTypes = core.getMultilineInput('dependency-types') as Dependencies[];
const dependencies = dependencyTypes.reduce(
const packagesToIgnore = core.getMultilineInput('ignore-packages');
const dependencies: Record<string, string> = dependencyTypes.reduce(
(acc, dependencyType) => ({ ...acc, ...packageJson[dependencyType] }),
{}
);
const filteredDependencies = Object.keys(dependencies)
.filter(dependency => !packagesToIgnore.includes(dependency))
.reduce(
(acc, dependencyName) => ({ ...acc, [dependencyName]: dependencies[dependencyName] }),
{}
);
if (!Object.keys(dependencies).length) {
core.setFailed('Dependencies in package.json are undefined.');
throw new Error();
}
return dependencies;
return filteredDependencies;
};
49 changes: 39 additions & 10 deletions test/utils/get-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ describe('getDependencies', () => {
}

beforeEach(() => {
(core.getMultilineInput as jest.Mock).mockReturnValue(['dependencies']);
(core.getMultilineInput as jest.Mock).mockImplementation(input => input === 'dependency-types' ? ['dependencies'] : []);
result = getDependencies(packageJson);
});

it('should return expected result', () => {
expect(core.setFailed).not.toHaveBeenCalled();
expect(result).toMatchObject(dependencies);
expect(result).toEqual(dependencies);
});
});

Expand All @@ -44,23 +44,27 @@ describe('getDependencies', () => {
const packageJson: PackageJson = {
name: 'my-package-json',
dependencies: {
'some-package': '1.2.3'
'some-package': '1.2.3',
'some-package-2': '4.5.6'
},
devDependencies: {
'some-other-package': '1.2.3'
'some-other-package': '1.2.3',
'some-other-package-2': '4.5.6'
}
}

beforeEach(() => {
(core.getMultilineInput as jest.Mock).mockReturnValue(['dependencies', 'devDependencies']);
(core.getMultilineInput as jest.Mock).mockImplementation(input => input === 'dependency-types' ? ['dependencies', 'devDependencies'] : []);
result = getDependencies(packageJson);
});

it('should return expected result', () => {
expect(core.setFailed).not.toHaveBeenCalled();
expect(result).toMatchObject({
expect(result).toEqual({
'some-package': '1.2.3',
'some-other-package': '1.2.3'
'some-package-2': '4.5.6',
'some-other-package': '1.2.3',
'some-other-package-2': '4.5.6'
});
});
});
Expand All @@ -70,13 +74,38 @@ describe('getDependencies', () => {
name: 'my-package-json'
}

it('should return expected result', () => {
expect(() => getDependencies(packageJson)).toThrowError();
expect(core.setFailed).toHaveBeenCalled();
});
});

describe('ignore dependencies case', () => {
let result: unknown;
const packageJson: PackageJson = {
name: 'my-package-json',
dependencies: {
'some-package': '1.2.3',
'some-package-to-ignore': '4.5.6'
},
devDependencies: {
'some-other-package': '1.2.3',
'some-other-package-2': '4.5.6'
}
}

beforeEach(() => {
(core.getInput as jest.Mock).mockReturnValue(['dependencies']);
(core.getMultilineInput as jest.Mock).mockImplementation(input => input === 'dependency-types' ? ['dependencies', 'devDependencies'] : ['some-package-to-ignore']);
result = getDependencies(packageJson);
});

it('should return expected result', () => {
expect(() => getDependencies(packageJson)).toThrowError();
expect(core.setFailed).toHaveBeenCalled();
expect(core.setFailed).not.toHaveBeenCalled();
expect(result).toEqual({
'some-package': '1.2.3',
'some-other-package': '1.2.3',
'some-other-package-2': '4.5.6'
});
});
});
});

0 comments on commit 7f7df5b

Please sign in to comment.