UNPKG

8.1 kBMarkdownView Raw
1# Prettier `package.json`
2
3[![CI](https://github.com/cameronhunter/prettier-package-json/actions/workflows/ci.yml/badge.svg)](https://github.com/cameronhunter/prettier-package-json/actions/workflows/ci.yml) [![npm](https://img.shields.io/npm/v/prettier-package-json.svg)](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
11Keys in `package.json` will be sorted in an [opinionated order](src/defaultOptions.ts) but may be configured to your own preferences.
12
13Input:
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
25Output:
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
39The `scripts` field is sorted alphabetically but keeps `pre` and `post` scripts surrounding their named script.
40
41Input:
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
57Output:
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
75The `author`, `contributors` and `maintainers` fields will be shortened to their string versions and sorted by name. Use
76the `--expand-users` option if you prefer user objects.
77
78Input:
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
93Output:
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
106Some files are included or excluded automatically by `npm`, these are removed from the `files` field before sorting
107alphabetically.
108
109Input:
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
120Output:
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
133Install:
134
135```sh
136yarn add prettier-package-json --dev
137```
138
139You can install it globally if you like:
140
141```sh
142yarn global add prettier-package-json
143```
144
145_We're defaulting to yarn but you can use npm if you like:_
146
147```sh
148npm install [-g] prettier-package-json
149```
150
151### CLI
152
153Run `prettier-package-json` through the CLI with this script. Run it without any arguments to see the options.
154
155To format a file in-place, use `--write`. You may want to consider committing your file before doing that, just in case.
156
157```sh
158prettier-package-json [opts] [filename]
159```
160
161In practice, this may look something like:
162
163```sh
164prettier-package-json --write ./package.json
165```
166
167#### Pre-commit hook for changed files
168
169You 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
173Install it along with [husky](https://github.com/cameronhunter/husky):
174
175```bash
176yarn add lint-staged husky --dev
177```
178
179and 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
192See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
193
194##### 2. bash script
195
196Alternately you can just save this script as `.git/hooks/pre-commit` and give it execute permission:
197
198```bash
199#!/bin/sh
200packagejsonfiles=$(git diff --cached --name-only --diff-filter=ACM | grep 'package\.json$' | tr '\n' ' ')
201[ -z "$packagejsonfiles" ] && exit 0
202
203diffs=$(node_modules/.bin/prettier-package-json -l $packagejsonfiles)
204[ -z "$diffs" ] && exit 0
205
206echo "here"
207echo >&2 "package.json files must be formatted with prettier-package-json. Please run:"
208echo >&2 "node_modules/.bin/prettier-package-json --write "$diffs""
209
210exit 1
211```
212
213### API
214
215The API has two functions, exported as `format` and `check`. Usage is as follows:
216
217```js
218import { format, check } from 'prettier-package-json';
219
220const options = {}; // optional
221
222format(json, options);
223check(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.
227This is similar to the `--list-different` parameter in the CLI and is useful for running in CI scenarios.
228
229### CI
230
231For usage in CI scenarios, you can use the `--list-different` CLI flag. The command will list all invalid files and return
232with a proper default error code, so that in case of an error or invalid file the build pipeline automatically fails.
233
234These 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
240These 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
253A 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
260Configuration file may also be passed using the `--config` CLI parameter.
261
262## Contributing
263
264See [CONTRIBUTING.md](CONTRIBUTING.md).