Skip to content

Instantly share code, notes, and snippets.

@redaktor
Last active August 29, 2015 13:57
Show Gist options
  • Save redaktor/9546054 to your computer and use it in GitHub Desktop.
Save redaktor/9546054 to your computer and use it in GitHub Desktop.
dive/dojo : a snippet to itterate recursive all views in a folder, abstracted
/* we use dojo on node.js allover and here as a "promise pattern" ! : */
/* http://github.com/kitsonk/dojo-node-boilerplate */
require(["dojo/Deferred", "dojo/promise/all"], function(def, all){
var rPath, name, eStr, iStr, e, _data;
function read (file){
var d = new def();
var rPath = path.relative(vDir,file);
var name = file;
/* you could name your file here,
e.g.: file is '/home/foo/views/projectA/index.html' and name should be 'projectA/index'
*/
fs.readFile(file, 'utf8', function (err,data) {
if (err) return console.log(err);
_data = String(_data.trim() || '');
d.resolve({id:name, data:_data});
});
return d.promise;
}
var promises = [];
dive(vDir, { directories: false, files: true, recursive: true },
function(err, file) {
/* DIVE CALLBACK 1 */
if (err) throw err;
promises.push( read(file) );
},
function() {
/* DIVE CALLBACK 2 */
console.log('1/2 complete - reading async now');
all(promises).then(function(results){
var views = {};
array.forEach(results, function(r,i){
views[r.id] = r.data;
});
vExt, vDir, rPath, name, eStr, iStr, e, _data = null;
console.log( '2/2 complete !', views );
});
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment