Skip to content

Commit b37a01e

Browse files
authored
feat: alphabetical rule (#221)
* feat: alphabetical rule * add to main.ts * chore: comitting generated dist * test * test * test * fix * fix --------- Co-authored-by: danadajian <danadajian@users.noreply.github.com>
1 parent 021e11a commit b37a01e

File tree

8 files changed

+173
-4
lines changed

8 files changed

+173
-4
lines changed

.eslintrc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"@typescript-eslint/prefer-function-type": "warn",
4343
"@typescript-eslint/prefer-includes": "error",
4444
"@typescript-eslint/prefer-string-starts-ends-with": "error",
45-
"@typescript-eslint/require-array-sort-compare": "error",
4645
"@typescript-eslint/restrict-plus-operands": "error",
4746
"@typescript-eslint/type-annotation-spacing": "error",
4847
"@typescript-eslint/no-explicit-any": "error",

.github/workflows/test.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ jobs:
6060
tags
6161
resolutions
6262
keys
63+
alphabetical
64+
dependency-types: |
65+
dependencies
66+
devDependencies

dist/index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25643,6 +25643,7 @@ const ranges_1 = __nccwpck_require__(6507);
2564325643
const tags_1 = __nccwpck_require__(130);
2564425644
const resolutions_1 = __nccwpck_require__(8808);
2564525645
const keys_1 = __nccwpck_require__(4315);
25646+
const alphabetical_1 = __nccwpck_require__(972);
2564625647
const pathToPackageJson = './package.json';
2564725648
exports.RULES_MAP = {
2564825649
ranges: {
@@ -25659,6 +25660,9 @@ exports.RULES_MAP = {
2565925660
keys: {
2566025661
method: keys_1.validateKeys,
2566125662
extraInput: pathToPackageJson
25663+
},
25664+
alphabetical: {
25665+
method: alphabetical_1.validateAlphabetical
2566225666
}
2566325667
};
2566425668
const run = () => {
@@ -25678,6 +25682,69 @@ exports.run = run;
2567825682
(0, exports.run)();
2567925683

2568025684

25685+
/***/ }),
25686+
25687+
/***/ 972:
25688+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
25689+
25690+
"use strict";
25691+
25692+
/*
25693+
Copyright 2021 Expedia, Inc.
25694+
Licensed under the Apache License, Version 2.0 (the "License");
25695+
you may not use this file except in compliance with the License.
25696+
You may obtain a copy of the License at
25697+
https://www.apache.org/licenses/LICENSE-2.0
25698+
Unless required by applicable law or agreed to in writing, software
25699+
distributed under the License is distributed on an "AS IS" BASIS,
25700+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25701+
See the License for the specific language governing permissions and
25702+
limitations under the License.
25703+
*/
25704+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
25705+
if (k2 === undefined) k2 = k;
25706+
var desc = Object.getOwnPropertyDescriptor(m, k);
25707+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
25708+
desc = { enumerable: true, get: function() { return m[k]; } };
25709+
}
25710+
Object.defineProperty(o, k2, desc);
25711+
}) : (function(o, m, k, k2) {
25712+
if (k2 === undefined) k2 = k;
25713+
o[k2] = m[k];
25714+
}));
25715+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25716+
Object.defineProperty(o, "default", { enumerable: true, value: v });
25717+
}) : function(o, v) {
25718+
o["default"] = v;
25719+
});
25720+
var __importStar = (this && this.__importStar) || function (mod) {
25721+
if (mod && mod.__esModule) return mod;
25722+
var result = {};
25723+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25724+
__setModuleDefault(result, mod);
25725+
return result;
25726+
};
25727+
Object.defineProperty(exports, "__esModule", ({ value: true }));
25728+
exports.validateAlphabetical = void 0;
25729+
const core = __importStar(__nccwpck_require__(2186));
25730+
const get_dependencies_1 = __nccwpck_require__(4715);
25731+
const validateAlphabetical = (packageJson) => {
25732+
const dependencyTypes = (0, get_dependencies_1.getDependencyTypes)();
25733+
dependencyTypes.forEach(dependencyType => {
25734+
const dependencies = packageJson[dependencyType];
25735+
if (!dependencies) {
25736+
throw new Error(`${dependencyType} specified in dependency-types but missing in package.json`);
25737+
}
25738+
const sortedDependencies = Object.keys(dependencies).sort();
25739+
const isSorted = JSON.stringify(Object.keys(dependencies)) === JSON.stringify(sortedDependencies);
25740+
if (!isSorted) {
25741+
core.setFailed(`${dependencyType} in package.json are not sorted alphabetically.`);
25742+
}
25743+
});
25744+
};
25745+
exports.validateAlphabetical = validateAlphabetical;
25746+
25747+
2568125748
/***/ }),
2568225749

