30 lines
598 B
JavaScript
30 lines
598 B
JavaScript
'use strict';
|
|
|
|
const glob = require('glob');
|
|
|
|
/**
|
|
* Map Routes to route files
|
|
*
|
|
* @param {string} path - folder with Routes
|
|
* @return {Object} - Object mapping routes to their files
|
|
*/
|
|
module.exports = function routeLoader(path) {
|
|
|
|
const basePath = path.replace(/\\/g, '/');
|
|
|
|
let paths = glob.sync(`${path}/**/*.js`);
|
|
paths = paths.sort();
|
|
paths = paths.map((path) => path.replace('\\', '/'));
|
|
|
|
let routes = {};
|
|
|
|
paths.forEach((path) => {
|
|
let routePath = path.replace(basePath, '')
|
|
.replace('.js', '')
|
|
.replace('index', '');
|
|
|
|
routes[routePath] = path;
|
|
});
|
|
|
|
return routes;
|
|
}; |