This commit is contained in:
2017-01-28 19:47:09 +08:00
parent 40bddcfcdd
commit d72827d08e
5 changed files with 8 additions and 5 deletions

View File

@ -1,34 +0,0 @@
import fs from 'fs'
import path from 'path'
import compose from 'koa-compose'
function getDirs (srcpath) {
return fs.readdirSync(srcpath).filter(file => {
return fs.statSync(path.join(srcpath, file)).isDirectory()
})
}
module.exports = (srcpath, filename = 'index.js') => {
let plugins = {}
let dirs = getDirs(srcpath)
let list = []
for (let name of dirs) {
let fn = require(path.join(srcpath, name, filename))
if (typeof fn !== 'function' && typeof fn.default === 'function') {
fn = fn.default
} else {
throw (new Error('plugin must be a function!'))
}
plugins[name] = fn
list.push(function (ctx, next) {
return fn(ctx, next) || next()
})
}
return compose(list)
}