rsm-hcd / AndcultureCode.Cli

and-cli command-line tool to manage the development of software applications
https://andculture.com
Apache License 2.0
14 stars 15 forks source link

Json utility command(s) #163

Open brandongregoryscott opened 3 years ago

brandongregoryscott commented 3 years ago

Throwing this idea out there, but would love some discussion if anyone knows of existing tooling (extensions, CLIs, linter settings, etc.) to do the same.

On a client project, we structure localized strings in JSON files on the backend in the structure of...

[
    {
        "Key": "AmazonSESEmailProvider.GetSender.NoDefaultFrom",
        "Value": "No From address is set."
    },
    {
        "Key": "AmazonSESEmailProvider.GetRecipients.NoDefaultTo",
        "Value": "No To address is set."
    },
    {
        "Key": "ERROR_RESOURCE_NOT_FOUND",
        "Value": "The resource or related resource {0} that was requested was not found."
    },
]

It's nice to keep this file's structure alphabetized by each object's Key key. I couldn't really track down anything that did this - alphabetizing by the value of a key in an arbitrary object (such as an array of objects...). It is very tedious to do by hand and often goes by the way side.

For a weekend hack project I whipped up some code that would read in a json file and alphabetize it, with an optional key to alphabetize by. It's rough around the edges and I didn't write any tests for it, but the idea is there.

The API would look something like this:

and-cli json --alphabetize package.json - Just a regular JSON object, nothing special. Sorts keys alphabetically. and-cli json --alphabetize dotnet/api/Presentation/Web/Cultures/Presentation.Web.es-ES.json --key Key - Sorts the array of Key/Value objects by the value of "Key" in addition to ensuring the keys are alphabetized

Again, this code isn't perfect but can be used as a base - https://github.com/brandongregoryscott/b-cli/blob/main/src/modules/json-alphabetize.ts

I added some additional functionality to, by default, write output to a new file that's timestamped, so the original file is not overwritten unless intended with the --in-place flag.

myty commented 3 years ago

Nifty idea. I like it.