import { basename , join } from 'path' ; import { sync as findUpSync , stop } from 'find-up' ; import { existsSync } from 'fs' ; import { DirectoryFileDetector , getCurrentApplicationContext , IMidwayBootstrapOptions , MidwayAspectService , MidwayConfigService , MidwayContainer , MidwayDecoratorService , MidwayEnvironmentService , MidwayInformationService , MidwayLoggerService , MidwayPipelineService , REQUEST_OBJ_CTX_KEY , safeRequire , } from '@midwayjs/core' ; import { ALL , APPLICATION_KEY , bindContainer , CONFIG_KEY , LOGGER_KEY , PIPELINE_IDENTIFIER , PLUGIN_KEY , } from '@midwayjs/decorator' ; import { MidwayWebFramework } from './framework/web' ; import { debuglog } from 'util' ; const debug = debuglog ( 'midway:debug' ) ; export function isTypeScriptEnvironment ( ) { const TS_MODE_PROCESS_FLAG = process . env . MIDWAY_TS_MODE ; if ( 'false' === TS_MODE_PROCESS_FLAG ) { return false ; } return TS_MODE_PROCESS_FLAG === 'true' || ! ! require . extensions [ '.ts' ] ; } export const parseNormalDir = ( baseDir , isTypescript = true ) => { if ( isTypescript ) { if ( ! existsSync ( join ( baseDir , 'package.json' ) ) ) { baseDir = basename ( baseDir ) ; } const isTypeScriptEnv = isTypeScriptEnvironment ( ) ; if ( isTypeScriptEnv ) { return { baseDir : join ( baseDir , 'src' ) , appDir : baseDir , } ; } else { return { baseDir : join ( baseDir , 'dist' ) , appDir : baseDir , } ; } } else { return { baseDir , appDir : baseDir , } ; } } ; export const findLernaRoot = ( findRoot = process . cwd ( ) ) => { const userHome = process . env . HOME ; return findUpSync ( directory => { if ( findUpSync . exists ( join ( directory , 'lerna.json' ) ) ) { return directory ; } if ( directory === userHome ) { return stop ; } } , { cwd : findRoot , type : 'directory' } ) ; } ; export 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' ) }` ; } ; export async function initializeAgentApplicationContext ( agent , globalOptions ) { if ( ! getCurrentApplicationContext ( ) ) { debug ( '[egg]: start "initializeGlobalApplicationContext"' ) ; const appDir = globalOptions . appDir ?? '' ; const baseDir = globalOptions . baseDir ?? '' ; const applicationContext = new MidwayContainer ( ) ; debug ( '[egg]: delegate module map from decoratorManager' ) ; bindContainer ( applicationContext ) ; global [ 'MIDWAY_APPLICATION_CONTEXT' ] = applicationContext ; applicationContext . registerObject ( 'baseDir' , baseDir ) ; applicationContext . registerObject ( 'appDir' , appDir ) ; if ( globalOptions . moduleDirector !== false ) { if ( globalOptions . moduleDetector === undefined || globalOptions . moduleDetector === 'file' ) { applicationContext . setFileDetector ( new DirectoryFileDetector ( { loadDir : baseDir , ignore : globalOptions . ignore ?? [ ] , } ) ) ; } else if ( globalOptions . moduleDetector ) { applicationContext . setFileDetector ( globalOptions . moduleDetector ) ; } } applicationContext . bindClass ( MidwayEnvironmentService ) ; applicationContext . bindClass ( MidwayInformationService ) ; applicationContext . bindClass ( MidwayDecoratorService ) ; applicationContext . bindClass ( MidwayConfigService ) ; applicationContext . bindClass ( MidwayAspectService ) ; applicationContext . bindClass ( MidwayLoggerService ) ; if ( globalOptions . preloadModules && globalOptions . preloadModules . length ) { for ( const preloadModule of globalOptions . preloadModules ) { applicationContext . bindClass ( preloadModule ) ; } } const configService = await applicationContext . getAsync ( MidwayConfigService ) ; configService . add ( [ { default : agent . config , } , ] ) ; const aspectService = await applicationContext . getAsync ( MidwayAspectService , [ applicationContext ] ) ; const decoratorService = await applicationContext . getAsync ( MidwayDecoratorService , [ applicationContext ] ) ; if ( ! globalOptions . imports ) { globalOptions . imports = [ safeRequire ( join ( globalOptions . baseDir , 'configuration' ) ) , ] ; } for ( const configurationModule of [ ] . concat ( globalOptions . imports ) ) { applicationContext . load ( configurationModule ) ; } await applicationContext . ready ( ) ; await configService . load ( ) ; debug ( '[core]: Current config = %j' , configService . getConfiguration ( ) ) ; await applicationContext . getAsync ( MidwayLoggerService , [ applicationContext , ] ) ; decoratorService . registerPropertyHandler ( CONFIG_KEY , ( propertyName , meta ) => { if ( meta . identifier === ALL ) { return this . configService . getConfiguration ( ) ; } else { return this . configService . getConfiguration ( meta . identifier ?? propertyName ) ; } } ) ; decoratorService . registerPropertyHandler ( LOGGER_KEY , ( propertyName , meta ) => { return this . loggerService . getLogger ( meta . identifier ?? propertyName ) ; } ) ; decoratorService . registerPropertyHandler ( PIPELINE_IDENTIFIER , ( key , meta , instance ) => { return new MidwayPipelineService ( instance [ REQUEST_OBJ_CTX_KEY ] ?. requestContext ?? this . applicationContext , meta . valves ) ; } ) ; decoratorService . registerPropertyHandler ( APPLICATION_KEY , ( propertyName , mete ) => { return agent ; } ) ; decoratorService . registerPropertyHandler ( PLUGIN_KEY , ( key , target ) => { return this . agent [ key ] ; } ) ; await aspectService . loadAspect ( ) ; } else { debug ( '[egg]: "initializeAgentApplicationContext" ignore re-init in single process' ) ; } const applicationContext = getCurrentApplicationContext ( ) ; const agentFramework = new MidwayWebFramework ( applicationContext ) ; agentFramework . app = agent ; agentFramework . configService = applicationContext . get ( MidwayConfigService ) ; agentFramework . overwriteApplication ( 'agent' ) ; return applicationContext ; }