1 | # Prettier `package.json`
|
2 |
|
3 | `prettier-package-json` is an opinionated JSON formatter inspired by `prettier`. It removes all original styling and ensures that the outputted `package.json` conforms to a consistent style.
|
4 |
|
5 | ## Usage
|
6 |
|
7 | Install:
|
8 |
|
9 | ```sh
|
10 | yarn add prettier-package-json --dev
|
11 | ```
|
12 |
|
13 | You can install it globally if you like:
|
14 |
|
15 | ```sh
|
16 | yarn global add prettier-package-json
|
17 | ```
|
18 |
|
19 | _We're defaulting to yarn but you can use npm if you like:_
|
20 |
|
21 | ```sh
|
22 | npm install [-g] prettier-package-json
|
23 | ```
|
24 |
|
25 | ### CLI
|
26 |
|
27 | Run `prettier-package-json` through the CLI with this script. Run it without any arguments to see the options.
|
28 |
|
29 | To format a file in-place, use `--write`. You may want to consider committing your file before doing that, just in case.
|
30 |
|
31 | ```sh
|
32 | prettier-package-json [opts] [filename]
|
33 | ```
|
34 |
|
35 | In practice, this may look something like:
|
36 |
|
37 | ```sh
|
38 | prettier-package-json --write ./package.json
|
39 | ```
|
40 |
|
41 | #### Pre-commit hook for changed files
|
42 |
|
43 | 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.
|
44 |
|
45 | ##### 1. [lint-staged](https://github.com/okonet/lint-staged)
|
46 |
|
47 | Install it along with [husky](https://github.com/typicode/husky):
|
48 |
|
49 | ```bash
|
50 | yarn add lint-staged husky --dev
|
51 | ```
|
52 |
|
53 | and add this config to your `package.json`:
|
54 |
|
55 | ```json
|
56 | {
|
57 | "scripts": {
|
58 | "precommit": "lint-staged"
|
59 | },
|
60 | "lint-staged": {
|
61 | "package.json": [
|
62 | "prettier-package-json --write",
|
63 | "git add"
|
64 | ]
|
65 | }
|
66 | }
|
67 | ```
|
68 |
|
69 | See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
|
70 |
|
71 | ##### 2. bash script
|
72 |
|
73 | Alternately you can just save this script as `.git/hooks/pre-commit` and give it execute permission:
|
74 |
|
75 | ```bash
|
76 | #!/bin/sh
|
77 | packagejsonfiles=$(git diff --cached --name-only --diff-filter=ACM | grep 'package\.json$' | tr '\n' ' ')
|
78 | [ -z "$packagejsonfiles" ] && exit 0
|
79 |
|
80 | diffs=$(node_modules/.bin/prettier-package-json -l $packagejsonfiles)
|
81 | [ -z "$diffs" ] && exit 0
|
82 |
|
83 | echo "here"
|
84 | echo >&2 "package.json files must be formatted with prettier-package-json. Please run:"
|
85 | echo >&2 "node_modules/.bin/prettier-package-json --write "$diffs""
|
86 |
|
87 | exit 1
|
88 | ```
|
89 |
|
90 | ### API
|
91 |
|
92 | The API has two functions, exported as `format` and `check`. Usage is as follows:
|
93 |
|
94 | ```js
|
95 | const { format, check } = require("prettier-package-json");
|
96 |
|
97 | const options = {} // optional
|
98 |
|
99 | format(json, options);
|
100 | check(json, options);
|
101 | ```
|
102 |
|
103 | `check` checks to see if the file has been formatted with `prettier-package-json` given those options and returns a Boolean.
|
104 | This is similar to the `--list-different` parameter in the CLI and is useful for running in CI scenarios.
|
105 |
|
106 | ### Options
|
107 |
|
108 | `prettier-package-json` ships with a handful of customizable format options, usable in both the CLI and API.
|
109 |
|
110 | | Option | Default | CLI override | API override |
|
111 | | ------------- | ------------- | ------------- | ------------- |
|
112 | | **Tab Width** - Specify the number of spaces per indentation-level. | `2` | `--tab-width <int>` | `tabWidth: <int>` |
|
113 | | **Tabs** - Indent lines with tabs instead of spaces. | `false` | `--use-tabs` | `useTabs: <bool>` |
|
114 | | **Expand Users** - Expand author and contributors into objects. | `false` | `--expand-users` | `expandUsers: <bool>` |
|
115 | | **Key Order** - Specify the order of keys. | See [default options](https://github.com/cameronhunter/prettier-package-json/blob/master/src/defaultOptions.js) | `--key-order <comma,separated,list...>` | `keyOrder: <array>` |
|
116 |
|
117 | ## Contributing
|
118 |
|
119 | See [CONTRIBUTING.md](CONTRIBUTING.md).
|