DEV Community

Cover image for ez-bundle – Javascript tool to research JSON keys in a target directory
Roberto B. Stanziale
Roberto B. Stanziale

Posted on

ez-bundle – Javascript tool to research JSON keys in a target directory

As a web developer, the task of internationalizing a web application can often lead to the accumulation of large and unwieldy language bundles over time. These bundles, containing key-value pairs representing translated strings, can become outdated or unused as the application evolves.

Have you ever wondered: Are all JSON keys still being used by my application?

This tool addresses this issue by providing a simple yet effective means to verify whether a specified directory contains all the keys defined in a JSON file provided as input.

It helps ensure that your language bundles are accurate, up-to-date, and efficiently utilized, minimizing the risk of translation errors and inconsistencies.

How it works

In order to be executed, it needs three things:

  • A JSON file containing your bundle of keys to verify
  • A target directory where you want to look for the keys in the bundle
  • A list of comma-separated file extensions to search for

After the execution, this utility will create an entries-not-found.txt file containing the unused keys as shown below:

widget.caseDetail.details
widget.caseDetail.identifier
widget.caseDetail.priority
widget.caseDetail.progressBar
...
Enter fullscreen mode Exit fullscreen mode

How to execute it

The following command is necessary:

node index.js bundle.json src/ js,ts,html
Enter fullscreen mode Exit fullscreen mode

Where bundle.json and src/ are examples of arguments passed as input to the script, and js,ts,html is an optional argument, otherwise, the default is ts,html.

Performance

For large bundles, it may take a while, for example:

[2023-09-14T10:34:22.663Z] [INFO] Number of keys: 1422
[2023-09-14T10:34:22.666Z] [INFO] Starting process...
[2023-09-14T10:51:52.683Z] [INFO] End process!
Enter fullscreen mode Exit fullscreen mode

In an Angular application using the ngx-translate library, with 1422 keys and more than 950 files to scan, it takes about 17 minutes to complete.

Of course, the script could always be improved from a performance point of view. It should be okay as an initial version because I don't expect it to be executed that often 😉

Constraints

Currently, this tool only works on Linux shells because it uses the grep command internally. For Windows, you can run it on shells like git bash.

Repository

Here you can find the script, download it, try it or use it for yourself:

GitHub logo rstanziale / ez-bundle

Given a json file, it performs a search for its keys in a target directory taken as input.

ez-bundle

As a web developer I found myself in the situation of internationalizing a web application, and clearly after a few years the language bundles can grow out of proportion. It is also possible that the application has been modified, refactored, etc...

Now, are all the keys in the language bundles still being used?

This tool allows you to answer that question.

How it works

To be executed, it needs three things:

  • A JSON file containing your bundle of keys to verify
  • A target directory where you want to look for the keys in the bundle
  • A list of file extensions comma separated to search for

After execution, this utility will create a entries-not-found.txt file containing the unused keys as shown below:

widget.caseDetail.details
widget.caseDetail.identifier
widget.caseDetail.priority
widget.caseDetail.progressBar

How to execute it

It is necessary to run the command:

node index.js bundle.json src/ js,ts,html

Where bundle.json and src/ are examples of…

Top comments (0)