UNPKG

@midwayjs/web

Version:

Midway Web Framework for Egg.js

119 lines 5.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCurrentDateString = exports.findLernaRoot = exports.parseNormalDir = void 0; exports.initializeAgentApplicationContext = initializeAgentApplicationContext; const path_1 = require("path"); const find_up_1 = require("find-up"); const fs_1 = require("fs"); const core_1 = require("@midwayjs/core"); const core_2 = require("@midwayjs/core"); const web_1 = require("./framework/web"); const util_1 = require("util"); const interface_1 = require("./interface"); const debug = (0, util_1.debuglog)('midway:debug'); const parseNormalDir = (baseDir, isTypescript = true) => { if (isTypescript) { // 这里要么就是 src 目录,要么就已经是根目录 if (!(0, fs_1.existsSync)((0, path_1.join)(baseDir, 'package.json'))) { baseDir = (0, path_1.basename)(baseDir); } const isTypeScriptEnv = (0, core_1.isTypeScriptEnvironment)(); if (isTypeScriptEnv) { return { baseDir: (0, path_1.join)(baseDir, process.env.MIDWAY_SOURCE_DIR || 'src'), appDir: baseDir, }; } else { return { baseDir: (0, path_1.join)(baseDir, process.env.MIDWAY_SOURCE_DIR || 'dist'), appDir: baseDir, }; } } else { // js baseDir return { baseDir, appDir: baseDir, }; } }; exports.parseNormalDir = parseNormalDir; const findLernaRoot = (findRoot = process.cwd()) => { const userHome = process.env.HOME; return (0, find_up_1.sync)(directory => { if (find_up_1.sync.exists((0, path_1.join)(directory, 'lerna.json'))) { return directory; } if (directory === userHome) { return find_up_1.stop; } }, { cwd: findRoot, type: 'directory' }); }; exports.findLernaRoot = findLernaRoot; const getCurrentDateString = (timestamp = Date.now()) => { const d = new Date(timestamp); return `${d.getFullYear()}-${(d.getMonth() + 1) .toString() .padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`; }; exports.getCurrentDateString = getCurrentDateString; async function initializeAgentApplicationContext(agent) { const applicationContext = (0, core_1.getCurrentApplicationContext)(); const agentFramework = new web_1.MidwayWebFramework(applicationContext); agentFramework['logger'] = agent.coreLogger; agentFramework['appLogger'] = agent.logger; agentFramework.app = agent; agentFramework['configService'] = applicationContext.get(core_1.MidwayConfigService); agentFramework['environmentService'] = applicationContext.get(core_1.MidwayEnvironmentService); agentFramework['loggerService'] = applicationContext.get(core_1.MidwayLoggerService); agentFramework['informationService'] = applicationContext.get(core_1.MidwayInformationService); agentFramework.overwriteApplication('agent'); // init decorator service const decoratorService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayDecoratorService); // register @Logger decorator handler decoratorService.registerPropertyHandler(interface_1.EGG_AGENT_APP_KEY, (propertyName, meta) => { return agent; }); if (process.env['EGG_CLUSTER_MODE'] === 'true') { // init aop support const aspectService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayAspectService); const configService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayConfigService); const loggerService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayLoggerService); // framework/config/plugin/logger/app decorator support // register base config hook decoratorService.registerPropertyHandler(core_2.CONFIG_KEY, (propertyName, meta) => { if (meta.identifier === core_2.ALL_VALUE_KEY) { return configService.getConfiguration(); } else { return configService.getConfiguration(meta.identifier ?? propertyName); } }); // register @Logger decorator handler decoratorService.registerPropertyHandler(core_2.LOGGER_KEY, (propertyName, meta) => { return loggerService.getLogger(meta.identifier ?? propertyName); }); // register @App decorator handler decoratorService.registerPropertyHandler(core_2.APPLICATION_KEY, (propertyName, mete) => { return agent; }); // register @MainApp decorator handler decoratorService.registerPropertyHandler(core_1.MAIN_APPLICATION_KEY, (key, target) => { return agent; }); // register @Plugin decorator handler decoratorService.registerPropertyHandler(core_2.PLUGIN_KEY, (key, target) => { return agent[key]; }); // init aspect module await aspectService.loadAspect(); debug('[egg]: added extra for "initializeAgentApplicationContext" in cluster mode'); } else { debug('[egg]: "initializeAgentApplicationContext" ignore re-init in single process'); } return applicationContext; } //# sourceMappingURL=utils.js.map