Last active
March 2, 2018 14:05
-
-
Save devinivy/12f4f27107c9f3e9a7ec3348900acd8e to your computer and use it in GitHub Desktop.
recursive require-dir for hapi routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const tree = require('require-dir')(null, { | |
recurse: true // Consider also adding filter() to ignore e.g. helper directories | |
}); | |
const flatten = (routes, obj) => { | |
if (Array.isArray(obj) || (obj.path && obj.method)) { | |
return routes.concat(obj); | |
} | |
return routes.concat( | |
Object.keys(obj) | |
.map((filename) => obj[filename]) | |
.reduce(flatten, []) | |
); | |
}; | |
module.exports = flatten([], tree); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment