Skip to content

Commit 9504e4e

Browse files
Brandon Pittmanharttle
Brandon Pittman
authored andcommitted
feat: add ability to pass JSON context to CLI
If you pass a JSON file or JSON string as the first argument to the CLI, you can use variables in your templates that get parsed by the CLI.
1 parent 0b9b50c commit 9504e4e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

bin/liquid.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#!/usr/bin/env node
22

33
const Liquid = require('..').Liquid
4+
var contextArg = process.argv.slice(2)[0]
5+
var context = {}
6+
7+
if (contextArg) {
8+
if (contextArg.endsWith('.json')) {
9+
const fs = require('fs')
10+
context = JSON.parse(fs.readFileSync(contextArg, 'utf8'))
11+
} else {
12+
context = JSON.parse(contextArg)
13+
}
14+
}
415

516
let tpl = ''
617
process.stdin.on('data', chunk => (tpl += chunk))
718
process.stdin.on('end', () => render(tpl))
819

920
async function render (tpl) {
1021
const liquid = new Liquid()
11-
const html = await liquid.parseAndRender(tpl)
22+
const html = await liquid.parseAndRender(tpl, context)
1223
console.log(html)
13-
}
24+
}

0 commit comments

Comments
 (0)