Skip to content

Commit 5df5f77

Browse files
committed
chore: intermediate custom sort
Squashed commit of the following: commit a44db9e23a35805b53161adaa9b85556f40b67a2 Merge: 162d2eb e240b44 Author: shellscape <[email protected]> Date: Sun Oct 6 19:20:28 2019 -0400 chore: Merge branch 'feat/sort-dependencies' of https://github.com/will-stone/prettier-plugin-package into will-stone-feat/sort-dependencies commit e240b44 Author: Will Stone <[email protected]> Date: Thu Oct 3 10:41:57 2019 +0100 feat: sort dependencies
1 parent 162d2eb commit 5df5f77

File tree

6 files changed

+120
-3
lines changed

6 files changed

+120
-3
lines changed

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
const { parsers } = require('prettier/parser-babylon');
1212

13+
const { dependencies } = require('./rules/dependencies');
1314
const { engines } = require('./rules/engines');
1415
const { files } = require('./rules/files');
1516
const { scripts } = require('./rules/scripts');
@@ -24,6 +25,7 @@ const format = (properties) => {
2425
props = engines(props);
2526
props = files(props);
2627
props = scripts(props);
28+
props = dependencies(props);
2729

2830
return props;
2931
};

lib/rules/dependencies.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright © 2019 Andrew Powell
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+
The above copyright notice and this permission notice shall be
9+
included in all copies or substantial portions of this Source Code Form.
10+
*/
11+
12+
const dependencyNames = [
13+
// dependencies
14+
'bundledDependencies',
15+
'optionalDependencies',
16+
'peerDependencies',
17+
'dependencies',
18+
'devDependencies',
19+
'resolutions'
20+
];
21+
22+
const process = (props) =>
23+
props.map((prop) => {
24+
if (dependencyNames.includes(prop.key.value)) {
25+
prop.value.properties.sort((a, b) =>
26+
a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0
27+
);
28+
}
29+
return prop;
30+
});
31+
32+
module.exports = { dependencies: process };

lib/rules/props.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright © 2019 Andrew Powell
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+
The above copyright notice and this permission notice shall be
9+
included in all copies or substantial portions of this Source Code Form.
10+
*/
11+
const primary = [
12+
// meta
13+
'name',
14+
'version',
15+
'flat',
16+
'private',
17+
'publishConfig',
18+
'description',
19+
'license',
20+
'repository',
21+
'author',
22+
'homepage',
23+
'bugs',
24+
25+
// entry
26+
'main',
27+
'bin',
28+
'module',
29+
30+
// constraints
31+
'engines',
32+
'cpu',
33+
'os',
34+
35+
// content and util
36+
'scripts',
37+
'files',
38+
'keywords',
39+
40+
// dependencies
41+
'bundledDependencies',
42+
'optionalDependencies',
43+
'peerDependencies',
44+
'dependencies',
45+
'devDependencies',
46+
'resolutions',
47+
48+
// types
49+
'types',
50+
'typings'
51+
];
52+
53+
const sort = (props) => {
54+
const unknown = [];
55+
const known = props.filter((prop) => {
56+
if (primary.includes(prop.key.value)) {
57+
return true;
58+
}
59+
unknown.push(prop);
60+
return false;
61+
});
62+
63+
known.sort((a, b) => {
64+
const aIndex = primary.indexOf(a.key.value);
65+
const bIndex = primary.indexOf(b.key.value);
66+
67+
return aIndex > bIndex ? 1 : aIndex < bIndex ? -1 : 0;
68+
});
69+
unknown.sort((a, b) => (a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0));
70+
71+
return known.concat(unknown);
72+
};
73+
74+
module.exports = { sort };

test/fixtures/fixture.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,25 @@
3838
"prettier"
3939
],
4040
"peerDependencies": {
41-
"prettier": "^1.18.2"
41+
"prettier": "^1.18.2",
42+
"eslint-config-shellscape": "^2.0.2"
43+
},
44+
"dependencies": {
45+
"nyc": "^14.1.0",
46+
"eslint-config-shellscape": "^2.0.2",
47+
"ava": "^2.2.0",
48+
"execa": "^2.0.3"
4249
},
4350
"typings": "dist/index.d.ts",
4451
"dependencies": {},
4552
"devDependencies": {
46-
"@commitlint/cli": "^8.1.0",
4753
"@commitlint/config-conventional": "^8.1.0",
54+
"lint-staged": "^9.2.0",
4855
"ava": "^2.2.0",
4956
"eslint-config-shellscape": "^2.0.2",
5057
"execa": "^2.0.3",
51-
"lint-staged": "^9.2.0",
5258
"nyc": "^14.1.0",
59+
"@commitlint/cli": "^8.1.0",
5360
"pre-commit": "^1.2.2",
5461
"prettier": "^1.18.2"
5562
},

test/snapshots/test.js.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Generated by [AVA](https://ava.li).
4848
"prettier"␊
4949
],␊
5050
"peerDependencies": {␊
51+
"eslint-config-shellscape": "^2.0.2",␊
5152
"prettier": "^1.18.2"␊
5253
},␊
5354
"dependencies": {},␊
@@ -130,6 +131,7 @@ Generated by [AVA](https://ava.li).
130131
"prettier"␊
131132
],␊
132133
"peerDependencies": {␊
134+
"eslint-config-shellscape": "^2.0.2",␊
133135
"prettier": "^1.18.2"␊
134136
},␊
135137
"dependencies": {},␊

test/snapshots/test.js.snap

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)