Skip to content

feat(cli): provide executable script for the package #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 8, 2025

Conversation

asopian
Copy link
Contributor

@asopian asopian commented Apr 29, 2025

Make the package to be executable from the CLI.
And allows transforming input from stdin or from a file (option -f).
Also allows jmespath expression to be given as param or from file (option -e).

Example:

  1. Using stdin
echo '{"hello": "world"}' | jp "hello"    # Output: "world"

Assuming jp_input.json :

{
  "hello": "world"
}

And jp_query :

 hello
  1. Using CLI parameters
jp -f jp_input.json -e jp_query   # Output: "world"

Later on this can be scripted or used with nodemon for continuous exploratory working with JSON transformation.
For example:

npx nodemon --watch jp_query.query --exec "jp -f jp_input.json -e jp_query.query > jp_output.json"

@springcomp
Copy link
Contributor

I like the idea.

@asopian would that benefit from being written in TypeScript ?

@asopian
Copy link
Contributor Author

asopian commented May 7, 2025

@springcomp Thanks. Not sure if Typescript would be beneficial, but don't mind either way. This PR was just following the established pattern and enhance the existing script. Feel free to let me know if prefer Typescript and I can update the script.

@springcomp
Copy link
Contributor

@asopian well I like the type safety that TypeScript brings, even though it might not provide a lot of value for a single script.

Speaking of which, I have tried to propose a simple conversion to TypeScript as a pull request to your contribution and I uncovered a bug 🙈.

  function printResult(inputJSON: string, expression: string, compact: boolean = false) {
    try {
      var parsedInput = JSON.parse(inputJSON);
-     console.log(JSON.stringify(jmespath.search(parsedInput, expression, null, compact), null, compact ? 0 : 2));
+     console.log(JSON.stringify(jmespath.search(parsedInput, expression, null), null, compact ? 0 : 2));
    } catch (e) {
      console.error('Error parsing JSON input:', e.message);
      process.exit(1);
    }
  }

Please, let me know what you think.

@asopian
Copy link
Contributor Author

asopian commented May 8, 2025

@springcomp Indeed it is good to have type-safety. Your proposed change/fix is looking good to me and have merged it. The build config and package.json will need updating, due to the use of Typescript on scripts directory. I will update it through a new commit later this week.

@asopian
Copy link
Contributor Author

asopian commented May 8, 2025

@springcomp Updated the package.json with the new binary path. Also improved the script error message, so that can differentiate if the error is coming from the JSON input or from the JMESPath expression. For your review.

@springcomp
Copy link
Contributor

@asopian That looks all right to me.

I have submitted more contributions as another pull request to enforce lint rules and format according to the project configuration. I also have moved the jp.js to the bin/ folder which, I think makes a clear distinction between automation scripts and target files.

Thanks for your contributions.

@asopian
Copy link
Contributor Author

asopian commented May 8, 2025

@springcomp Thank you for more contribution. Also thanks for pointing out about formatting/linting. Learn something new about Biome :)

@springcomp
Copy link
Contributor

@cawalch that looks good to me.

@cawalch cawalch merged commit da2d6c6 into jmespath-community:main May 8, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants