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