pretty-package-json
v1.0.0
Published
Prettier for `package.json`.
Downloads
7
Readme
pretty-package-json
Prettier for
package.json
.
Overview
I like my package.json
files to be nearly organized, making sure that
each property is in the same order as listed in the official npm package.json
documentation,
including nested objects like author
, contributors
, directories
and more.
This tools allows me to stop doing that manually. I just
%!pretty-package-json
from Vim and call it a day.
Usage
Format given file, stdin
or package.json
otherwise according to
rules, and print it to stdout
:
pretty-package-json whatever.json
pretty-package-json < whatever.json
pretty-package-json
Format given file or package.json
otherwise, and overwrite it:
pretty-package-json --write whatever.json
pretty-package-json --write
Rules
The top-level keys are sorted as defined in the documentation.
name
version
description
keywords
homepage
bugs
license
author
contributors
funding
files
main
browser
bin
man
directories
repository
scripts
config
dependencies
devDependencies
peerDependencies
peerDependenciesMeta
bundledDependencies
optionalDependencies
overrides
engines
os
cpu
private
publishConfig
workspaces
Those are automatically fetched from the documentation. But for nested object keys, there's extra rules that need to be defined manually (see below).
We also support extra keys that are not part of the npm
documentation but are commonly used in the ecosystem, like type
,
module
, exports
, and types
.
Sorting and unknown keys
Anywhere we're sorting according to a predefined order, unknown keys will be added at the end in the same order they were found.
Empty structures
Empty arrays and objects are removed.
Redundant homepage
and bugs
If the homepage
and bugs
match the one that can be derived from
the repository
by hosted-git-info,
the keys will be removed.
For example:
{
"homepage": "https://github.com/valeriangalliat/pretty-package-json",
"bugs": "https://github.com/valeriangalliat/pretty-package-json/issues",
"repository": "valeriangalliat/pretty-package-json"
}
Here the homepage
and bugs
are redundant and it will be rewritten
as:
{
"repository": "valeriangalliat/pretty-package-json"
}
author
and contributors
Converted to the short form name <email> (url)
unless peopleFormat
is set to object
, then it's sorted as:
name
email
url
bin
If contains only a single script matching the package name, it's flattened as a string.
directories
bin
doc
lib
man
repository
Convert to the shortest form supported, e.g. use/repo
if hosted on
GitHub, gitlab:user/repo
, a full URL, or otherwise sorted as:
type
url
directory
scripts
Sorted alphabetically unless sortScripts
is set to false
.
dependencies
, devDependencies
, peerDependencies
and optionalDependencies
Sorted alphabetically like npm does by default when populating those objects.
engines
node
npm
Extra keys
$schema
: for JSON Schema validation.type
: Node.js input type, e.g."type": "module"
or"type": "commonjs"
.module
: Node.js legacy method that allowed to define the ES module entry point, as opposed to CommonJS inmain
.exports
: Node.js field allowing to define hybrid entry points.types
: TypeScript types.
To confirm this list and see the non-npm top-level keys that were manually added, run:
git diff --no-index npm-keys.json keys.json