#!/usr/bin/env node var path = require('path'); var fs = require('fs'); var browserify = require('browserify'); var vashify = require('vashify'); var objectAssign = require('object-assign'); require('../lib/helpers/layout'); var args = process.argv.slice(2); var files = args.filter(function (a, i) { return a[0] != '-' && (args[i-1] ? args[i-1][0] !== '-' : true) }); var opts = args.reduce(function (parsed, a, i) { if (a[0] !== '-') return parsed; // skip if (a[0] === '-' && a[1] === '-') a = a.slice(2); if (args[i+1] && args[i+1][0] !== '-') parsed[a] = args[i+1]; else parsed[a] = true; return parsed; }, {}); // These are passed directly into vash too, aka vash.config.* at compile time. var vashifyOptions = objectAssign({}, { // use the full vash experience to allow the layout helpers to function runtime: path.join(__dirname, '../'), // require yourself compiler: require('../'), // Insert the hook normally handled by vash.loadFile that causes the layout // helpers to "seal" themselves and finish rendering. onRenderEnd: function (err, ctx) { if (ctx.finishLayout) ctx.finishLayout(); } }, opts, opts.config ? JSON.parse(opts.config) : {}); if (files.length > 1) { throw new Error('Can only accept a single file, instead got ' + files.length + ' ' + files.join(' ')); } var bopts = { basedir: process.cwd(), builtins: false, insertGlobalVars: ['__filename', '__dirname', 'process'] }; var unique = opts.helper ? 'vash-helper' : 'vash-render-' + Date.now(); var b = browserify(bopts) .transform(vashify, vashifyOptions) .require(files.length === 0 ? process.stdin : files[0], { expose: unique }) .bundle(function (err, buf) { if (err) throw err; if (opts.model) opts.model = JSON.parse(opts.model); // Assume helpers are precompiled and will attach to `vash.helpers` within // this execution environment. if (opts.helpers) { console.error('reading in helpers', opts.helpers) console.error(Object.keys(vashifyOptions.compiler.helpers)) eval(fs.readFileSync(opts.helpers, 'utf8')); //require(opts.helpers); console.error(Object.keys(vashifyOptions.compiler.helpers)) require('vash-helper'); } if (opts.helper) { process.stdout.write(buf.toString()); } else { eval(buf.toString()); var rendered = require(unique)(opts.model, vashifyOptions); } });