Last active
August 29, 2015 13:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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