backend: router hot-reloading

This commit is contained in:
41666 2019-02-25 00:47:08 -06:00
parent 11cf1d4805
commit 959a2eb46f
4 changed files with 53 additions and 20 deletions

View file

@ -3,7 +3,7 @@ const glob = require('glob')
const PROD = process.env.NODE_ENV === 'production'
module.exports = async (router, ctx) => {
module.exports = async (router, ctx, { forceClear = false } = {}) => {
const apis = glob.sync(`./api/**/!(index).js`)
log.debug('found apis', apis)
@ -14,7 +14,11 @@ module.exports = async (router, ctx) => {
}
log.debug(`mounting ${a}`)
try {
require(a.replace('api/', ''))(router, ctx)
const pathname = a.replace('api/', '')
if (forceClear) {
delete require.cache[require.resolve(pathname)]
}
require(pathname)(router, ctx)
} catch (e) {
log.error(`couldn't mount ${a}`, e)
}