From 9504e4e45ba603a141a95bd6488af7bceac702bb Mon Sep 17 00:00:00 2001 From: Brandon Pittman Date: Sun, 10 Nov 2019 16:37:05 +0900 Subject: [PATCH] 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. --- bin/liquid.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bin/liquid.js b/bin/liquid.js index 36f251c36a..e0011360ea 100755 --- a/bin/liquid.js +++ b/bin/liquid.js @@ -1,6 +1,17 @@ #!/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)) @@ -8,6 +19,6 @@ 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) -} \ No newline at end of file +}