mock平台

plugin.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const _ = require('underscore');
  2. function getPluginConfig(name, type) {
  3. let pluginConfig;
  4. if (type === 'ext') {
  5. pluginConfig = require('../exts/yapi-plugin-' + name);
  6. } else {
  7. pluginConfig = require('yapi-plugin-' + name);
  8. }
  9. if (!pluginConfig || typeof pluginConfig !== 'object') {
  10. throw new Error(`Plugin ${name} Config 配置错误,请检查 yapi-plugin-${name}/index.js`);
  11. }
  12. return {
  13. server: pluginConfig.server,
  14. client: pluginConfig.client
  15. }
  16. }
  17. /**
  18. * type @string enum[plugin, ext] plugin是外部插件,ext是内部插件
  19. */
  20. exports.initPlugins = function (plugins, type) {
  21. if (!plugins) {
  22. return [];
  23. }
  24. if (typeof plugins !== 'object' || !Array.isArray(plugins)) {
  25. throw new Error('插件配置有误,请检查', plugins);
  26. }
  27. plugins = plugins.map(item => {
  28. let pluginConfig;
  29. if (item && typeof item === 'string') {
  30. pluginConfig = getPluginConfig(item, type);
  31. return Object.assign({}, pluginConfig, { name: item, enable: true })
  32. } else if (item && typeof item === 'object') {
  33. pluginConfig = getPluginConfig(item.name, type);
  34. return Object.assign({},
  35. pluginConfig,
  36. {
  37. name: item.name,
  38. options: item.options,
  39. enable: item.enable === false ? false : true
  40. })
  41. }
  42. })
  43. plugins = plugins.filter(item => {
  44. return item.enable === true && (item.server || item.client)
  45. })
  46. return _.uniq(plugins, item => item.name)
  47. }