index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const mm = require('mm');
  3. const cluster = require('./lib/cluster');
  4. const app = require('./lib/app');
  5. // egg-bin will set this flag to require files for instrument
  6. if (process.env.EGG_BIN_PREREQUIRE) require('./lib/prerequire');
  7. /**
  8. * @namespace mm
  9. */
  10. function mock(...args) {
  11. return mm(...args);
  12. }
  13. module.exports = mock;
  14. module.exports.default = mock;
  15. // inherit & extends mm
  16. Object.assign(mock, mm, {
  17. restore() {
  18. cluster.restore();
  19. mm.restore();
  20. },
  21. /**
  22. * Create a egg mocked application
  23. * @function mm#app
  24. * @param {Object} [options]
  25. * - {String} baseDir - The directory of the application
  26. * - {Object} plugins - Tustom you plugins
  27. * - {String} framework - The directory of the egg framework
  28. * - {Boolean} [true] cache - Cache application based on baseDir
  29. * - {Boolean} [true] coverage - Swtich on process coverage, but it'll be slower
  30. * - {Boolean} [true] clean - Remove $baseDir/logs
  31. * @return {App} return {@link Application}
  32. * @example
  33. * ```js
  34. * var app = mm.app();
  35. * ```
  36. */
  37. app,
  38. /**
  39. * Create a egg mocked cluster application
  40. * @function mm#cluster
  41. * @see ClusterApplication
  42. */
  43. cluster,
  44. /**
  45. * mock the serverEnv of Egg
  46. * @member {Function} mm#env
  47. * @param {String} env - contain default, test, prod, local, unittest
  48. * @see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L78
  49. */
  50. env(env) {
  51. mm(process.env, 'EGG_MOCK_SERVER_ENV', env);
  52. mm(process.env, 'EGG_SERVER_ENV', env);
  53. },
  54. /**
  55. * mock console level
  56. * @param {String} level - logger level
  57. */
  58. consoleLevel(level) {
  59. level = (level || '').toUpperCase();
  60. mm(process.env, 'EGG_LOG', level);
  61. },
  62. home(homePath) {
  63. if (homePath) {
  64. mm(process.env, 'EGG_HOME', homePath);
  65. }
  66. },
  67. });
  68. process.setMaxListeners(100);
  69. process.once('SIGQUIT', () => {
  70. process.exit(0);
  71. });
  72. process.once('SIGTERM', () => {
  73. process.exit(0);
  74. });
  75. process.once('SIGINT', () => {
  76. process.exit(0);
  77. });