New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

prettier-package-json

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prettier-package-json

Prettier formatter for package.json files

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
33K
decreased by-12.61%
Maintainers
1
Weekly downloads
 
Created
Source

Prettier package.json

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.

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.

1. lint-staged

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$' | tr '\n' ' ')
[ -z "$packagejsonfiles" ] && exit 0

diffs=$(node_modules/.bin/prettier-package-json -l $packagejsonfiles)
[ -z "$diffs" ] && exit 0

echo "here"
echo >&2 "package.json files must be formatted with prettier-package-json. Please run:"
echo >&2 "node_modules/.bin/prettier-package-json --write "$diffs""

exit 1

API

The API has two functions, exported as format and check. format usage is as follows:

const prettier = require("prettier-package-json");

const options = {} // optional
prettier.format(source, options);

check checks to see if the file has been formatted with prettier-package-json given those options and returns a Boolean. This is similar to the --list-different parameter in the CLI and is useful for running in CI scenarios.

Options

Prettier ships with a handful of customizable format options, usable in both the CLI and API.

OptionDefaultCLI overrideAPI override
Tab Width - Specify the number of spaces per indentation-level.2--tab-width <int>tabWidth: <int>
Tabs - Indent lines with tabs instead of spaces.false--use-tabsuseTabs: <bool>
Key Order - Specify the order of keys.See default options--key-orderkeyOrder: <comma,separated,list...>

Contributing

See CONTRIBUTING.md.

Keywords

FAQs

Package last updated on 01 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc