npm package: @orgiltse/service
Description: ============
Author: oril_tse orgilts603@gmail.com
Latest version: 1.0.2

import {
    Router
} from "express";
import fs from "fs";
import path from "path";
import controller from "../utils/controller";
export const requirePath = __basedir => (__dirpath, _routers = [], action = "", onlyRoute) => {
    try {
        let isDirectory = false;
        let ROUTE;
        const accepted = (route) => {
            return !_routers.find(router => {
                return router.path === route.path && router.method === route.method;
            });
        };
        try {
            isDirectory = fs.lstatSync(path.normalize(__basedir + "/" + __dirpath)).isDirectory();
        } catch (err) {
            isDirectory = false;
        }
        if (isDirectory) {
            fs.readdirSync(__basedir + "/" + __dirpath).forEach(file => {
                let subpath = __basedir + "/" + __dirpath + "/" + file;
                let filename = path.normalize(file).replace(/.[^/.]+$/, "");
                if (fs.lstatSync(path.normalize(subpath)).isDirectory()) {
                    if (file === "logics" || file === "interface") return; // eslint-disable-next-line no-param-reassign          
                    _routers = requirePath(__basedir)(path.normalize("/" + __dirpath + "/" + file), _routers, action + "/" + filename);
                } else if (file.indexOf(".js.map") == -1) {
                    let obj = require(subpath);
                    Object.keys(obj).forEach(key => {
                        let model = obj[key];
                        if (Array.isArray(model)) {
                            let path = model[0] || action + "/" + filename, // Path                
                                method = model[1], // Method                
                                func = model[2]; // Function              
                            ROUTE = {
                                path: path,
                                method: method,
                                func: func
                            };
                            if (accepted(ROUTE)) _routers.push(ROUTE);
                        } else if (model) { // ONLY HTTP POST              
                            ROUTE = {
                                path: action + "/" + filename,
                                method: "POST",
                                func: model
                            };
                            if (accepted(ROUTE)) _routers.push(ROUTE);
                        }
                    });
                }
            });
        } else {
            let obj = require(path.normalize(__basedir + "/" + __dirpath));
            Object.keys(obj).forEach(key => {
                let model = obj[key];
                if (Array.isArray(model)) {
                    let path = model[0] || onlyRoute, // Path            
                        method = model[1], // Method            
                        func = model[2]; // Function          
                    ROUTE = {
                        path: path,
                        method: method,
                        func: func
                    };
                    if (accepted(ROUTE)) _routers.push(ROUTE);
                } else if (model) { // ONLY HTTP POST          
                    ROUTE = {
                        path: onlyRoute,
                        method: "POST",
                        func: model
                    };
                    if (accepted(ROUTE)) _routers.push(ROUTE);
                }
            });
        }
    } catch (err) {
        throw err;
    }
    return _routers;
};
export default (__basedir, data) => {
    const router = new Router();
    const routes = (Array.isArray(data) ? data : [data]).reduce((accumulator, iterator) => {
        return [...accumulator, ...requirePath("")(path.join(__basedir, iterator), [], "", iterator)];
    }, []);
    routes.forEach(route => {
        router[route.method.toLowerCase()](route.path, controller(route.func));
    });
    return router;
};