1.1.0 • Published 10 months ago

compare-json-files v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

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.

1.1.0

10 months ago

1.0.0

10 months ago