UNPKG

2.37 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var path = require('path');
4var fs = require('fs');
5var browserify = require('browserify');
6var vashify = require('vashify');
7var objectAssign = require('object-assign');
8
9require('../lib/helpers/layout');
10
11var args = process.argv.slice(2);
12var files = args.filter(function (a, i) {
13 return a[0] != '-' && (args[i-1] ? args[i-1][0] !== '-' : true)
14});
15var opts = args.reduce(function (parsed, a, i) {
16 if (a[0] !== '-') return parsed; // skip
17 if (a[0] === '-' && a[1] === '-') a = a.slice(2);
18 if (args[i+1] && args[i+1][0] !== '-') parsed[a] = args[i+1];
19 else parsed[a] = true;
20 return parsed;
21}, {});
22
23// These are passed directly into vash too, aka vash.config.* at compile time.
24var vashifyOptions = objectAssign({}, {
25
26 // use the full vash experience to allow the layout helpers to function
27 runtime: path.join(__dirname, '../'),
28
29 // require yourself
30 compiler: require('../'),
31
32 // Insert the hook normally handled by vash.loadFile that causes the layout
33 // helpers to "seal" themselves and finish rendering.
34 onRenderEnd: function (err, ctx) {
35 if (ctx.finishLayout) ctx.finishLayout();
36 }
37}, opts, opts.config ? JSON.parse(opts.config) : {});
38
39if (files.length > 1) {
40 throw new Error('Can only accept a single file, instead got '
41 + files.length + ' ' + files.join(' '));
42}
43
44var bopts = {
45 basedir: process.cwd(),
46 builtins: false,
47 insertGlobalVars: ['__filename', '__dirname', 'process']
48};
49
50var unique = opts.helper ? 'vash-helper' : 'vash-render-' + Date.now();
51
52var b = browserify(bopts)
53.transform(vashify, vashifyOptions)
54.require(files.length === 0 ? process.stdin : files[0], { expose: unique })
55.bundle(function (err, buf) {
56 if (err) throw err;
57
58 if (opts.model) opts.model = JSON.parse(opts.model);
59
60 // Assume helpers are precompiled and will attach to `vash.helpers` within
61 // this execution environment.
62 if (opts.helpers) {
63 console.error('reading in helpers', opts.helpers)
64 console.error(Object.keys(vashifyOptions.compiler.helpers))
65 eval(fs.readFileSync(opts.helpers, 'utf8'));
66
67 //require(opts.helpers);
68 console.error(Object.keys(vashifyOptions.compiler.helpers))
69 require('vash-helper');
70 }
71
72 if (opts.helper) {
73 process.stdout.write(buf.toString());
74 } else {
75 eval(buf.toString());
76 var rendered = require(unique)(opts.model, vashifyOptions);
77 }
78});
\No newline at end of file