{"size": 4014, "ext": "js", "max_stars_repo_path": "examples/node_modules/io/lib/ManageRoutes.js", "max_stars_repo_name": "Vinayk93/io", "max_stars_repo_stars_event_min_datetime": "2018-02-25T00:23:38.000Z", "max_stars_repo_stars_event_max_datetime": "2018-02-25T04:18:58.000Z", "max_issues_repo_path": "examples/node_modules/io/lib/ManageRoutes.js", "max_issues_repo_name": "Vinayk93/io", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "examples/node_modules/io/lib/ManageRoutes.js", "max_forks_repo_name": "Vinayk93/io", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 36.4909090909, "max_line_length": 116, "alphanum_fraction": 0.5558046836}
/*jslint node: true */
/*jshint globalstrict: true*/
'use strict';
var YAML = require('yamljs');
var fs = require('fs');
var writeFile = require('write-file');

var Routes = function (app) {
    this.app = app;
    this.YAML = {};
    this.modules = {};
};

function createfolder(filepath, callback) {
    fs.stat(filepath, function (err, stat) {
        if (err) {
            console.log("file is not found");
            writeFile(filepath,`
                /*jslint node: true */
/*jshint globalstrict: true*/
'use strict';
exports.handlers = function (event, context, callback) {
    console.log(event); //parameter passed in it
    console.log(context);
    callback(null, '{"statusCode":400,"message":"this is the response"}');
};`, function (err2) {
                if (err2) {
                    console.log(err2);
                }
                console.log('file is written');
            });
        } else {
            // console.log(stat);
            callback(filepath);
        }
    });
}

Routes.prototype.Implement = function (Yaml_file, callback) {
    var self = this;
    console.log(Yaml_file);
    /**
     * Now we need to convert yaml to json and make it to the work for all routes defined
     * align all the app routes to middlewares[multiple] and main sections
     */
    YAML.load(global.FolderLocation + "/" + Yaml_file, function (convertedYaml) {
        console.log(convertedYaml.paths);
        self.YAML = convertedYaml;
        var allpaths = convertedYaml.paths,
            alltags = [],
            path = 0,
            method = "",
            filepath = "",
            folderpath = "";
        for (path in allpaths) {
            for (method in allpaths[path]) {
                folderpath = global.FolderLocation +'/controllers/' + allpaths[path][method].tags[0] + path;
                console.log(path);
                filepath = folderpath + "/" + method + ".js";
                self.modules[path.replace(/\//g,"_")+ method.toLowerCase()] = require(filepath);

                createfolder(filepath, function(return_filepath) {
                    // console.log(data);
                    console.log(return_filepath);

                    // this.modules['']
                    alltags.push(return_filepath);
                });
            }
            // console.log(allpaths[path]);    
        }
        console.log(alltags);
        //now import all modules in a context
        //get all the corresonding folders in the YAML then parse it to array
        callback("done");
    });
};

Routes.prototype.MainAPP = function () {
    var self=this;
    // console.log(this.YAML);
    this.app.use('/*', function (req, res, next) {
        console.log(req.originalUrl);
        //if(path having any . mean request for file then redirect to assets)
        var rx_static = /\b[\w\/]*\.[\w]*/;
        if (!rx_static.test(req.originalUrl)) {
            //find in the yaml
            console.log("dynamic content");
            //find particluar domain and then call that function as it need to show that we have something.
            // all exported files in one place so no one really do about it like import them all inside
            var Callfolder = req.originalUrl.replace(/\//g,"_") + req.method.toLowerCase();
            // now here get swagger content of that file name allpath[path][method]get request
            // console.log(self.modules);
            // var DeviceModelBrandget = require('/Users/vk/test/io/examples/controllers/DeviceModel/brand/get.js');
            self.modules[Callfolder].handlers(req.params || req.body , req.headers , function(err, response) {
                console.log(err, response);
                res.send(response);
                // if you find statuscode or code then pass it through
            });
        } else {
            // then show static content
            next();
        }
        // res.send("bahen ke lode");
    });
};

module.exports = function (app) {
    return new Routes(app);
};