#!/usr/bin/env node var fs = require('fs') ,path = require('path') ,uglify = require('uglify-js') ,cli = require('commander') ,vash = require('../index') function prefix(tplStr, options){ if( options.targetNamespace ){ tplStr = options.targetNamespace + '["' + ( options.propertyName || options.file.replace(/\/|\\/g, options.separator) ) + '"]=' + tplStr; // ensure the simple namespace exists var accum = ''; options.targetNamespace.split('.').reduce(function(all, prop, i){ accum += all + prop + ' = ' + all + prop + ' || {};\n'; all += prop; return all + '.'; }, '') tplStr = accum + tplStr; } return tplStr; } function ugly(tplStr, options){ var ast ,jsp = uglify.parser ,pro = uglify.uglify; if( options.uglify ){ ast = jsp.parse(tplStr); ast = pro.ast_mangle(ast); ast = pro.ast_squeeze(ast); tplStr = pro.gen_code(ast); } return tplStr; } function render(tpl, options){ var model = {}; if( options.render ){ try { model = JSON.parse( options.render ); } catch(e) { try { model = JSON.parse(fs.readFileSync(options.render)); } catch(e) { } } return tpl(model); } return tpl; } function stdin(options){ var buffer = '' process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function(chunk){ buffer += chunk; }) process.stdin.on('end', function(){ var tpl = vash[options.helper ? 'compileHelper' : 'compile'](buffer, options.vopts); if( options.render ){ tpl = render(tpl, options); } else { tpl = prefix( options.autolink ? tpl.toClientString() : tpl.toString(), options); tpl = ugly(tpl, options); } process.stdout.write(tpl); }) } function file(filepath, options){ var contents = fs.readFileSync( filepath, 'utf8' ) ,tpl = vash[options.helper ? 'compileHelper' : 'compile'](contents, options.vopts) ,outpath = path.join( options.out ,path.join( path.dirname(filepath) ,path.basename(filepath) + '.js' ).replace(/\/|\\/g, options.separator) ) if( options.render ){ tpl = render(tpl, options); } else { tpl = prefix( options.autolink ? tpl.toClientString() : tpl.toString(), options); tpl = ugly(tpl, options); } if( options.out ) { fs.writeFile( outpath, tpl.toString(), 'utf8', function(err){ if(err){ process.stderr.write( err ) } } ) } else { process.stdout.write(tpl.toString()); } } // TODO: if passed a directory, recursively traverse cli.on('--help', function(){ console.log(' Examples:'); console.log(''); console.log(' $ echo \'
Hello!
\' | vash --json \'{ "debug": false }\' -t myapp.tplcache -p hello'); console.log(''); console.log(' > myapp = myapp || {};'); console.log(' > myapp.tplcache = myapp.tplcache || {};'); console.log(' > myapp.tplcache["hello"]=vash.link( function anonymous(model,html) {'); console.log(' > html.buffer.push(\'\\n\'); '); console.log(' > return html.buffer.flush(); '); console.log(' > } )'); console.log(''); console.log(' Remember to include vash-runtime.js if precompiling for client-side usage.') console.log(''); }); cli .option('-t, --target-namespace