Verified Commit 360196b0 authored by Aral Balkan's avatar Aral Balkan
Browse files

No longer hardcoded to only work with a root folder called .routes

parent 1970fa24
......@@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Nothing yet.
## [1.1.1] - 2019-05-15
### Fixed
- No longer hardcoded to only work with a root folder called `.routes`.
## [1.1.0] - 2019-05-14
### Added
......
const fs = require('fs')
const path = require('path')
let directoryRoot = null
function routes (directory) {
let _routes = []
if (directoryRoot === null) directoryRoot = new RegExp(`^.*${directory.replace('.', '\\.')}`)
const files = fs.readdirSync(directory, {withFileTypes: true})
files.forEach(file => {
......@@ -22,7 +26,7 @@ function routes (directory) {
// File.
//
let routeCallbackFilePath = path.resolve(path.join(directory, file.name))
let routeUrlPath = path.join(directory.replace(/.*\.routes/, ''), file.name.replace('.js', ''))
let routeUrlPath = path.join(directory.replace(directoryRoot, ''), file.name.replace('.js', ''))
routeUrlPath = routeUrlPath.replace(/index$/, '')
routeUrlPath = routeUrlPath.replace(/\/$/, '')
if (!routeUrlPath.startsWith('/')) routeUrlPath = `/${routeUrlPath}`
......
{
"name": "@ind.ie/web-routes-from-files",
"version": "1.1.0",
"version": "1.1.1",
"description": "Recursively traverses a given directory structure and uses convention to create a list of web route objects that map url paths to JavaScript callback files.",
"main": "index.js",
"directories": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment