Skip to content
Browse files

Fixes to scaffolder readFile usage

  • Loading branch information...
1 parent 8d81595 commit 35bdf11e28bdab652a5fd39dc8f3860930c8c12e @DamonOehlman committed
Showing with 60 additions and 49 deletions.
  1. +41 −31 lib/commands/build.js
  2. +6 −8 lib/views/console.js
  3. +11 −10 lib/views/editor.js
  4. +2 −0 package.json
View
72 lib/commands/build.js
@@ -1,10 +1,11 @@
var async = require('async'),
+ debug = require('debug')('demogen-build'),
fs = require('fs'),
path = require('path'),
handlebars = require('handlebars'),
_ = require('underscore'),
reIgnore = /^_/,
- reDemoFile = /(^demo\.|\.js$)/i,
+ reDemoFile = /\.js$/i,
reScriptHelper = /^(init|cleanup)\.js$/i,
reKeyedFile = /^(.*)\.(js|css)$/i,
readers = {},
@@ -92,6 +93,7 @@ function compile(scaffolder, demo, callback) {
// reset the includes
demo.includes = [];
+ debug('compiling demo, output file: ' + outputFile);
// if we have demo.css then this is a css demo, not js
if (demo.css.demo) {
@@ -118,19 +120,18 @@ function compile(scaffolder, demo, callback) {
// initialise the keymappings
demo.keys = demo.keys || [];
-
+
// prepare the views
- prepareViews(scaffolder, demo, function() {
+ prepareViews(scaffolder, demo, function(err) {
+ if (err) return callback(err);
+
// if we have a layout for the demo, then build
if (demo.key && demo.layout) {
template = handlebars.compile(demo.layout);
// write the file
fs.writeFile(outputFile, template(demo), function(err) {
- if (err) {
- scaffolder.out('!{red}Unable to generate!{} file !{underline}{0}', outputFile);
- }
- else {
+ if (! err) {
scaffolder.out('Generated demo file: !{underline}{0}', outputFile);
} // if..else
@@ -180,6 +181,9 @@ function findSnippets(scaffolder, targetPath, config, callback) {
}),
key;
+ debug('config path: ' + configFile);
+ debug('demo path: ' + demoPath);
+
function checkPath(file, pathCallback) {
var testPath = path.join(targetPath, file);
@@ -241,15 +245,14 @@ function findSnippets(scaffolder, targetPath, config, callback) {
});
}
catch (e) {
- scaffolder.out('!{red}Error parsing deck config @ {0}', configFile);
+ return callback(new Error('Error parsing config file: ' + configFile));
} // try..catch
} // if
-
+
// read the contents of the directory
fs.readdir(targetPath, function(err, files) {
if (err) {
- scaffolder.out('Unable to read directory: {0}', targetPath);
- callback();
+ return callback(new Error('Unable to read directory: ' + targetPath));
}
else {
async.forEach(files, checkPath, function(err) {
@@ -264,13 +267,16 @@ function findSnippets(scaffolder, targetPath, config, callback) {
async.forEach(
subdirs,
function(testPath, dirCallback) {
- findSnippets(scaffolder, testPath, childConfig, function(childSnippets) {
- snippets = snippets.concat(childSnippets);
- dirCallback(null);
+ findSnippets(scaffolder, testPath, childConfig, function(err, childSnippets) {
+ if (! err) {
+ snippets = snippets.concat(childSnippets);
+ }
+
+ dirCallback(err);
});
},
function(err) {
- callback(snippets);
+ callback(null, snippets);
}
);
});
@@ -355,37 +361,39 @@ function orderScripts(scripts) {
function prepareViews(scaffolder, demo, callback) {
var views = (demo.views || 'editor').split(/[\s\,]/),
- preparedViews = {},
- viewFn;
+ preparedViews = {};
+
+ debug('preparing views for demo: ' + (demo.key || ''), views);
// iterate through the views and add them to the views
async.forEach(
views,
function(view, viewCallback) {
+ var viewFn;
+
try {
viewFn = require('../views/' + view);
}
catch (e) {
- viewFn = null;
- scaffolder.out('Could not load view renderer: {0}', view);
-
- console.log(e);
} // try..catch
if (viewFn) {
- viewFn.call(null, scaffolder, demo, function(contents) {
- preparedViews[view] = contents;
- viewCallback();
+ viewFn.call(null, scaffolder, demo, function(err, contents) {
+ if (! err) {
+ preparedViews[view] = contents;
+ }
+
+ viewCallback(err);
});
}
else {
- viewCallback();
+ viewCallback(new Error('Could not load view renderer: ' + view));
} // if..else
},
function(err) {
demo.views = preparedViews;
if (callback) {
- callback();
+ callback(err);
} // if
}
);
@@ -402,8 +410,10 @@ exports.run = function(opts, callback) {
};
this.out('Looking for demos in: !{underline}{0}', baseConfig.basePath);
- findSnippets(scaffolder, path.join(baseConfig.basePath, '_sources'), baseConfig, function(snippets) {
- if (snippets && snippets.length) {
+ findSnippets(scaffolder, path.join(baseConfig.basePath, '_sources'), baseConfig, function(err, snippets) {
+ if (! err) {
+ debug('found ' + snippets.length + ' snippets');
+
// compile each of the decks
async.forEach(
snippets,
@@ -413,8 +423,8 @@ exports.run = function(opts, callback) {
callback || function() {}
);
}
- else if (callback) {
- callback();
- } // if..else
+ else {
+ callback(err);
+ }
});
};
View
14 lib/views/console.js
@@ -8,13 +8,11 @@ module.exports = function(scaffolder, demo, callback) {
// demo.includes.unshift('_demogen/js/fakeconsole.js');
// read the fakeconsole files
- scaffolder.readFile('views/console.css', function(err, data) {
- if (! err) {
- demo.css.push(data);
- } // if
-
- scaffolder.readFile('views/console.html', function(err, data) {
- callback(data);
- });
+ scaffolder.readFile('views/console.css', 'utf8', function(err, data) {
+ if (err) return callback(err);
+
+ // add teh css
+ demo.css.push(data);
+ scaffolder.readFile('views/console.html', 'utf8', callback);
});
};
View
21 lib/views/editor.js
@@ -3,21 +3,22 @@ var fs = require('fs'),
handlebars = require('handlebars');
module.exports = function(scaffolder, demo, callback) {
- scaffolder.readFile('views/editor.css', function(err, data) {
- if (! err) {
- demo.css.push(data);
- } // if
+ var template;
+
+ scaffolder.readFile('views/editor.css', 'utf8', function(err, data) {
+ if (err) return callback(err);
+
+ // add the data to the demo css
+ demo.css.push(data);
- scaffolder.readFile('views/editor.html', function(err, data) {
- if (err) {
- data = '<div id="editor">{{{ code }}}</div>';
- } // if
+ scaffolder.readFile('views/editor.html', 'utf8', function(err, data) {
+ if (err) return callback(err);
// compile the handlebars template
- var template = handlebars.compile(data);
+ template = handlebars.compile(data);
// fire the callback, returning the view contents
- callback(template(demo));
+ callback(null, template(demo));
});
});
};
View
2 package.json
@@ -16,7 +16,9 @@
},
"dependencies": {
"async": "0.1.x",
+ "debug": "*",
"handlebars": ">= 1.0.0",
+ "markdown": "0.3.x",
"underscore": "1.3.x",
"scaffolder": "0.5.x"
},

0 comments on commit 35bdf11

Please sign in to comment.
Something went wrong with that request. Please try again.