Permalink
Browse files

Add callback

  • Loading branch information...
1 parent 036f4ba commit 44d12eb210f53a0e53749e4660320607a45a2fba @pvorb committed Feb 4, 2012
Showing with 20 additions and 16 deletions.
  1. +20 −16 bake.js
View
@@ -5,7 +5,7 @@ var dive = require('dive');
var ejs = require('ejs');
// Main function
-var bake = function(conf, hooks) {
+var bake = function(conf, hooks, cb) {
// File counter
var todo = 0;
@@ -14,7 +14,7 @@ var bake = function(conf, hooks) {
if (typeof conf == 'string')
conf = JSON.parse(conf);
if (typeof conf != 'object')
- throw new Error('parameter conf must be a valid configuration object');
+ return cb(new Error('parameter conf must be a valid configuration object'));
// Ensure `hooks` is an object
if (typeof hooks != 'object')
@@ -40,7 +40,8 @@ var bake = function(conf, hooks) {
// Dive into the public directory
dive(inputDir, function(err, master) {
// Throw errors
- if (err) throw err;
+ if (err)
+ return cb(err);
// Matching variable
var match;
@@ -57,7 +58,8 @@ var bake = function(conf, hooks) {
// Read the master-file's contents
fs.readFile(master, 'utf8', function(err, data) {
// Throw errors
- if (err) throw err;
+ if (err)
+ return cb(err);
// Get the properties
// `prop` is the file specific property object
@@ -67,34 +69,35 @@ var bake = function(conf, hooks) {
prop.has = prop.hasOwnProperty;
// Amend `prop` by properties in `conf.properties` if defined
- if (conf.properties != undefined)
+ if (conf.properties)
for (var key in conf.properties) {
- if (prop[key] == undefined)
+ if (typeof prop[key] == 'undefined')
prop[key] = conf.properties[key];
}
// Assert that `prop.template` is set
- if (prop.template == undefined)
+ if (typeof prop.template == 'undefined')
prop.template = 'default.tpl';
// `__propBefore` hook
- if (hooks.__propBefore != undefined)
+ if (hooks.__propBefore)
prop = hooks.__propBefore(master, prop);
// Various property hooks
for (var key in prop)
- if (hooks[key] != undefined)
+ if (hooks[key])
prop[key] = hooks[key](master, prop);
// `__propAfter` hook
- if (hooks.__propAfter != undefined)
+ if (hooks.__propAfter)
prop = hooks.__propAfter(master, prop);
// Read the template file
fs.readFile(path.resolve(tplDir, prop.template), 'utf8',
function(err, result) {
// Throw errors
- if (err) throw err;
+ if (err)
+ return cb(err);
// (Pre-)Insert the content (so ejs-tags in
// `prop.__content` are parsed, too.
@@ -106,7 +109,7 @@ var bake = function(conf, hooks) {
'.' + fileExt[masterExt]);
// New file's path
- if (prop._id == undefined)
+ if (typeof prop._id == 'undefined')
prop._id = resName.replace(inputDir, '');
// Remove first slash
@@ -122,10 +125,11 @@ var bake = function(conf, hooks) {
// Write contents
fs.writeFile(resName, result, function(err) {
// Throw errors
- if (err) throw err;
+ if (err)
+ return cb(err);
// `__written` hook
- if (hooks.__writeAfter != undefined)
+ if (hooks.__writeAfter)
hooks.__writeAfter(master, prop);
// Log status on success
@@ -134,11 +138,11 @@ var bake = function(conf, hooks) {
// When file counter is zero
if (!--todo) {
// `__complete` hook
- if (hooks.__complete != undefined)
+ if (hooks.__complete)
hooks.__complete(master, prop);
// State final message
- console.log('Everything has been successfully baked!');
+ return cb(null);
}
});
});

0 comments on commit 44d12eb

Please sign in to comment.