UNPKG

4.06 kBJavaScriptView Raw
1#!/usr/bin/env node
2/*jslint es5: true */
3
4var swig = require('../index'),
5 optimist = require('optimist'),
6 fs = require('fs'),
7 path = require('path'),
8 filters = require('../lib/filters'),
9 utils = require('../lib/utils'),
10 uglify = require('uglify-js');
11
12var command,
13 argv = optimist
14 .usage('\n Usage:\n' +
15 ' $0 compile [files] [options]\n' +
16 ' $0 run [files] [options]\n' +
17 ' $0 render [files] [options]\n'
18 )
19 .describe({
20 v: 'Show the Swig version number.',
21 o: 'Output location.',
22 h: 'Show this help screen.',
23 j: 'Variable context as a JSON file.',
24 c: 'Variable context as a CommonJS-style file. Used only if option `j` is not provided.',
25 m: 'Minify compiled functions with uglify-js',
26 'filters': 'Custom filters as a CommonJS-style file',
27 'tags': 'Custom tags as a CommonJS-style file',
28 'options': 'Customize Swig\'s Options from a CommonJS-style file',
29 'wrap-start': 'Template wrapper beginning for "compile".',
30 'wrap-end': 'Template wrapper end for "compile".',
31 'method-name': 'Method name to set template to and run from.'
32 })
33 .alias('v', 'version')
34 .alias('o', 'output')
35 .default('o', 'stdout')
36 .alias('h', 'help')
37 .alias('j', 'json')
38 .alias('c', 'context')
39 .alias('m', 'minify')
40 .default('wrap-start', 'var tpl = ')
41 .default('wrap-end', ';')
42 .default('method-name', 'tpl')
43 .check(function (argv) {
44 if (argv.v) {
45 return;
46 }
47
48 if (!argv._.length) {
49 throw new Error('');
50 }
51
52 command = argv._.shift();
53 if (command !== 'compile' && command !== 'render' && command !== 'run') {
54 throw new Error('Unrecognized command "' + command + '". Use -h for help.');
55 }
56
57 if (argv['method-name'] !== 'tpl' && argv['wrap-start'] !== 'var tpl =') {
58 throw new Error('Cannot use arguments "--method-name" and "--wrap-start" together.');
59 }
60
61 if (argv['method-name'] !== 'tpl') {
62 argv['wrap-start'] = 'var ' + argv['method-name'] + ' = ';
63 }
64 })
65 .argv,
66 ctx = {},
67 out = function (file, str) {
68 console.log(str);
69 },
70 efn = function () {},
71 anonymous,
72 files,
73 fn;
74
75// What version?
76if (argv.v) {
77 console.log(require('../package').version);
78 process.exit(0);
79}
80
81// Pull in any context data provided
82if (argv.j) {
83 ctx = JSON.parse(fs.readFileSync(argv.j, 'utf8'));
84} else if (argv.c) {
85 ctx = require(argv.c);
86}
87
88if (argv.o !== 'stdout') {
89 argv.o += '/';
90 argv.o = path.normalize(argv.o);
91
92 try {
93 fs.mkdirSync(argv.o);
94 } catch (e) {
95 if (e.errno !== 47) {
96 throw e;
97 }
98 }
99
100 out = function (file, str) {
101 file = path.basename(file);
102 fs.writeFileSync(argv.o + file, str, { flags: 'w' });
103 console.log('Wrote', argv.o + file);
104 };
105}
106
107// Set any custom filters
108if (argv.filters) {
109 utils.each(require(path.resolve(argv.filters)), function (filter, name) {
110 swig.setFilter(name, filter);
111 });
112}
113
114// Set any custom tags
115if (argv.tags) {
116 utils.each(require(path.resolve(argv.tags)), function (tag, name) {
117 swig.setTag(name, tag.parse, tag.compile, tag.ends, tag.block);
118 });
119}
120
121// Specify swig default options
122if (argv.options) {
123 swig.setDefaults(require(argv.options));
124}
125
126switch (command) {
127case 'compile':
128 fn = function (file, str) {
129 var r = swig.precompile(str, { filename: file, locals: ctx }).tpl.toString().replace('anonymous', '');
130
131 r = argv['wrap-start'] + r + argv['wrap-end'];
132
133 if (argv.m) {
134 r = uglify.minify(r, { fromString: true }).code;
135 }
136
137 out(file, r);
138 };
139 break;
140
141case 'run':
142 fn = function (file, str) {
143 (function () {
144 eval(str);
145 var __tpl = eval(argv['method-name']);
146 out(file, __tpl(swig, ctx, filters, utils, efn));
147 }());
148 };
149 break;
150
151case 'render':
152 fn = function (file, str) {
153 out(file, swig.render(str, { filename: file, locals: ctx }));
154 };
155 break;
156}
157
158argv._.forEach(function (file) {
159 var str = fs.readFileSync(file, 'utf8');
160 fn(file, str);
161});