eggInfo.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. /**
  3. * Getting plugin info in child_process to prevent effecting egg application( splitting scopes ).
  4. */
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const tslib_1 = require("tslib");
  7. require("cache-require-paths");
  8. const fs_1 = tslib_1.__importDefault(require("fs"));
  9. const path_1 = tslib_1.__importDefault(require("path"));
  10. const config_1 = require("../config");
  11. const utils = tslib_1.__importStar(require("../utils"));
  12. const cwd = process.cwd();
  13. const eggInfo = {};
  14. const startTime = Date.now();
  15. if (utils.checkMaybeIsTsProj(cwd)) {
  16. // only require ts-node in ts project
  17. const tsconfigPath = path_1.default.resolve(cwd, './tsconfig.json');
  18. if (fs_1.default.existsSync(tsconfigPath)) {
  19. require('ts-node').register(utils.readJson5(tsconfigPath));
  20. }
  21. else {
  22. require('ts-node/register');
  23. }
  24. }
  25. const framework = (utils.getPkgInfo(cwd).egg || {}).framework || 'egg';
  26. const loader = getLoader(cwd, framework);
  27. if (loader) {
  28. try {
  29. loader.loadPlugin();
  30. }
  31. catch (e) {
  32. // do nothing
  33. }
  34. // hack loadFile, ignore config file without customLoader for faster booting
  35. mockFn(loader, 'loadFile', filepath => {
  36. if (filepath && filepath.substring(filepath.lastIndexOf(path_1.default.sep) + 1).startsWith('config.')) {
  37. const fileContent = fs_1.default.readFileSync(filepath, 'utf-8');
  38. if (!fileContent.includes('customLoader'))
  39. return;
  40. }
  41. return true;
  42. });
  43. try {
  44. loader.loadConfig();
  45. }
  46. catch (e) {
  47. // do nothing
  48. }
  49. eggInfo.plugins = loader.allPlugins;
  50. eggInfo.config = loader.config;
  51. eggInfo.eggPaths = loader.eggPaths;
  52. eggInfo.timing = Date.now() - startTime;
  53. }
  54. utils.writeFileSync(config_1.eggInfoPath, JSON.stringify(eggInfo));
  55. /* istanbul ignore next */
  56. function noop() { }
  57. function mockFn(obj, name, fn) {
  58. const oldFn = obj[name];
  59. obj[name] = (...args) => {
  60. const result = fn.apply(obj, args);
  61. if (result)
  62. return oldFn.apply(obj, args);
  63. };
  64. }
  65. function getLoader(baseDir, framework) {
  66. const frameworkPath = path_1.default.join(baseDir, 'node_modules', framework);
  67. const eggCore = findEggCore(baseDir, frameworkPath);
  68. /* istanbul ignore if */
  69. if (!eggCore)
  70. return;
  71. const EggLoader = eggCore.EggLoader;
  72. const egg = utils.requireFile(frameworkPath) || utils.requireFile(framework);
  73. /* istanbul ignore if */
  74. if (!egg || !EggLoader)
  75. return;
  76. process.env.EGG_SERVER_ENV = 'local';
  77. return new EggLoader({
  78. baseDir,
  79. logger: {
  80. debug: noop,
  81. info: noop,
  82. warn: noop,
  83. error: noop,
  84. },
  85. app: Object.create(egg.Application.prototype),
  86. });
  87. }
  88. function findEggCore(baseDir, frameworkPath) {
  89. let eggCorePath = path_1.default.join(baseDir, 'node_modules/egg-core');
  90. if (!fs_1.default.existsSync(eggCorePath)) {
  91. eggCorePath = path_1.default.join(frameworkPath, 'node_modules/egg-core');
  92. }
  93. // try to load egg-core in cwd
  94. const eggCore = utils.requireFile(eggCorePath);
  95. if (!eggCore) {
  96. // try to resolve egg-core
  97. return utils.requireFile('egg-core');
  98. }
  99. return eggCore;
  100. }