npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tillig/json-sort-cli

v2.0.2

Published

CLI interface to json-stable-stringify that can also be used as a pre-commit hook.

Downloads

875

Readme

JSON Stable Stringify CLI and Pre-Commit Hook

This is a command line interface to the json-stable-stringify library so you can format/sort JSON files using that algorithm. It supports .editorconfig and provides a pre-commit hook configuration.

Use as CLI

# Install
npm install -g @tillig/json-sort-cli

# See the options available
json-sort --help

# Check if files are sorted
json-sort *.json --insert-final-newline true

# Sort files if needed
json-sort *.json --insert-final-newline true --autofix

Use as Pre-Commit Hook

This is similar to the pretty-format-json hook with the following differences:

  • Uses json-stable-stringify as the algorithm for sorting and formatting (for example, empty arrays will be expanded to two lines).
  • Supports JSON with comments (but will remove any comments on sort!).
  • Obeys .editorconfig, if present, for indents and line endings.

For basic checking, your hook will look like this:

repos:
  - repo: https://github.com/tillig/json-sort-cli
    rev: v1.0.0 # Use a version tag or SHA commit hash
    hooks:
      - id: json-sort

You can also pass arguments to control behavior, like auto-fixing any issues:

repos:
  - repo: https://github.com/tillig/json-sort-cli
    rev: v1.0.0 # Use a version tag or SHA commit hash
    hooks:
      - id: json-sort
        args:
          - --autofix

Controlling Formatting

The default formatting for json-sort is:

  • Two-spaces for indents.
  • System newline for line endings.
  • No final newline at the end of the content.

json-sort will obey .editorconfig settings in your repo. A sample .editorconfig might look like this:

root = true

# Default for all files
[*]
indent_style = space
insert_final_newline = true

# Specific overrides for JSON files
[*.json]
indent_size = 2

The specific .editorconfig directives that can affect formatting are:

  • end_of_line: Set to lf, cr, or crlf to specify the line ending type. If unset, the system line ending will be used.
  • indent_size: The number of spaces to use for an indent. If indent_style is tab, this value is ignored.
  • indent_style: Set to tab or space to use hard or soft tabs respectively.
  • insert_final_newline: Set to true to add a newline at the end of the sorted content, false to skip adding the line ending.

You can specify command line options to control formatting as well. These correspond to the .editorconfig settings.

  • --end-of-line
  • --indent-size
  • --indent-style
  • --insert-final-newline

Command line options take precedence over .editorconfig settings; .editorconfig settings override defaults.

Warn vs. Autofix

If json-sort detects any changes in content due to sorting, it will exit with a non-zero code.

By default, the formatter is non-destructive - it will warn you if the sorted content is different from the original content. This allows for easier integration with pre-commit hooks and build scripts where you don't want these things automatically changing content.

If you want the formatter to overwrite the existing file with the sorted content, specify the --autofix argument.

Console output from the command will tell you if the difference is structural or whitespace. Since the sort command obeys .editorconfig there are sometimes non-obvious differences - file encoding, tabs vs. spaces, etc. The messages will tell you what the sort thinks needs changing.