Skip to content

Commit

Permalink
- added bower utils
Browse files Browse the repository at this point in the history
- added template.render() and various other template helpers
  • Loading branch information
der-On committed Aug 8, 2014
1 parent 7ac611a commit 8ff11e8
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 8 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var path = require('path')
module.exports.geddyPath = geddyPath;
module.exports.template = require('./lib/template');
module.exports.jake = require('./lib/jake');
module.exports.bower = require('./lib/bower');

/**
* Checks if a command line flag is set
Expand Down
27 changes: 27 additions & 0 deletions lib/bower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var exec = require('child_process').exec;

/**
* Installs bower components
* @param deps {Array}
* @param cb {Function(error)} - Called when all components installed
*/
function install(deps, cb)
{
var p = exec('bower install ' + deps.join(' ') + ' --save', cb);
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
}
module.exports.install = install;

/**
* Uninstalls bower components
* @param deps {Array}
* @param cb {Function(error)} - Called when all components uninstalled
*/
function uninstall(deps, cb)
{
var p = exec('bower uninstall ' + deps.join(' ') + ' --save', cb);
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
}
module.exports.uninstall = uninstall;
191 changes: 183 additions & 8 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,32 @@ function write(src, dest, data, transform) {
, supported = ['.ejs', '.jade', '.ms', '.swig', '.hbs'];

if (!extToEngine[ext]) {
fail('Unsuported template engine. Try one of these instead: ' + supported.join(', '));
throw new Error('Unsuported template engine. Try one of these instead: ' + supported.join(', '));
return;
}

// Write file
fs.writeFileSync(dest, render(src, getEngineFromExt(ext), data, transform), 'utf8');

console.log('[Added] ' + dest);
}
module.exports.write = write;

/**
* Renders a template file
*
* @param src {String}
* @param engine {String} - Template engine
* @param data {Object}
* @param transform {Function(content, data)} - (optional) if set this function can transform the rendered template before it gets written to the file
* @returns {String}
*/
function render(src, engine, data, transform)
{
var supported = Object.keys(engineToExt);

if (!engineToExt[engine]) {
throw new Error('Unsuported template engine. Try one of these instead: ' + supported.join(' '));
return;
}

Expand All @@ -50,28 +75,178 @@ function write(src, dest, data, transform) {
, templContent;

// render with the correct adapter
adapter = new Adapter({engine: getEngineFromExt(ext), template: text});
adapter = new Adapter({engine: engine, template: text});
templContent = adapter.render(data);

if (typeof transform === 'function') {
templContent = transform(templContent, data);
}

// Write file
fs.writeFileSync(dest, templContent, 'utf8');

console.log('[Added] ' + dest);
return templContent;
}
module.exports.write = write;
module.exports.render = render;

/**
* Returns template engine for a file extension
* @param ext {String}
* @returns {String}
*/
function getEngineFromExt(ext)
{
return extToEngine[ext] || defaultEngine;
}
module.exports.getEngineFromExt = getEngineFromExt;

/**
* Returns file extension for a template engine
* @param engine {String}
* @returns {String}
*/
function getExtFromEngine(engine)
{
return engineToExt[engine] || defaultExt;
}
module.exports.getExtFromEngine = getExtFromEngine;
module.exports.getExtFromEngine = getExtFromEngine;

/**
* Returns an engine specific script include
* @param src {String}
* @param engine {String}
* @returns {string}
*/
function script(src, engine) {
switch(engine) {
case 'ejs':
return "<@- scriptLink('" + src + "', {type:'text/javascript'}) @>";
break;
case 'jade':
return "!= scriptLink('" + src + "', {type:'text/javascript'})";
break;
case 'swig':
return "{{ scriptLink('" + src + "', {type:'text/javascript'}) }}";
break;
case 'handlebars':
case 'mustache':
default:
return '<script type="text/javascript" src="' + src + '"></script>';
}
}
module.exports.script = script;

/**
* Returns an engine specific stylesheet include
* @param src {String}
* @param engine {String}
* @returns {string}
*/
function stylesheet(src, engine) {
switch(engine) {
case 'ejs':
return "<@- styleLink('" + src + "', {rel:'stylesheet'}) @>";
break;
case 'jade':
return "!= styleLink('" + src + "', {rel:'stylesheet'})";
break;
case 'swig':
return "{{ styleLink('" + src + "', {rel:'stylesheet'}) }}";
break;
case 'handlebars':
case 'mustache':
default:
return '<link rel="stylesheet" href="' + src + '">';
}
}
module.exports.stylesheet = stylesheet;

/**
* Returns an engine specific partial include
* @param src {String}
* @param data {String} - (optional)
* @param engine {String}
* @returns {string}
*/
function partial(/*src, [data], engine*/) {
var args = Array.prototype.slice.call(arguments);
var src = args.shift();
var engine = args.pop();
var data = args.pop() || null;
if (data) {
data = ', ' + data;
}
else data = '';

switch(engine) {
case 'ejs':
return "<@- partial('" + src + "'" + data + ") @>";
break;
case 'jade':
return "!= partial('" + src + "'" + data + ")";
break;
case 'swig':
return "{{ partial('" + src + "'" + data + ") }}";
break;
case 'handlebars':
case 'mustache':
return "{{{partial('" + src + "'" + data + ")}}}";
break;
default:
return "<!-- partial('" + src + "'" + data + ") -->";
}
}
module.exports.partial = partial;

/**
* Returns engine specific unescaped content
* @param content {String}
* @param engine {String}
* @returns {String}
*/
function unescaped(content, engine)
{
switch(engine) {
case 'ejs':
return "<@- " + content + " @>";
break;
case 'jade':
return "!= " + content;
break;
case 'swig':
return "{{ " + content + " }}";
break;
case 'handlebars':
case 'mustache':
return "{{{" + content + "}}}";
break;
default:
return '<!-- ' + content + ' -->';
}
}
module.exports.unescaped = unescaped;

/**
* Returns engine specific escaped content
* @param content {String}
* @param engine {String}
* @returns {String}
*/
function escaped(content, engine)
{
switch(engine) {
case 'ejs':
return "<@= " + content + " @>";
break;
case 'jade':
return "!= " + content;
break;
case 'swig':
return "{{ " + content + " }}";
break;
case 'handlebars':
case 'mustache':
return "{{{" + content + "}}}";
break;
default:
return '<!-- ' + content + ' -->';
}
}
module.exports.escaped = escaped;

0 comments on commit 8ff11e8

Please sign in to comment.