1 | # Prettier `package.json`
|
2 |
|
3 | [](https://github.com/cameronhunter/prettier-package-json/actions/workflows/ci.yml) [](https://www.npmjs.com/package/prettier-package-json)
|
4 |
|
5 | `prettier-package-json` is a JSON formatter inspired by `prettier`. It removes all original styling and ensures that the outputted `package.json` conforms to a consistent style. By default it uses opinionated defaults but can be configured to your individual needs.
|
6 |
|
7 | ## Features
|
8 |
|
9 | ### Consistent key order
|
10 |
|
11 | Keys in `package.json` will be sorted in an [opinionated order](src/defaultOptions.ts) but may be configured to your own preferences.
|
12 |
|
13 | Input:
|
14 |
|
15 | ```json
|
16 | {
|
17 | "description": "Prettier formatter for package.json files",
|
18 | "name": "prettier-package-json",
|
19 | "license": "MIT",
|
20 | "version": "1.0.1",
|
21 | "author": "Cameron Hunter <hello@cameronhunter.co.uk>"
|
22 | }
|
23 | ```
|
24 |
|
25 | Output:
|
26 |
|
27 | ```json
|
28 | {
|
29 | "name": "prettier-package-json",
|
30 | "description": "Prettier formatter for package.json files",
|
31 | "author": "Cameron Hunter <hello@cameronhunter.co.uk>",
|
32 | "license": "MIT",
|
33 | "version": "1.0.1"
|
34 | }
|
35 | ```
|
36 |
|
37 | ### Sensibly sorted scripts
|
38 |
|
39 | The `scripts` field is sorted alphabetically but keeps `pre` and `post` scripts surrounding their named script.
|
40 |
|
41 | Input:
|
42 |
|
43 | ```json
|
44 | {
|
45 | "name": "prettier-package-json",
|
46 | "version": "1.0.1",
|
47 | "scripts": {
|
48 | "test": "test",
|
49 | "pretest": "pretest",
|
50 | "version": "version",
|
51 | "postversion": "postversion",
|
52 | "build": "build"
|
53 | }
|
54 | }
|
55 | ```
|
56 |
|
57 | Output:
|
58 |
|
59 | ```json
|
60 | {
|
61 | "name": "prettier-package-json",
|
62 | "version": "1.0.1",
|
63 | "scripts": {
|
64 | "build": "build",
|
65 | "pretest": "pretest",
|
66 | "test": "test",
|
67 | "version": "version",
|
68 | "postversion": "postversion"
|
69 | }
|
70 | }
|
71 | ```
|
72 |
|
73 | ### Expand/contract author, contributors, and maintainers
|
74 |
|
75 | The `author`, `contributors` and `maintainers` fields will be shortened to their string versions and sorted by name. Use
|
76 | the `--expand-users` option if you prefer user objects.
|
77 |
|
78 | Input:
|
79 |
|
80 | ```json
|
81 | {
|
82 | "name": "prettier-package-json",
|
83 | "version": "1.0.1",
|
84 | "author": {
|
85 | "name": "Cameron Hunter",
|
86 | "email": "hello@cameronhunter.co.uk",
|
87 | "url": "https://cameronhunter.co.uk"
|
88 | },
|
89 | "contributors": ["Barry", "Adam <adam@email.com>"]
|
90 | }
|
91 | ```
|
92 |
|
93 | Output:
|
94 |
|
95 | ```json
|
96 | {
|
97 | "name": "prettier-package-json",
|
98 | "version": "1.0.1",
|
99 | "author": "Cameron Hunter <hello@cameronhunter.co.uk> (https://cameronhunter.co.uk)",
|
100 | "contributors": ["Adam <adam@email.com>", "Barry"]
|
101 | }
|
102 | ```
|
103 |
|
104 | ### Filter and sort files field
|
105 |
|
106 | Some files are included or excluded automatically by `npm`, these are removed from the `files` field before sorting
|
107 | alphabetically.
|
108 |
|
109 | Input:
|
110 |
|
111 | ```json
|
112 | {
|
113 | "name": "prettier-package-json",
|
114 | "version": "1.0.1",
|
115 | "main": "src/index.js",
|
116 | "files": ["src/index.js", "src", "CHANGELOG.md", "readme.md", "package-lock.json"]
|
117 | }
|
118 | ```
|
119 |
|
120 | Output:
|
121 |
|
122 | ```json
|
123 | {
|
124 | "name": "prettier-package-json",
|
125 | "version": "1.0.1",
|
126 | "main": "src/index.js",
|
127 | "files": ["src"]
|
128 | }
|
129 | ```
|
130 |
|
131 | ## Usage
|
132 |
|
133 | Install:
|
134 |
|
135 | ```sh
|
136 | yarn add prettier-package-json --dev
|
137 | ```
|
138 |
|
139 | You can install it globally if you like:
|
140 |
|
141 | ```sh
|
142 | yarn global add prettier-package-json
|
143 | ```
|
144 |
|
145 | _We're defaulting to yarn but you can use npm if you like:_
|
146 |
|
147 | ```sh
|
148 | npm install [-g] prettier-package-json
|
149 | ```
|
150 |
|
151 | ### CLI
|
152 |
|
153 | Run `prettier-package-json` through the CLI with this script. Run it without any arguments to see the options.
|
154 |
|
155 | To format a file in-place, use `--write`. You may want to consider committing your file before doing that, just in case.
|
156 |
|
157 | ```sh
|
158 | prettier-package-json [opts] [filename]
|
159 | ```
|
160 |
|
161 | In practice, this may look something like:
|
162 |
|
163 | ```sh
|
164 | prettier-package-json --write ./package.json
|
165 | ```
|
166 |
|
167 | #### Pre-commit hook for changed files
|
168 |
|
169 | You can use this with a pre-commit tool. This can re-format your files that are marked as "staged" via git add before you commit.
|
170 |
|
171 | ##### 1. [lint-staged](https://github.com/okonet/lint-staged)
|
172 |
|
173 | Install it along with [husky](https://github.com/cameronhunter/husky):
|
174 |
|
175 | ```bash
|
176 | yarn add lint-staged husky --dev
|
177 | ```
|
178 |
|
179 | and add this config to your `package.json`:
|
180 |
|
181 | ```json
|
182 | {
|
183 | "scripts": {
|
184 | "precommit": "lint-staged"
|
185 | },
|
186 | "lint-staged": {
|
187 | "package.json": ["prettier-package-json --write", "git add"]
|
188 | }
|
189 | }
|
190 | ```
|
191 |
|
192 | See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
|
193 |
|
194 | ##### 2. bash script
|
195 |
|
196 | Alternately you can just save this script as `.git/hooks/pre-commit` and give it execute permission:
|
197 |
|
198 | ```bash
|
199 | #!/bin/sh
|
200 | packagejsonfiles=$(git diff --cached --name-only --diff-filter=ACM | grep 'package\.json$' | tr '\n' ' ')
|
201 | [ -z "$packagejsonfiles" ] && exit 0
|
202 |
|
203 | diffs=$(node_modules/.bin/prettier-package-json -l $packagejsonfiles)
|
204 | [ -z "$diffs" ] && exit 0
|
205 |
|
206 | echo "here"
|
207 | echo >&2 "package.json files must be formatted with prettier-package-json. Please run:"
|
208 | echo >&2 "node_modules/.bin/prettier-package-json --write "$diffs""
|
209 |
|
210 | exit 1
|
211 | ```
|
212 |
|
213 | ### API
|
214 |
|
215 | The API has two functions, exported as `format` and `check`. Usage is as follows:
|
216 |
|
217 | ```js
|
218 | import { format, check } from 'prettier-package-json';
|
219 |
|
220 | const options = {}; // optional
|
221 |
|
222 | format(json, options);
|
223 | check(json, options);
|
224 | ```
|
225 |
|
226 | `check` checks to see if the file has been formatted with `prettier-package-json` given those options and returns a Boolean.
|
227 | This is similar to the `--list-different` parameter in the CLI and is useful for running in CI scenarios.
|
228 |
|
229 | ### CI
|
230 |
|
231 | For usage in CI scenarios, you can use the `--list-different` CLI flag. The command will list all invalid files and return
|
232 | with a proper default error code, so that in case of an error or invalid file the build pipeline automatically fails.
|
233 |
|
234 | These are the status codes:
|
235 |
|
236 | - `0`: all files valid, no error occured.
|
237 | - `1`: an error ocurred, for example a JSON parse error. See message on `stderr` for details.
|
238 | - `2`: not all files are valid.
|
239 |
|
240 | These exit codes are only set when in `--list-different` mode.
|
241 |
|
242 | ### Options
|
243 |
|
244 | `prettier-package-json` ships with a handful of customizable format options, usable in both the CLI, API, and configuration file.
|
245 |
|
246 | | Option | Default | CLI override | API override |
|
247 | | ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | --------------------------------------- | ----------------------------- |
|
248 | | **Tab Width** - Specify the number of spaces per indentation-level. | `2` | `--tab-width <int>` | `tabWidth: <int>` |
|
249 | | **Tabs** - Indent lines with tabs instead of spaces. | `false` | `--use-tabs` | `useTabs: <bool>` |
|
250 | | **Expand Users** - Expand author and contributors into objects. | `false` | `--expand-users` | `expandUsers: <bool>` |
|
251 | | **Key Order** - Specify the order of keys. | See [default options](src/defaultOptions.ts) | `--key-order <comma,separated,list...>` | `keyOrder: <array\|function>` |
|
252 |
|
253 | A configuration file will be searched for using [`cosmiconfig`](https://www.npmjs.com/package/cosmiconfig) rules:
|
254 |
|
255 | - `prettier-package-json` field in `package.json`.
|
256 | - `prettier-package-json` file (JSON or YAML), extentionless "rc" file.
|
257 | - `prettier-package-json` file with the extensions `.json`, `.yaml`, `.yml`, `.js`, or `.cjs`.
|
258 | - `prettier-package-json.config.js` or `prettier-package-json.config.cjs` CommonJS module.
|
259 |
|
260 | Configuration file may also be passed using the `--config` CLI parameter.
|
261 |
|
262 | ## Contributing
|
263 |
|
264 | See [CONTRIBUTING.md](CONTRIBUTING.md).
|