feat!: take CLI template as positional argument

Make template a required positional arg (drop --template) for v11 per #586; stdin template remains unsupported without a special error.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Yang Jun
2026-07-24 02:08:59 +08:00
co-authored by Cursor
parent 44712f64fc
commit 456d7dc6a4
3 changed files with 17 additions and 15 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ npm install liquidjs
**CLI** **CLI**
```bash ```bash
npx liquidjs --template 'Hello, {{ name }}!' --context '{"name": "Liquid"}' npx liquidjs 'Hello, {{ name }}!' --context '{"name": "Liquid"}'
``` ```
See the [setup guide][setup] for partials, layouts, caching, and other options. See the [setup guide][setup] for partials, layouts, caching, and other options.
+9 -7
View File
@@ -14,7 +14,7 @@ async function render () {
program program
.name('liquidjs') .name('liquidjs')
.description('Render a Liquid template') .description('Render a Liquid template')
.requiredOption('-t, --template <liquid | @path>', 'liquid template to render (inline or @path to a file)') // TODO: Change to argument in 11.0 .argument('<template>', 'liquid template to render (inline or @path to a file)')
.option('-c, --context <json | @path>', 'input context in JSON format (@- to read from stdin)') .option('-c, --context <json | @path>', 'input context in JSON format (@- to read from stdin)')
.option('-o, --output <path>', 'write rendered output to file (omit to write to stdout)') .option('-o, --output <path>', 'write rendered output to file (omit to write to stdout)')
.option('--cache [size]', 'cache previously parsed template structures (default cache size: 1024)') .option('--cache [size]', 'cache previously parsed template structures (default cache size: 1024)')
@@ -45,12 +45,7 @@ async function render () {
.parse() .parse()
const options = program.opts() const options = program.opts()
const template = await resolveTemplate(program.args[0])
if (options.template === '@-') {
throw new Error(`Reading template from stdin is not supported. Pass an inline template or @path.`)
}
const template = await resolveInputOption(options.template)
const context = await resolveContext(options.context) const context = await resolveContext(options.context)
const liquid = new Liquid(options) const liquid = new Liquid(options)
const output = liquid.parseAndRenderSync(template, context) const output = liquid.parseAndRenderSync(template, context)
@@ -70,6 +65,13 @@ async function resolveContext (contextOption) {
return context return context
} }
async function resolveTemplate (templateOption) {
if (templateOption && templateOption.startsWith('@') && templateOption !== '@-') {
return resolveInputOption(templateOption)
}
return templateOption
}
async function resolveInputOption (option) { async function resolveInputOption (option) {
let content = null let content = null
if (option) { if (option) {
+7 -7
View File
@@ -56,33 +56,33 @@ Pre-built UMD bundles are also available:
LiquidJS can also be used to render a template directly from CLI using `npx`: LiquidJS can also be used to render a template directly from CLI using `npx`:
```bash ```bash
npx liquidjs --template '{{"hello" | capitalize}}' npx liquidjs '{{"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) or you can read it from a file by using the `@` character followed by a path, like so:
```bash ```bash
npx liquidjs --template @./some-template.liquid npx liquidjs @./some-template.liquid
``` ```
A context can be passed inline, from a path, or piped through `stdin`. The following three are equivalent: A context can be passed inline, from a path, or piped through `stdin`. The following three are equivalent:
```bash ```bash
npx liquidjs --template 'Hello, {{ name }}!' --context '{"name": "Snake"}' npx liquidjs 'Hello, {{ name }}!' --context '{"name": "Snake"}'
npx liquidjs --template 'Hello, {{ name }}!' --context @./some-context.json npx liquidjs 'Hello, {{ name }}!' --context @./some-context.json
echo '{"name": "Snake"}' | npx liquidjs --template 'Hello, {{ name }}!' --context @- echo '{"name": "Snake"}' | npx liquidjs 'Hello, {{ name }}!' --context @-
``` ```
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): 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 ```bash
npx liquidjs --template '{{"hello" | capitalize}}' --output ./hello.txt npx liquidjs '{{"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: 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 ```bash
npx liquidjs --template @./some-template.liquid --js-truthy npx liquidjs @./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`. 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`.