Make async and cleaner

pull/1446/head
John McLear 2013-02-04 00:00:39 +00:00
parent 8b8cf01785
commit da0b331502
1 changed files with 41 additions and 15 deletions

View File

@ -1,25 +1,26 @@
var path = require("path") var path = require("path")
, npm = require("npm") , npm = require("npm")
, fs = require("fs"); , fs = require("fs")
, async = require("async");
exports.expressCreateServer = function (hook_name, args, cb) { exports.expressCreateServer = function (hook_name, args, cb) {
args.app.get('/tests/frontend/specs_list.js', function(req, res){ args.app.get('/tests/frontend/specs_list.js', function(req, res){
fs.readdir('tests/frontend/specs', function(err, files){
fs.readdir('node_modules', function(err, plugins){ // installed plugins async.parallel({
plugins.forEach(function(plugin){ // for each one coreSpecs: function(callback){
if(fs.existsSync("node_modules/"+plugin+"/static/tests/frontend/specs")){ // If the folder exists exports.getCoreTests(callback);
fs.readdir("node_modules/"+plugin+"/static/tests/frontend/specs/", function(err, pluginFiles){ },
files.push("/static/plugins/"+plugin+"/static/tests/frontend/specs/"+pluginFiles); pluginSpecs: function(callback){
}); exports.getPluginTests(callback);
} }
}); },
}); function(err, results){
var files = results.coreSpecs; // push the core specs to a file object
if(err){ return res.send(500); } files = files.concat(results.pluginSpecs); // add the plugin Specs to the core specs
console.debug("Sent browser the following test specs:", files.sort());
res.send("var specs_list = " + JSON.stringify(files.sort()) + ";\n"); res.send("var specs_list = " + JSON.stringify(files.sort()) + ";\n");
}); });
}); });
var url2FilePath = function(url){ var url2FilePath = function(url){
@ -56,3 +57,28 @@ exports.expressCreateServer = function (hook_name, args, cb) {
res.redirect('/tests/frontend/'); res.redirect('/tests/frontend/');
}); });
} }
exports.getPluginTests = function(callback){
var pluginSpecs = [];
var plugins = fs.readdirSync('node_modules');
plugins.forEach(function(plugin){
if(fs.existsSync("node_modules/"+plugin+"/static/tests/frontend/specs")){ // if plugins exists
var specFiles = fs.readdirSync("node_modules/"+plugin+"/static/tests/frontend/specs/");
async.forEach(specFiles, function(spec){ // for each specFile push it to pluginSpecs
pluginSpecs.push("/static/plugins/"+plugin+"/static/tests/frontend/specs/" + spec);
},
function(err){
// blow up if something bad happens!
});
}
});
callback(null, pluginSpecs);
}
exports.getCoreTests = function(callback){
fs.readdir('tests/frontend/specs', function(err, coreSpecs){ // get the core test specs
if(err){ return res.send(500); }
callback(null, coreSpecs);
});
}