Skip to content

Commit b123e1e

Browse files
committed
feat: yarn key support, sort scripts
1 parent d2e6857 commit b123e1e

File tree

8 files changed

+202
-14
lines changed

8 files changed

+202
-14
lines changed

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,54 @@ $ npx prettier --write package.json
4848

4949
This plugin enforces its own set of opinionated rules:
5050

51+
### Scripts
52+
53+
Keys in `scripts` are ordered alphabetically. Use prefixes wisely to properly order child scripts. e.g. `lint`, `lint:ts`.
54+
5155
### Sorting
5256

5357
Top-level keys are sorted according to a style commonly seen in the packages of [@sindresorhus](https://github.com/sindresorhus). Known keys, and their order are:
5458

5559
```js
5660
[
61+
// meta
5762
'name',
5863
'version',
64+
'flat',
65+
'private',
5966
'publishConfig',
6067
'description',
6168
'license',
6269
'repository',
6370
'author',
6471
'homepage',
6572
'bugs',
73+
74+
// entry
6675
'main',
76+
'bin',
77+
78+
// constraints
6779
'engines',
80+
'cpu',
81+
'os',
82+
83+
// content and util
6884
'scripts',
6985
'files',
7086
'keywords',
71-
'peerDependencies',
87+
88+
// dependencies
89+
'bundledDependencies',
7290
'optionalDependencies',
91+
'peerDependencies',
7392
'dependencies',
74-
'devDependencies'
93+
'devDependencies',
94+
'resolutions'
7595
]
7696
```
7797

78-
Unknown keys, or keys not part of the list above, will be alphabetically sorted and added to the end of the file.
98+
Unknown keys, or keys not part of the list above, will be alphabetically sorted and added to the end of the file. Note that this list takes into account both `npm` and `yarn` keys.
7999

80100
### Forthcoming
81101

@@ -85,7 +105,6 @@ Forthcoming rules include:
85105
- [ ] Engines format
86106
- [ ] Files order and content
87107
- [ ] Repository format
88-
- [ ] Scripts order
89108

90109
## Meta
91110

lib/index.js

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

13+
const { scripts } = require('./scripts');
1314
const { sort } = require('./sort');
1415

1516
const { 'json-stringify': parser } = parsers;
1617
const rePkg = /package\.json$/;
1718

1819
const format = (input) => {
19-
const result = JSON.stringify(sort(input), null, 2);
20+
let pkg = JSON.parse(input);
21+
pkg = sort(pkg);
22+
pkg = scripts(pkg);
23+
24+
const result = JSON.stringify(pkg, null, 2);
2025
return result;
2126
};
2227

lib/scripts.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 sortScripts = (pkg) => {
12+
const { scripts } = pkg;
13+
14+
if (!scripts) {
15+
return pkg;
16+
}
17+
18+
const result = Object.assign({}, pkg, { scripts: {} });
19+
const keys = Object.keys(scripts).sort();
20+
21+
for (const key of keys) {
22+
if (scripts[key]) {
23+
result.scripts[key] = scripts[key];
24+
}
25+
}
26+
27+
return result;
28+
};
29+
30+
module.exports = { scripts: sortScripts };

lib/sort.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,43 @@
99
included in all copies or substantial portions of this Source Code Form.
1010
*/
1111
const primary = [
12+
// meta
1213
'name',
1314
'version',
15+
'flat',
16+
'private',
17+
'publishConfig',
1418
'description',
1519
'license',
1620
'repository',
1721
'author',
1822
'homepage',
1923
'bugs',
24+
25+
// entry
2026
'main',
27+
'bin',
28+
29+
// constraints
2130
'engines',
31+
'cpu',
32+
'os',
33+
34+
// content and util
2235
'scripts',
2336
'files',
2437
'keywords',
38+
39+
// dependencies
40+
'bundledDependencies',
41+
'optionalDependencies',
2542
'peerDependencies',
2643
'dependencies',
27-
'devDependencies'
44+
'devDependencies',
45+
'resolutions'
2846
];
2947

30-
const sort = (input) => {
31-
const pkg = JSON.parse(input);
48+
const sort = (pkg) => {
3249
const result = {};
3350
const unknown = Object.keys(pkg)
3451
.filter((key) => !primary.includes(key))

test/fixtures/fixture.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
},
1414
"scripts": {
1515
"ci:coverage": "nyc npm run ci:test && nyc report --reporter=text-lcov > coverage.lcov",
16+
"lint": "eslint --fix --cache lib test && prettier --write package.json",
1617
"ci:lint": "npm run lint && npm run security",
18+
"security": "npm audit",
1719
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
1820
"ci:test": "npm run test -- --verbose",
1921
"commitlint": "commitlint",
20-
"commitmsg": "commitlint -e $GIT_PARAMS",
21-
"lint": "eslint --fix --cache lib test && prettier --write package.json",
22-
"lint-staged": "lint-staged",
2322
"prepublishOnly": "npm run lint",
24-
"security": "npm audit",
25-
"test": "ava"
23+
"commitmsg": "commitlint -e $GIT_PARAMS",
24+
"test": "ava",
25+
"lint-staged": "lint-staged"
2626
},
2727
"files": [
2828
"lib/",

test/snapshots/test.js.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,82 @@ Generated by [AVA](https://ava.li).
8282
"pre-commit": "lint-staged"␊
8383
}␊
8484
`
85+
86+
## preprocess
87+
88+
> Snapshot 1
89+
90+
`{␊
91+
"name": "prettier-plugin-package",␊
92+
"version": "0.1.1",␊
93+
"description": "An opinionated package.json formatter plugin for Prettier",␊
94+
"license": "MPL-2.0",␊
95+
"repository": "shellscape/prettier-plugin-package",␊
96+
"author": "shellscape",␊
97+
"homepage": "https://github.com/shellscape/prettier-plugin-package",␊
98+
"bugs": "https://github.com/shellscape/prettier-plugin-package/issues",␊
99+
"main": "lib/index.js",␊
100+
"engines": {␊
101+
"node": ">= 8.0.0"␊
102+
},␊
103+
"scripts": {␊
104+
"ci:coverage": "nyc npm run ci:test && nyc report --reporter=text-lcov > coverage.lcov",␊
105+
"ci:lint": "npm run lint && npm run security",␊
106+
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",␊
107+
"ci:test": "npm run test -- --verbose",␊
108+
"commitlint": "commitlint",␊
109+
"commitmsg": "commitlint -e $GIT_PARAMS",␊
110+
"lint": "eslint --fix --cache lib test && prettier --write package.json",␊
111+
"lint-staged": "lint-staged",␊
112+
"prepublishOnly": "npm run lint",␊
113+
"security": "npm audit",␊
114+
"test": "ava"␊
115+
},␊
116+
"files": [␊
117+
"lib/",␊
118+
"README.md",␊
119+
"LICENSE"␊
120+
],␊
121+
"keywords": [␊
122+
"package",␊
123+
"package.json",␊
124+
"plugin",␊
125+
"prettier"␊
126+
],␊
127+
"peerDependencies": {␊
128+
"prettier": "^1.18.2"␊
129+
},␊
130+
"dependencies": {},␊
131+
"devDependencies": {␊
132+
"@commitlint/cli": "^8.1.0",␊
133+
"@commitlint/config-conventional": "^8.1.0",␊
134+
"ava": "^2.2.0",␊
135+
"eslint-config-shellscape": "^2.0.2",␊
136+
"execa": "^2.0.3",␊
137+
"lint-staged": "^9.2.0",␊
138+
"nyc": "^14.1.0",␊
139+
"pre-commit": "^1.2.2",␊
140+
"prettier": "^1.18.2"␊
141+
},␊
142+
"ava": {␊
143+
"files": [␊
144+
"!**/fixtures/**"␊
145+
]␊
146+
},␊
147+
"lint-staged": {␊
148+
"*.js": [␊
149+
"eslint --fix",␊
150+
"git add"␊
151+
]␊
152+
},␊
153+
"nyc": {␊
154+
"include": [␊
155+
"lib/*.js"␊
156+
],␊
157+
"exclude": [␊
158+
"test/"␊
159+
]␊
160+
},␊
161+
"pre-commit": "lint-staged"␊
162+
}␊
163+
`

test/snapshots/test.js.snap

59 Bytes
Binary file not shown.

test/test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,32 @@ test('randomize', (t) => {
1818
const options = {
1919
filepath: join(__dirname, 'package.json'),
2020
parser: 'json-stringify',
21-
plugins: ['.']
21+
plugins: ['.'],
22+
shit: 'balls'
23+
};
24+
const keys = shuffle(Object.keys(pkg));
25+
const fixture = {};
26+
27+
for (const key of keys) {
28+
fixture[key] = pkg[key];
29+
}
30+
31+
const input = JSON.stringify(fixture, null, 2);
32+
const output = prettier.format(input, options);
33+
34+
t.snapshot(output);
35+
});
36+
37+
test('preprocess', (t) => {
38+
const options = {
39+
filepath: join('package.json'),
40+
parser: 'json-stringify',
41+
plugins: ['.'],
42+
preprocess: (input) => {
43+
const { version, repository } = JSON.parse(input);
44+
const result = { repository, version };
45+
return result;
46+
}
2247
};
2348
const keys = shuffle(Object.keys(pkg));
2449
const fixture = {};
@@ -32,3 +57,16 @@ test('randomize', (t) => {
3257

3358
t.snapshot(output);
3459
});
60+
61+
test('not package.json', (t) => {
62+
const options = {
63+
filepath: 'batman.json',
64+
parser: 'json-stringify',
65+
plugins: ['.']
66+
};
67+
const fixture = { version: 'batman', name: 'joker' };
68+
const input = JSON.stringify(fixture, null, 2);
69+
const output = prettier.format(input, options);
70+
71+
t.is(input.trim(), output.trim());
72+
});

0 commit comments

Comments
 (0)