hjson / hjson-js

Hjson for JavaScript
https://hjson.github.io/
MIT License
416 stars 49 forks source link

CLI to write back to hjson #32

Closed MartinMuzatko closed 6 years ago

MartinMuzatko commented 6 years ago

Hello! I'm using hjson in combination with jq to read from hjson on the commandline. See https://jqplay.org/s/pD2ADtE_rn

I would like to be able to write to hjson file via terminal. Right now this is not possible.

Suggested syntax:

hjson -s .test "newstring"

Let me know what you think!

laktak commented 6 years ago

If I understand you correctly you should be able to do this with

hjson -c < input.hjson | jq [...] | hjson > output.hjson

where you replace [...] with the commands to edit your json.

MartinMuzatko commented 6 years ago

Thanks for the reply. Unfortunately, this will not keep my comments and this will only put the selected properties as raw text. What I need, is to change only one property and write it back with the full hjson file while keeping comments.

So

{
    // some comments
    test: "my test"
    numbers: 34938
}

can be turned into

{
    // some comments
    test: "my test is complete"
    numbers: 34938
}

It looks like I have to workaround this with sed :/

laktak commented 6 years ago

Would this help?

node -e 'var Hjson=require("hjson"), fs=require("fs"); var data=Hjson.rt.parse(fs.readFileSync("test.hjson", "utf8")); data.foo="bar"; console.log(Hjson.rt.stringify(data));'

You need to npm i hjson first or require it from npm's global folder.

MartinMuzatko commented 6 years ago

This is sort of what I'm doing as of now. Having this built into hjson would be nice. I will share my implementation next year. Thank you so far :)