{"size": 2010, "ext": "js", "max_stars_repo_path": "app/router.js", "max_stars_repo_name": "AoiYamada/chatroom-server", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/router.js", "max_issues_repo_name": "AoiYamada/chatroom-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/router.js", "max_forks_repo_name": "AoiYamada/chatroom-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.7692307692, "max_line_length": 106, "alphanum_fraction": 0.5562189055}
const path = require("path");
const fs = require("fs");
const {
  PATHS: { APP },
} = require("config");
const Router = require("koa-router");
const router = new Router();

const paths = walk(APP);
const empty_regex = new RegExp(`${paths.empty_folders.map(v => `(?!\/${v}\/)`).join("")}/([\\w]+)`, "g");

for (const file_path of paths.files) {
  const dir_obj = path.parse(file_path);
  if (dir_obj.ext === ".js" && !dir_obj.name.startsWith("_")) {
    const method = dir_obj.name;

    const controller = dir_obj.dir.split(/\\|\//).slice(-1)[0];
    const base_with_ID = dir_obj.dir
      .replace(APP, "")
      .replace(/\\/g, "/")
      .replace(empty_regex, "/$1/:$1_ID");

    const base_without_ID = base_with_ID.slice(0, -`:${controller}_ID`.length);
    const handler = require(file_path);

    switch (method) {
      case "get":
      case "post":
      case "put":
      case "patch":
      case "delete":
        router[method](base_without_ID, ...handler);
        router[method](base_with_ID, ...handler);
      case "head":
      case "options":
        // Pending
        break;
      default:
    }
  }
}

module.exports = router;

// helper(s)
// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
function walk(dir) {
  const files = [];
  const empty_folders = [];
  const list = fs.readdirSync(dir);
  if (!list.some(v => ~v.indexOf(".js"))) {
    empty_folders.push(path.parse(dir).base);
  }

  let pending = list.length;
  if (!pending)
    return {
      files,
      empty_folders,
    };
  for (let file of list) {
    file = path.resolve(dir, file);
    const stat = fs.statSync(file);
    if (stat && stat.isDirectory()) {
      const res = walk(file);
      files.push(...res.files);
      empty_folders.push(...res.empty_folders);
    } else {
      files.push(file);
    }
    if (!--pending)
      return {
        files,
        empty_folders,
      };
  }
}
