All Projects → cameronhunter → Prettier Package Json

cameronhunter / Prettier Package Json

Prettier formatter for package.json files

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Prettier Package Json

Prettier
Prettier is an opinionated code formatter.
Stars: ✭ 41,411 (+48052.33%)
Mutual labels:  prettier, printer, json
Git Praise
A nicer git blame.
Stars: ✭ 24 (-72.09%)
Mutual labels:  prettier, pretty-print, cli
Html Table Cli
Create interactive tables from JSON on the command-line
Stars: ✭ 23 (-73.26%)
Mutual labels:  cli, json
Bashmultitool
A library for bash shell program containing useful functions. Can be imported into scripts to create colourful and functional scripts and TUIs.
Stars: ✭ 27 (-68.6%)
Mutual labels:  printer, cli
Fast Xml Parser
Validate XML, Parse XML to JS/JSON and vise versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
Stars: ✭ 1,021 (+1087.21%)
Mutual labels:  cli, json
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+782.56%)
Mutual labels:  cli, json
Kt
Kafka command line tool that likes JSON
Stars: ✭ 799 (+829.07%)
Mutual labels:  cli, json
Pretty Print Json
🦋 Pretty-print JSON data into HTML to indent and colorize (written in TypeScript)
Stars: ✭ 41 (-52.33%)
Mutual labels:  pretty-print, json
Trdsql
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.
Stars: ✭ 593 (+589.53%)
Mutual labels:  cli, json
Http Prompt
An interactive command-line HTTP and API testing client built on top of HTTPie featuring autocomplete, syntax highlighting, and more. https://twitter.com/httpie
Stars: ✭ 8,329 (+9584.88%)
Mutual labels:  cli, json
Saw
Fast, multi-purpose tool for AWS CloudWatch Logs
Stars: ✭ 1,071 (+1145.35%)
Mutual labels:  cli, json
Colorjson
Fast Color JSON Marshaller + Pretty Printer for Golang
Stars: ✭ 71 (-17.44%)
Mutual labels:  pretty-print, json
Mevn Cli
Light speed setup for MEVN(Mongo Express Vue Node) Apps
Stars: ✭ 696 (+709.3%)
Mutual labels:  prettier, cli
Structured Text Tools
A list of command line tools for manipulating structured text data
Stars: ✭ 6,180 (+7086.05%)
Mutual labels:  cli, json
Ponzu
Headless CMS with automatic JSON API. Featuring auto-HTTPS from Let's Encrypt, HTTP/2 Server Push, and flexible server framework written in Go.
Stars: ✭ 5,373 (+6147.67%)
Mutual labels:  cli, json
Jplot
iTerm2 expvar/JSON monitoring tool
Stars: ✭ 949 (+1003.49%)
Mutual labels:  cli, json
Plugin Php
Prettier PHP Plugin
Stars: ✭ 1,243 (+1345.35%)
Mutual labels:  prettier, printer
Prettier Eslint Cli
CLI for prettier-eslint
Stars: ✭ 451 (+424.42%)
Mutual labels:  prettier, cli
Ramda Cli
🐏 A CLI tool for processing data with functional pipelines
Stars: ✭ 515 (+498.84%)
Mutual labels:  cli, json
Rdoc
colourised R docs in the terminal
Stars: ✭ 49 (-43.02%)
Mutual labels:  pretty-print, cli

Prettier package.json

Build Status npm

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 <[email protected]>"
}

Output:

{
  "name": "prettier-package-json",
  "description": "Prettier formatter for package.json files",
  "author": "Cameron Hunter <[email protected]>",
  "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": "[email protected]",
    "url": "https://cameronhunter.co.uk"
  },
  "contributors": [
    "Barry",
    "Adam <[email protected]>"
  ]
}

Output:

{
  "name": "prettier-package-json",
  "version": "1.0.1",
  "author": "Cameron Hunter <[email protected]> (https://cameronhunter.co.uk)",
  "contributors": [
    "Adam <[email protected]>",
    "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.

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. Usage is as follows:

const { format, check } = require("prettier-package-json");

const options = {}; // optional

format(json, options);
check(json, 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.

CI

For usage in CI scenarios, you can use the --list-different CLI flag. The command will list all invalid files and return with a proper default error code, so that in case of an error or invalid file the build pipeline automatically fails.

These are the status codes:

  • 0: all files valid, no error occured.
  • 1: an error ocurred, for example a JSON parse error. See message on stderr for details.
  • 2: not all files are valid.

These exit codes are only set when in --list-different mode.

Options

prettier-package-json ships with a handful of customizable format options, usable in both the CLI and API.

Option Default CLI override API 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-tabs useTabs: <bool>
Expand Users - Expand author and contributors into objects. false --expand-users expandUsers: <bool>
Key Order - Specify the order of keys. See default options --key-order <comma,separated,list...> keyOrder: <array|function>

Contributing

See CONTRIBUTING.md.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].