Skip to content

Commit

Permalink
feat: add ability to pass JSON context to CLI
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Brandon Pittman authored and harttle committed Nov 14, 2019
1 parent 0b9b50c commit 9504e4e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions bin/liquid.js
@@ -1,13 +1,24 @@
#!/usr/bin/env node

const Liquid = require('..').Liquid
var contextArg = process.argv.slice(2)[0]
var context = {}

if (contextArg) {
if (contextArg.endsWith('.json')) {
const fs = require('fs')
context = JSON.parse(fs.readFileSync(contextArg, 'utf8'))
} else {
context = JSON.parse(contextArg)
}
}

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

async function render (tpl) {
const liquid = new Liquid()
const html = await liquid.parseAndRender(tpl)
const html = await liquid.parseAndRender(tpl, context)
console.log(html)
}
}

0 comments on commit 9504e4e

Please sign in to comment.