mock平台

yapi.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const path = require('path');
  2. const fs = require('fs-extra');
  3. const nodemailer = require('nodemailer');
  4. const config = require('../../config.json');
  5. let insts = new Map();
  6. let mail;
  7. const WEBROOT = path.resolve(__dirname, '..'); //路径
  8. const WEBROOT_SERVER = __dirname;
  9. const WEBROOT_RUNTIME = path.resolve(__dirname, '../..');
  10. const WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log');
  11. const WEBCONFIG = config;
  12. fs.ensureDirSync(WEBROOT_LOG);
  13. if (WEBCONFIG.mail && WEBCONFIG.mail.enable) {
  14. mail = nodemailer.createTransport(WEBCONFIG.mail);
  15. }
  16. /**
  17. * 获取一个model实例,如果不存在则创建一个新的返回
  18. * @param {*} m class
  19. * @example
  20. * yapi.getInst(groupModel, arg1, arg2)
  21. */
  22. function getInst(m, ...args) {
  23. if (!insts.get(m)) {
  24. insts.set(m, new m(args));
  25. }
  26. return insts.get(m);
  27. }
  28. function delInst(m) {
  29. try {
  30. insts.delete(m);
  31. } catch (err) {
  32. console.error(err); // eslint-disable-line
  33. }
  34. }
  35. let r = {
  36. fs: fs,
  37. path: path,
  38. WEBROOT: WEBROOT,
  39. WEBROOT_SERVER: WEBROOT_SERVER,
  40. WEBROOT_RUNTIME: WEBROOT_RUNTIME,
  41. WEBROOT_LOG: WEBROOT_LOG,
  42. WEBCONFIG: WEBCONFIG,
  43. getInst: getInst,
  44. delInst: delInst,
  45. getInsts: insts
  46. };
  47. if (mail) r.mail = mail;
  48. module.exports = r;