Skip to content

Commit

Permalink
feat: support multiple dependency types (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jan 4, 2022
1 parent 3096b45 commit d0f17a1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ steps:
allowed-tags: canary
```

Specify `dependency-types` to denote which type of package.json dependencies you wish to validate. Valid options include `dependencies`, `devDependencies`, `peerDependencies`, and `optionalDependencies`. Defaults to `dependencies`.
```yaml
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: ExpediaGroup/package-json-validator@v1
with:
rules: ranges
dependency-types: devDependencies
```

### Other Usages
You may also enforce multiple rules (and pass additional inputs) like this:
```yaml
Expand All @@ -81,18 +93,9 @@ steps:
allowed-tags: |
alpha
canary
```

Specify `dependency-type` to denote which type of package.json dependencies you wish to validate. Valid options include `dependencies`, `devDependencies`, `peerDependencies`, and `optionalDependencies`. Defaults to `dependencies`.
```yaml
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: ExpediaGroup/package-json-validator@v1
with:
rules: ranges
dependency-type: devDependencies
dependency-types: |
dependencies
devDependencies
```

# Contact
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ inputs:
allowed-tags:
description: "Can be used with the tags rule. Line-separated list of package version tags allowed in your package.json. Omitting this input means no version tags are allowed."
required: false
dependency-type:
description: "Specifies the dependency list to validate within your package.json."
dependency-types:
description: "Line-separated list of dependency types to validate within your package.json."
required: false
default: "dependencies"
runs:
Expand Down
6 changes: 3 additions & 3 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.

19 changes: 12 additions & 7 deletions src/utils/get-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ limitations under the License.
import * as core from '@actions/core';
import { PackageJson } from 'type-fest';

export const getDependencies = (packageJson: PackageJson) => {
const dependencyType = core.getInput('dependency-type') as keyof Pick<
PackageJson,
'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies'
>;
const dependencies = packageJson[dependencyType];
if (!dependencies) {
type Dependencies = keyof Pick<
PackageJson,
'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies'
>;

export const getDependencies = (packageJson: PackageJson): PackageJson.Dependency => {
const dependencyTypes = core.getMultilineInput('dependency-types') as Dependencies[];
const dependencies = dependencyTypes.reduce(
(acc, dependencyType) => ({ ...acc, ...packageJson[dependencyType] }),
{}
);
if (!Object.keys(dependencies).length) {
core.setFailed('Dependencies in package.json are undefined.');
throw new Error();
}
Expand Down
31 changes: 28 additions & 3 deletions test/utils/get-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('getDependencies', () => {
}

beforeEach(() => {
(core.getInput as jest.Mock).mockReturnValue('dependencies');
(core.getMultilineInput as jest.Mock).mockReturnValue(['dependencies']);
result = getDependencies(packageJson);
});

Expand All @@ -39,13 +39,39 @@ describe('getDependencies', () => {
});
});

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

beforeEach(() => {
(core.getMultilineInput as jest.Mock).mockReturnValue(['dependencies', 'devDependencies']);
result = getDependencies(packageJson);
});

it('should return expected result', () => {
expect(core.setFailed).not.toHaveBeenCalled();
expect(result).toMatchObject({
'some-package': '1.2.3',
'some-other-package': '1.2.3'
});
});
});

describe('dependencies do not exist case', () => {
const packageJson: PackageJson = {
name: 'my-package-json'
}

beforeEach(() => {
(core.getInput as jest.Mock).mockReturnValue('dependencies');
(core.getInput as jest.Mock).mockReturnValue(['dependencies']);
});

it('should return expected result', () => {
Expand All @@ -54,4 +80,3 @@ describe('getDependencies', () => {
});
});
});

0 comments on commit d0f17a1

Please sign in to comment.