2568325750
/***/ 4315:

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { validateVersionRanges } from './rules/ranges';
1818
import { validateVersionTags } from './rules/tags';
1919
import { validateResolutions } from './rules/resolutions';
2020
import { validateKeys } from './rules/keys';
21+
import { validateAlphabetical } from './rules/alphabetical';
2122

2223
type GithubError = {
2324
status: number;
@@ -46,6 +47,9 @@ export const RULES_MAP: {
4647
keys: {
4748
method: validateKeys,
4849
extraInput: pathToPackageJson
50+
},
51+
alphabetical: {
52+
method: validateAlphabetical
4953
}
5054
};
5155

src/rules/alphabetical.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2021 Expedia, Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
https://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import * as core from '@actions/core';
15+
import { PackageJson } from 'type-fest';
16+
import { getDependencyTypes } from '../utils/get-dependencies';
17+
18+
export const validateAlphabetical = (packageJson: PackageJson) => {
19+
const dependencyTypes = getDependencyTypes();
20+
dependencyTypes.forEach(dependencyType => {
21+
const dependencies = packageJson[dependencyType];
22+
if (!dependencies) {
23+
throw new Error(
24+
`${dependencyType} specified in dependency-types but missing in package.json`
25+
);
26+
}
27+
const sortedDependencies = Object.keys(dependencies).sort();
28+
const isSorted =
29+
JSON.stringify(Object.keys(dependencies)) === JSON.stringify(sortedDependencies);
30+
if (!isSorted) {
31+
core.setFailed(`${dependencyType} in package.json are not sorted alphabetically.`);
32+
}
33+
});
34+
};

test/rules/alphabetical.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {PackageJson} from 'type-fest';
2+
import * as core from '@actions/core';
3+
import { validateAlphabetical } from '../../src/rules/alphabetical';
4+
import { getMultilineInput } from '@actions/core';
5+
6+
jest.mock('@actions/core');
7+
8+
(getMultilineInput as jest.Mock).mockReturnValue(['dependencies', 'devDependencies']);
9+
10+
describe('alphabetical', () => {
11+
it('should fail when dependencies are not in alphabetical order', () => {
12+
const packageJson: PackageJson = {
13+
dependencies: {
14+
'a-package': '1.2.3',
15+
'c-package': '1.2.3',
16+
'b-package': '1.2.3',
17+
},
18+
devDependencies: {
19+
'c-package': '1.2.3',
20+
'd-package': '1.2.3',
21+
'e-package': '1.2.3',
22+
}
23+
};
24+
validateAlphabetical(packageJson);
25+
expect(core.setFailed).toHaveBeenCalled();
26+
});
27+
28+
it('should fail when devDependencies are not in alphabetical order', () => {
29+
const packageJson: PackageJson = {
30+
dependencies: {
31+
'a-package': '1.2.3',
32+
'b-package': '1.2.3',
33+
'c-package': '1.2.3',
34+
},
35+
devDependencies: {
36+
'd-package': '1.2.3',
37+
'c-package': '1.2.3',
38+
'e-package': '1.2.3',
39+
}
40+
};
41+
validateAlphabetical(packageJson);
42+
expect(core.setFailed).toHaveBeenCalled();
43+
});
44+
45+
it('should not fail when dependencies are in alphabetical order', () => {
46+
const packageJson: PackageJson = {
47+
dependencies: {
48+
'a-package': '1.2.3',
49+
'b-package': '1.2.3',
50+
'c-package': '1.2.3',
51+
},
52+
devDependencies: {
53+
'c-package': '1.2.3',
54+
'd-package': '1.2.3',
55+
'e-package': '1.2.3',
56+
}
57+
};
58+
validateAlphabetical(packageJson);
59+
expect(core.setFailed).not.toHaveBeenCalled();
60+
});
61+
});

test/rules/resolutions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as core from '@actions/core';
55
jest.mock('@actions/core');
66

77
describe('resolutions', () => {
8-
it('should fail when resolutions are present', function () {
8+
it('should fail when resolutions are present', () => {
99
const packageJson: PackageJson = {
1010
dependencies: {},
1111
resolutions: {
@@ -16,7 +16,7 @@ describe('resolutions', () => {
1616
expect(core.setFailed).toHaveBeenCalled();
1717
});
1818

19-
it('should not fail when resolutions are not present', function () {
19+
it('should not fail when resolutions are not present', () => {
2020
const packageJson: PackageJson = {
2121
dependencies: {}
2222
};

0 commit comments

Comments
 (0)