Code coverage report for lib/handlerParser.js

Statements: 93.75% (30 / 32)      Branches: 50% (2 / 4)      Functions: 100% (5 / 5)      Lines: 93.55% (29 / 31)      Ignored: none     

All files » lib/ » handlerParser.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 531         1 1     15 15   15       15 15   15 15 15 15 15 15   15 31 31 31 67     67 67   31 31   31 31 31 31   31     15          
var logger = require("./logger"), 
	fs = require("fs"),
	stripComments = require("strip-json-comments"),
	_ = require("lodash");
 
var CONFIG_FILE_PATH = process.cwd() + "/vatican-conf.json";
module.exports = {
 
	parse: function(dir, cb) {
		var paths = [];
	    fs.readdir(dir, function(err, files) {
 
	        Iif(err) {
	            logger.error("Error reading folder: " + dir);
	            cb("Error reading folder: " + dir);
	        } else {
	            var fpath = "";
	            var regExp = /@endpoint\s*\((url:.+)\s*(method:(?:[ \t]*)(?:get|put|post|delete))\s*(name:.+)?\s*\)[\s\n]*([^\s]*)\.prototype\.([^\s]*)/gim
 
	            _(files).where(function(f) { return f.match(/\.js$/i); }).forEach(function(fname) {
	                fpath = dir + "/" + fname;
	                logger.info("Openning file: " + fpath);
	                var content = fs.readFileSync(fpath).toString();
	                content = content.replace(/\/\/\s*@/g, "@") //We allow commenting the line of the endpoint for correct editor syntax coloring
	                content = stripComments(content) //we remove the comments so we don't deal with commented out endpoints
 
	                while( (matches = regExp.exec(content)) !== null) {
	                    var params = _.compact(matches.slice(1,4))
	                    var currentPath = {};
	                    params.forEach(function(p) {
	                        var parts = p.split(":"),
	                        	key = parts.shift(),
	                            value = parts.join(":").trim();
	                        Eif(value)
	                        	currentPath[key] = value;
	                    })
	                    var actionStr = matches[5]
	                        handlerName = matches[4]
	                    
	                    currentPath['action'] = actionStr.trim();
	                    currentPath['handlerPath'] = fpath;
	                    currentPath['handlerName'] = handlerName
	                    currentPath.method = currentPath.method.toUpperCase()
	                    
	                    paths.push(currentPath);
	                }
	            });
				cb(null, paths);
	        }
	    }); 
	}
};