yapi/common/lib.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-10-05 09:12:44 +08:00
const path = require('path');
2017-09-17 13:36:51 +08:00
2017-10-05 09:12:44 +08:00
function getPluginConfig(name, type) {
let pluginConfig;
if(type === 'ext'){
pluginConfig = require('../exts/yapi-plugin-' + name);
}else {
pluginConfig = require('../node_modules/yapi-plugin-' + name);
}
2017-09-30 19:27:06 +08:00
if(!pluginConfig || typeof pluginConfig !== 'object'){
2017-10-05 09:12:44 +08:00
throw new Error(`Plugin ${name} Config 配置错误,请检查 yapi-plugin-${name}/index.js`);
}
return {
server: pluginConfig.server,
client: pluginConfig.client
2017-09-30 19:27:06 +08:00
}
}
2017-09-17 13:36:51 +08:00
module.exports = {
2017-10-05 09:12:44 +08:00
/**
* type @string enum[plugin, ext] plugin是外部插件ext是内部插件
*/
initPlugins: function (plugins, type) {
2017-09-17 13:36:51 +08:00
if (!plugins) {
return [];
}
if (typeof plugins !== 'object' || !Array.isArray(plugins)) {
2017-09-30 19:27:06 +08:00
throw new Error('插件配置有误,请检查', plugins);
2017-09-17 13:36:51 +08:00
}
return plugins.map(item => {
2017-09-30 19:27:06 +08:00
let pluginConfig;
2017-09-17 13:36:51 +08:00
if (item && typeof item === 'string') {
2017-10-05 09:12:44 +08:00
pluginConfig = getPluginConfig(item, type);
return Object.assign({}, pluginConfig, { name: item, enable: true })
2017-09-17 13:36:51 +08:00
} else if (item && typeof item === 'object') {
2017-10-05 09:12:44 +08:00
pluginConfig = getPluginConfig(item.name, type);
return Object.assign({},
pluginConfig,
{
name: item.name,
options: item.options,
enable: item.enable === false ? false : true
})
2017-09-17 13:36:51 +08:00
}
})
}
}