squirrelchat / smol-toml

A small, fast, and correct TOML (1.0.0) parser and serializer
BSD 3-Clause "New" or "Revised" License
154 stars 9 forks source link

Is smol callable via CLI ? #27

Open nvuillam opened 2 months ago

nvuillam commented 2 months ago

We are looking for a TOML linter to integrate within MegaLinter, and smol is a serious candidate :)

I don't see in the documentation a way to call it via CLI... is there one ?

If not, would it be considered adding one ?

Many thanks for your responses :)

cyyynthia commented 2 months ago

Hi! smol-toml is only a library and does not provide a CLI currently. It is however trivial to use it via CLI using node -e "..." or by using a thin wrapper file.

For use as a linter, I see a few of blockers that may make this library limited, mostly:

I do want to address these at some point, however there are a handful of unanswered questions about the internal design to preserve performance, simplicity of plain JS objects, and flexibility, so I don't really know when I'll get around addressing these (or how) 😅

nvuillam commented 2 months ago

@cyyynthia Thanks for your reply :)

We're looking for a TOML linter, not a formatter for now (it seems there is a prettier-toml anyway), so the linting capability would be enough :)

Would you eventually interested that someone makes a PR to CLI-ify your tool ? ( don't know who and where, just asking ^^ )

It shouldn't be that complicated using cosmiconfig :)

cyyynthia commented 2 months ago

Well, this library is purely a parser/serializer and I can see the added value of a CLI tool, but I am quite happy about the library being zero-dependency which makes it very lightweight to install and use

Checking if a file has errors is really trivial (just a call to parse in a try/catch), so I'm not sure if it's worth having it here. Projects wanting to use it via CLI can most likely afford using a wrapper file, use Just shebang recipes, or node -e 🤔

cyyynthia commented 2 months ago

FYI this is what I mean by using node -e:

$ node -e "try{require('smol-toml').parse('[a.x]\nz=')}catch(e){console.log(e.message);process.exit(0)}"
Invalid TOML document: incomplete key-value declaration: no value specified

1:  [a.x]
2:  z=
      ^

Which could be implemented as a very barebones "CLI" that uses a naive arg parser:

$ pnpx smol-toml [file.toml]
> Reads file.toml, and serializes it back to stdout (or errors if invalid)

$ pnpx smol-toml --to-json [file.toml]
> Reads file.toml, and serializes it as (pretty-printed) JSON to stdout (or errors if invalid)

$ pnpx smol-toml --to-toml [file.json]
> Reads file.json, and serializes it as TOML to stdout (or errors if invalid)