-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const fsReaddirRecursive = require("fs-readdir-recursive")
const path = require("path")
const { promises: fsPromise } = require("fs")
const _ = require("lodash")
module.exports = ({ path: folderPath = process.env.PWD + "/routes" }) => (app, options, next) => {
const dir = fsReaddirRecursive("./routes")
const toObject = path => {
return {
path
}
}
const addFileName = require("./libs/addFileName")
const addName = require("./libs/addName")
const addMethod = require("./libs/addMethod")
const addURL = require("./libs/addURL")
const remove = require("./libs/remove")
const routes = dir
.map(toObject)
.map(addFileName)
.map(addName)
.map(addMethod)
.map(addURL)
.map(remove(["fileName", "name"]))
console.log(routes)
routes.forEach(route => {
const handler = require([folderPath, route.path].join('/'))(app)
app.route({
...route,
handler
})
})
const jsonRoutes = JSON.stringify(routes.map(remove(["path"])), null, 2)
fsPromise.writeFile("routes.json", jsonRoutes)
next()
}