Permalink
Browse files

Add callbacks consequently

  • Loading branch information...
pvorb committed Feb 15, 2012
1 parent 29b81c1 commit fd2ba903fd3063848f7e77d30329fe4b938dfe3d
View
152 bake.js
@@ -3,6 +3,7 @@ var path = require('path');
var props = require('props');
var dive = require('dive');
var ejs = require('ejs');
+var async = require('async');
// Main function
var bake = function(conf, hooks, cb) {
@@ -66,9 +67,6 @@ var bake = function(conf, hooks, cb) {
// `prop` is the file specific property object
var prop = props(data);
- // Define `hasOwnProperty` shorthand
- prop.has = prop.hasOwnProperty;
-
// Amend `prop` by properties in `conf.properties` if defined
if (conf.properties)
for (var key in conf.properties) {
@@ -82,71 +80,107 @@ var bake = function(conf, hooks, cb) {
// `__propBefore` hook
if (hooks.__propBefore)
- prop = hooks.__propBefore(master, prop);
-
- // Various property hooks
- for (var key in prop)
- if (hooks[key])
- prop[key] = hooks[key](master, prop);
-
- // `__propAfter` hook
- if (hooks.__propAfter)
- prop = hooks.__propAfter(master, prop);
+ hooks.__propBefore(master, prop, propBeforeCB);
+ else
+ propBeforeCB(null, prop);
- // Read the template file
- fs.readFile(path.resolve(tplDir, prop.template), 'utf8',
- function(err, result) {
- // Throw errors
+ function propBeforeCB(err, prop) {
if (err)
return cb(err);
- // (Pre-)Insert the content (so ejs-tags in
- // `prop.__content` are parsed, too.
- result = result.replace(/<%=\s+__content\s+%>/g,
- prop.__content);
-
- // Result's filename
- var resName = master.replace(fileExtPattern,
- '.' + fileExt[masterExt]);
-
- // New file's path
- if (typeof prop._id == 'undefined')
- prop._id = resName.replace(inputDir, '');
-
- // Remove first slash
- if (/^\//, prop._id)
- prop._id = prop._id.substring(1);
-
- // Add output dir
- resName = path.resolve(outputDir, prop._id);
-
- // Render ejs-template
- result = ejs.render(result, { locals: prop });
-
- // Write contents
- fs.writeFile(resName, result, function(err) {
- // Throw errors
+ // tasks for async
+ var tasks = {};
+
+ // Various property hooks
+ for (var key in prop) {(function (key) {
+ if (hooks[key])
+ tasks[key] = function (callback) {
+ hooks[key](master, prop, callback);
+ };
+ else
+ tasks[key] = function (callback) {
+ callback(null, prop.key);
+ };
+ })(key);}
+
+ // run tasks in parallel
+ async.parallel(tasks, function (err, prop) {
if (err)
return cb(err);
- // `__written` hook
- if (hooks.__writeAfter)
- hooks.__writeAfter(master, prop);
-
- // Log status on success
- console.log(' ' + resName + ' written.');
-
- // When file counter is zero
- if (!--todo) {
- // `__complete` hook
- if (hooks.__complete)
- hooks.__complete(master, prop);
-
- // State final message
- return cb(null);
+ // `__propAfter` hook
+ if (hooks.__propAfter)
+ prop = hooks.__propAfter(master, prop, propAfterCB);
+ else
+ propAfterCB(null, prop);
+
+ function propAfterCB(err, prop) {
+ if (err)
+ return cb(err);
+
+ // Read the template file
+ fs.readFile(path.resolve(tplDir, prop.template), 'utf8',
+ function(err, result) {
+ // Throw errors
+ if (err)
+ return cb(err);
+
+ // (Pre-)Insert the content (so ejs-tags in
+ // `prop.__content` are parsed, too.
+ result = result.replace(/<%=\s+__content\s+%>/g,
+ prop.__content);
+
+ // Result's filename
+ var resName = master.replace(fileExtPattern,
+ '.' + fileExt[masterExt]);
+
+ // New file's path
+ if (typeof prop._id == 'undefined')
+ prop._id = resName.replace(inputDir, '');
+
+ // Remove first slash
+ if (/^\//, prop._id)
+ prop._id = prop._id.substring(1);
+
+ // Add output dir
+ resName = path.resolve(outputDir, prop._id);
+
+ // Render ejs-template
+ result = ejs.render(result, { locals: prop });
+
+ // Write contents
+ fs.writeFile(resName, result, function(err) {
+ // Throw errors
+ if (err)
+ return cb(err);
+
+ // `__written` hook
+ if (hooks.__writeAfter)
+ hooks.__writeAfter(master, prop, writeAfterCB);
+ else
+ writeAfterCB(null);
+
+ function writeAfterCB(err) {
+ if (err)
+ return cb(err);
+
+ // Log status on success
+ console.log(' ' + resName + ' written.');
+
+ // When file counter is zero
+ if (!--todo) {
+ // `__complete` hook
+ if (hooks.__complete)
+ hooks.__complete(master, prop, cb);
+ else
+ return cb(null);
+ }
+ }
+ });
+ });
}
});
- });
+ }
});
}
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit fd2ba90

Please sign in to comment.