README
SnO2WMaN Folked Version
Features
- No Alphabetical Sorting in
scripts
package.json
Prettier
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.
Features
Consistent key order
Keys in package.json
will be sorted in an opinionated order but may be configured to your own preferences.
Input:
{
"description": "Prettier formatter for package.json files",
"name": "prettier-package-json",
"license": "MIT",
"version": "1.0.1",
"author": "Cameron Hunter <hello@cameronhunter.co.uk>"
}
Output:
{
"name": "prettier-package-json",
"description": "Prettier formatter for package.json files",
"author": "Cameron Hunter <hello@cameronhunter.co.uk>",
"license": "MIT",
"version": "1.0.1"
}
Sensibly sorted scripts
The scripts
field is sorted alphabetically but keeps pre
and post
scripts surrounding their named script.
Input:
{
"name": "prettier-package-json",
"version": "1.0.1",
"scripts": {
"test": "test",
"pretest": "pretest",
"version": "version",
"postversion": "postversion",
"build": "build"
}
}
Output:
{
"name": "prettier-package-json",
"version": "1.0.1",
"scripts": {
"build": "build",
"pretest": "pretest",
"test": "test",
"version": "version",
"postversion": "postversion"
}
}
Expand/contract author, contributors, and maintainers
The author
, contributors
and maintainers
fields will be shortened to their string versions and sorted by name. Use
the --expand-users
option if you prefer user objects.
Input:
{
"name": "prettier-package-json",
"version": "1.0.1",
"author": {
"name": "Cameron Hunter",
"email": "hello@cameronhunter.co.uk",
"url": "https://cameronhunter.co.uk"
},
"contributors": [
"Barry",
"Adam <adam@email.com>"
]
}
Output:
{
"name": "prettier-package-json",
"version": "1.0.1",
"author": "Cameron Hunter <hello@cameronhunter.co.uk> (https://cameronhunter.co.uk)",
"contributors": [
"Adam <adam@email.com>",
"Barry"
]
}
Filter and sort files field
Some files are included or excluded automatically by npm
, these are removed from the files
field before sorting
alphabetically.
Input:
{
"name": "prettier-package-json",
"version": "1.0.1",
"main": "src/index.js",
"files": [
"src/index.js",
"src",
"CHANGELOG.md",
"readme.md",
"package-lock.json"
]
}
Output:
{
"name": "prettier-package-json",
"version": "1.0.1",
"main": "src/index.js",
"files": [
"src"
]
}
Usage
Install:
yarn add prettier-package-json --dev
You can install it globally if you like:
yarn global add prettier-package-json
We're defaulting to yarn but you can use npm if you like:
npm install [-g] prettier-package-json
CLI
Run prettier-package-json
through the CLI with this script. Run it without any arguments to see the options.
To format a file in-place, use --write
. You may want to consider committing your file before doing that, just in case.
prettier-package-json [opts] [filename]
In practice, this may look something like:
prettier-package-json --write ./package.json
Pre-commit hook for changed files
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.
lint-staged
1.Install it along with husky:
yarn add lint-staged husky --dev
and add this config to your package.json
:
{
"scripts": {
"precommit": "lint-staged"
},
"lint-staged": {
"package.json": [
"prettier-package-json --write",
"git add"
]
}
}
See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
2. bash script
Alternately you can just save this script as .git/hooks/pre-commit
and give it execute permission:
#!/bin/sh
packagejsonfiles=$(git diff --cached --name-only --diff-filter=ACM | grep 'package\.json