feat: Adds support for options to CLI and improves usability (#586)

* Improved UX of CLI

* Updated docs and setup tutorial

* Corrected version number in TODO comments

* Downgraded lock file version

* Restored previous lock file

* Removed --keep-output-type option

* Switch to using async IO to avoid problems stdin in some scenarios

https://stackoverflow.com/questions/40362369/stdin-read-fails-on-some-input

https://stackoverflow.com/questions/40362369/stdin-read-fails-on-some-input

* Addressed feedback from code review

* Changed to curl-like systax for specifing inputs as literals, paths or stdin

---------

Co-authored-by: Daniel Rosenberg <[email protected]>
This commit is contained in:
Daniel Rosenberg
2023-02-14 22:03:00 +08:00
committed by GitHub
co-authored by Daniel Rosenberg
parent a6618538d1
commit 24c8a1e372
5 changed files with 179 additions and 26 deletions
+35 -4
View File
@@ -53,20 +53,51 @@ Pre-built UMD bundles are also available:
## LiquidJS in CLI
LiquidJS is also available from CLI:
LiquidJS can also be used to render a template directly from CLI using `npx`:
```bash
echo '{{"hello" | capitalize}}' | npx liquidjs
npx liquidjs --template '{{"hello" | capitalize}}'
```
If you pass a path to a JSON file or a JSON string as the first argument, it will be used as the context for your template.
You can either pass the template inline (as shown above) or you can read it from a file by using the `@` character followed by a path, like so:
```bash
echo 'Hello, {{ name }}.' | npx liquidjs '{"name": "Snake"}'
npx liquidjs --template @./some-template.liquid
```
You can also use the `@-` syntax to read the template from `stdin`:
```bash
echo '{{"hello" | capitalize}}' | npx liquidjs --template @-
```
A context can be passed in the same ways (i.e. inline, from a path or piped through `stdin`). The following three are equivalent:
```bash
npx liquidjs --template 'Hello, {{ name }}!' --context '{"name": "Snake"}'
npx liquidjs --template 'Hello, {{ name }}!' --context @./some-context.json
echo '{"name": "Snake"}' | npx liquidjs --template 'Hello, {{ name }}!' --context @-
```
Note that you can only use the `stdin` specifier `@-` for a single argument. If you try to use it for both `--template` and `--context` you will get an error.
The rendered output is written to `stdout` by default, but you can also specify an output file (if the file exists, it will be overwritten):
```bash
npx liquidjs --template '{{"hello" | capitalize}}' --output ./hello.txt
```
You can also pass a number of options to customize template rendering behavior. For example, the `--js-truthy` option can be used to enable JavaScript truthiness:
```bash
npx liquidjs --template @./some-template.liquid --js-truthy
```
Most of the [options available through the JavaScript API][options] are also available from the CLI. For help on available options, use `npx liquidjs --help`.
## Miscellaneous
A ReactJS demo is also added by [@stevenanthonyrevo](https://github.com/stevenanthonyrevo), see [liquidjs/demo/reactjs/](https://github.com/harttle/liquidjs/blob/master/demo/reactjs/).
[intro]: ./intro-to-liquid.html
[options]: ./options.md