yapi/server/plugin.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-09-05 10:24:13 +08:00
const yapi = require('./yapi.js');
2017-09-06 10:18:13 +08:00
const plugin_path = yapi.path.join(yapi.WEBROOT, 'plugins')
2017-09-05 17:04:59 +08:00
var hooks = {
'third_login': {
type: 'single',
listener: null
},
'add_interface': {
type: 'mulit',
listener: []
}
};
function bindHook(name, listener){
if(!name) throw new Error('缺少hookname');
if(name in hooks === false){
throw new Error('不存在的hookname');
}
if(hooks[name].type === 'multi'){
hooks[name].listener.push(listener);
}else{
hooks[name].listener = listener;
}
}
function emitHook(name){
if(!name) throw new Error('缺少hookname');
if(name in hooks === false){
throw new Error('不存在的hookname');
}
if(hooks[name] && typeof hooks[name] === 'object'){
if(hooks[name].type === 'single' && typeof hooks[name].listener === 'function'){
return hooks[name].listener.apply(yapi, Array.prototype.slice.call(arguments, 1));
}
if(Array.isArray(hooks[name.listener])){
hooks[name].listener.forEach(listener=>{
listener.apply(yapi, Array.prototype.slice.call(arguments,1))
})
}
}
}
yapi.bindHook = bindHook;
yapi.emitHook = emitHook;
2017-09-05 10:24:13 +08:00
module.exports = function(){
if(yapi.WEBCONFIG.plugins && Array.isArray(yapi.WEBCONFIG.plugins)){
yapi.WEBCONFIG.plugins.forEach(plugin=>{
2017-09-05 17:04:59 +08:00
if(!yapi.commons.fileExist(yapi.path.join(plugin_path, 'yapi-plugin-' + plugin + '/server.js'))){
2017-09-06 10:18:13 +08:00
throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件请安装此插件?`);
2017-09-05 17:36:16 +08:00
process.exit();
2017-09-05 17:04:59 +08:00
}
let pluginModule = require(yapi.path.join(plugin_path, 'yapi-plugin-' + plugin + '/server.js'));
pluginModule.apply(yapi)
2017-09-05 10:24:13 +08:00
})
}
}