diff --git a/bin/liquid.js b/bin/liquid.js index ceb0fdfa0..703f4a99f 100755 --- a/bin/liquid.js +++ b/bin/liquid.js @@ -14,8 +14,8 @@ async function render () { program .name('liquidjs') .description('Render a Liquid template') - .requiredOption('-t, --template ', 'liquid template to render (inline or @path to a file)') - .option('-c, --context ', 'input context in JSON format (@- to read from stdin)') + .requiredOption('-t, --template ', 'liquid template to render (inline, @path, or @- for stdin)') + .option('-c, --context ', 'input context in JSON format (inline, @path, or @- for stdin)') .option('-o, --output ', 'write rendered output to file (omit to write to stdout)') .option('--cache [size]', 'cache previously parsed template structures (default cache size: 1024)') .option('--extname ', 'use a default filename extension when resolving partials and layouts') @@ -45,7 +45,12 @@ async function render () { .parse() const options = program.opts() - const template = await resolveTemplate(options.template) + + if (Object.values(options).filter((value) => value === '@-').length > 1) { + throw new Error(`The stdin input specifier '@-' must only be used once.`) + } + + const template = await resolveInputOption(options.template) const context = await resolveContext(options.context) const liquid = new Liquid(options) const output = liquid.parseAndRenderSync(template, context) @@ -65,13 +70,6 @@ async function resolveContext (contextOption) { return context } -async function resolveTemplate (templateOption) { - if (templateOption && templateOption.startsWith('@') && templateOption !== '@-') { - return resolveInputOption(templateOption) - } - return templateOption -} - async function resolveInputOption (option) { let content = null if (option) { diff --git a/docs/source/tutorials/setup.md b/docs/source/tutorials/setup.md index 8db5f8f40..3bafc6522 100644 --- a/docs/source/tutorials/setup.md +++ b/docs/source/tutorials/setup.md @@ -59,13 +59,14 @@ LiquidJS can also be used to render a template directly from CLI using `npx`: npx liquidjs --template '{{"hello" | capitalize}}' ``` -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: +You can either pass the template inline (as shown above), read it from a file with `@` followed by a path, or from `stdin` with `@-`: ```bash npx liquidjs --template @./some-template.liquid +echo '{{"hello" | capitalize}}' | npx liquidjs --template @- ``` -A context can be passed inline, from a path, or piped through `stdin`. The following three are equivalent: +A context can be passed the same ways (inline, from a path, or via `@-` for `stdin`). The following three are equivalent: ```bash npx liquidjs --template 'Hello, {{ name }}!' --context '{"name": "Snake"}' @@ -73,6 +74,8 @@ 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