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

compare-json-files

v1.1.0

Published

Compare and test JSON files. Useful for i18n translations. You can check if the keys are missing or the values are different.

Downloads

10

Readme

compare-json-files

compare-json-files is a CLI tool designed to compare translation JSON files and identify missing keys across different language files. It ensures consistency in translations by preventing commits if there are discrepancies.

Features

  • Compare translation JSON files in a specified directory.
  • Detect missing keys across translation files.
  • Display results in an easy-to-read table format, including file paths and line numbers.
  • Use as a pre-commit hook to prevent incomplete translation commits.

Installation

First, install the package globally or as a local dependency in your project.

Global Installation

npm install -g compare-json-files

Local Installation (as a project dependency)

npm install compare-json-files --save-dev

Usage

The tool can be run via the command line in any project with translation files.

compare-json-files [options]

By default, the tool expects your translation JSON files to be located in a folder named langs. If your files are in a different directory, you can specify it using the --dir option.

Command Line Options

-d, --dir : The directory where your translation files are located. Default is "langs".

Example 1: Using Default Directory

compare-json-files

This will compare the JSON files located in the langs directory and display any missing keys.

Example 2: Specifying a Custom Directory

compare-json-files --dir ./i18n/translations

This will look for translation files in the i18n/translations directory. Output

For every file, it checks and lists all missing keys, referencing the location of the missing key in both the missing file and the reference file. If no issues are found, it reports that the files are consistent.

File: en.json
Missing keys (2):
┌──────────┬──────────────────────────────────────────────┬──────────────────────────────────────────────┐
│   Key    │                Missing File                  │                Reference File                │
├──────────┼──────────────────────────────────────────────┼──────────────────────────────────────────────┤
│ title    │ file:///path/to/en.json:12:1                 │ file:///path/to/tr.json:10:1                 │
│ subtitle │ file:///path/to/en.json:25:1                 │ file:///path/to/fr.json:30:1                 │
└──────────┴──────────────────────────────────────────────┴──────────────────────────────────────────────┘
File: fr.json
All keys are present.

Using compare-json-files as a Git Pre-Commit Hook

To ensure that you don’t commit incomplete translations, you can add compare-json-files as a git pre-commit hook. This will run the tool before each commit and prevent the commit if missing keys are found.

Step 1: Install Husky

Husky is a tool that makes it easy to add git hooks in Node.js projects. Install it as a dev dependency:

npm install husky --save-dev

Step 2: Initialize Husky

Run the following command to initialize Husky in your project:

npx husky install

This will create a .husky/ directory with the necessary files.

Step 3: Add Pre-Commit Hook

Create a pre-commit hook by running:

npx husky add .husky/pre-commit "npx compare-json-files"

This adds a pre-commit hook that will run the compare-json-files tool before every commit. If any missing keys are found, the commit will be blocked.

Step 4: Test the Hook

Try to commit your changes. If there are any translation inconsistencies, the commit will fail, and you’ll see the missing keys in the console.

License

This project is licensed under the MIT License.