all files / lib/ es6HandlerParser.js

100% Statements 25/25
50% Branches 1/2
66.67% Functions 2/3
100% Lines 25/25
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                                                     
 
const stripComments = require("strip-json-comments");
 
const endpointRE = /@endpoint\s*\((url:.+)\s*(method:(?:[ \t]*)(?:get|put|post|delete))\s*(name:.+)?\s*\)[\s\n]*([a-zA-Z0-9_]*)\([a-zA-Z0-9 ,]*\)/gim
const classNameRE = /class ([a-zA-Z-0-9_ ]+) *{/;
 
module.exports = class ES6HandlerParser {
 
 
	static parseString(fpath, content, cb) {
		let classNameParts = content.match(classNameRE);
 
		let handlerClassName = classNameParts[1].trim();
		let matches = [];
 
 
        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
        let paths = [];
 
 
		while( (matches = endpointRE.exec(content)) !== null) {
			console.log(matches)
			
            var params = 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;
            })
            /**/
            let actionStr = matches[4],
                handlerName = handlerClassName;
            
            currentPath['action'] = actionStr.trim();
            currentPath['handlerPath'] = fpath;
            currentPath['handlerName'] = handlerName
            currentPath.method = currentPath.method.toUpperCase()
            
            paths.push(currentPath);
            // */
        }
		cb(null, paths);
	}
 
	static parse(dir, cb) {
 
	}
}