UNPKG

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