diff --git a/core/common-util/index.ts b/core/common-util/index.ts index f1025197..cecc82d1 100644 --- a/core/common-util/index.ts +++ b/core/common-util/index.ts @@ -7,3 +7,4 @@ export * from './src/StackUtil'; export * from './src/ProxyUtil'; export * from './src/ModuleConfig'; export * from './src/TimerUtil'; +export * from './src/RuntimeConfig'; diff --git a/core/common-util/src/RuntimeConfig.ts b/core/common-util/src/RuntimeConfig.ts new file mode 100644 index 00000000..ac1dfb7d --- /dev/null +++ b/core/common-util/src/RuntimeConfig.ts @@ -0,0 +1,18 @@ +export type EnvType = 'local' | 'unittest' | 'prod' | string; + +export interface RuntimeConfig { + /** + * Application name + */ + name: string; + + /** + * Application environment + */ + env: EnvType; + + /** + * Application directory + */ + baseDir: string; +} diff --git a/core/tegg/index.ts b/core/tegg/index.ts index 8a85dd44..e913b9a8 100644 --- a/core/tegg/index.ts +++ b/core/tegg/index.ts @@ -7,3 +7,4 @@ export * from '@eggjs/tegg-background-task'; export * as aop from '@eggjs/aop-decorator'; export * as orm from '@eggjs/tegg-orm-decorator'; export * as schedule from '@eggjs/tegg-schedule-decorator'; +export { RuntimeConfig } from '@eggjs/tegg-common-util'; diff --git a/plugin/tegg/app/extend/application.ts b/plugin/tegg/app/extend/application.ts index 90d10c41..9e4c7667 100644 --- a/plugin/tegg/app/extend/application.ts +++ b/plugin/tegg/app/extend/application.ts @@ -16,7 +16,8 @@ import { LoadUnitInstanceLifecycleUtil, } from '@eggjs/tegg-runtime'; import { LoaderFactory } from '@eggjs/tegg-loader'; -import { EggProtoImplClass, PrototypeUtil, IdenticalUtil } from '@eggjs/tegg'; +import { EggProtoImplClass, PrototypeUtil, IdenticalUtil, RuntimeConfig } from '@eggjs/tegg'; +import type { Application } from 'egg'; export default { // @eggjs/tegg-metadata should not depend by other egg plugins. @@ -78,6 +79,16 @@ export default { return IdenticalUtil; }, + get runtimeConfig(): RuntimeConfig { + const app = this as unknown as Application; + const config = app.config; + return { + baseDir: config.baseDir, + env: config.env, + name: config.name, + }; + }, + async getEggObject(clazz: EggProtoImplClass) { const proto = PrototypeUtil.getClazzProto(clazz); if (!proto) { diff --git a/plugin/tegg/test/EggCompatible.test.ts b/plugin/tegg/test/EggCompatible.test.ts index fc932401..f3bbf359 100644 --- a/plugin/tegg/test/EggCompatible.test.ts +++ b/plugin/tegg/test/EggCompatible.test.ts @@ -81,6 +81,13 @@ describe('test/EggCompatible.test.ts', () => { await app.mockModuleContextScope(async () => { const baseDir = app.module.multiModuleService.configService.getBaseDir(); assert(baseDir); + + const runtimeConfig = app.module.multiModuleService.configService.getRuntimeConfig(); + assert.deepEqual(runtimeConfig, { + baseDir: path.join(__dirname, 'fixtures/apps/egg-app'), + env: 'unittest', + name: 'egg-app', + }); }); }); diff --git a/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/ConfigService.ts b/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/ConfigService.ts index 47ee9c21..df55c324 100644 --- a/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/ConfigService.ts +++ b/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/ConfigService.ts @@ -1,4 +1,4 @@ -import { AccessLevel, SingletonProto, Inject } from '@eggjs/tegg'; +import { AccessLevel, SingletonProto, Inject, RuntimeConfig } from '@eggjs/tegg'; import { EggAppConfig } from 'egg'; interface XSessionUser { @@ -10,10 +10,13 @@ interface XSessionUser { }) export default class ConfigService { @Inject() - user: XSessionUser; + private user: XSessionUser; @Inject() - config: EggAppConfig; + private config: EggAppConfig; + + @Inject() + private runtimeConfig: RuntimeConfig; getBaseDir(): string { return this.config.baseDir; @@ -22,4 +25,9 @@ export default class ConfigService { async getCurrentUserName(): Promise { return this.user.userName; } + + getRuntimeConfig(): RuntimeConfig { + return this.runtimeConfig; + } + } diff --git a/standalone/standalone/src/Runner.ts b/standalone/standalone/src/Runner.ts index e3608efe..65738146 100644 --- a/standalone/standalone/src/Runner.ts +++ b/standalone/standalone/src/Runner.ts @@ -1,4 +1,4 @@ -import { ModuleConfigUtil, ModuleReference } from '@eggjs/tegg-common-util'; +import { ModuleConfigUtil, ModuleReference, RuntimeConfig } from '@eggjs/tegg-common-util'; import { EggPrototype, LoadUnit, @@ -55,6 +55,14 @@ export class Runner { moduleConfig: [], }; + const runtimeConfig: Partial = { + baseDir: this.cwd, + }; + // Inject runtimeConfig + this.innerObjects.runtimeConfig = [{ + obj: runtimeConfig, + }]; + for (const reference of this.moduleReferences) { const absoluteRef = { path: ModuleConfigUtil.resolveModuleDir(reference.path, this.cwd), diff --git a/standalone/standalone/src/StandaloneLoadUnit.ts b/standalone/standalone/src/StandaloneLoadUnit.ts index 9a36bd58..5e054b8a 100644 --- a/standalone/standalone/src/StandaloneLoadUnit.ts +++ b/standalone/standalone/src/StandaloneLoadUnit.ts @@ -40,7 +40,6 @@ export class StandaloneLoadUnit implements LoadUnit { } } - containPrototype(proto: EggPrototype): boolean { return !!(this.protoMap.get(proto.name)?.find(t => t === proto)); } diff --git a/standalone/standalone/test/fixtures/inner-object/foo.ts b/standalone/standalone/test/fixtures/inner-object/foo.ts index 793c8726..61d66e89 100644 --- a/standalone/standalone/test/fixtures/inner-object/foo.ts +++ b/standalone/standalone/test/fixtures/inner-object/foo.ts @@ -14,4 +14,5 @@ export class Foo implements MainRunner { async main(): Promise { return this.hello.hello(); } + } diff --git a/standalone/standalone/test/fixtures/runtime-config/foo.ts b/standalone/standalone/test/fixtures/runtime-config/foo.ts new file mode 100644 index 00000000..ab76aac3 --- /dev/null +++ b/standalone/standalone/test/fixtures/runtime-config/foo.ts @@ -0,0 +1,15 @@ +import { Inject, SingletonProto } from '@eggjs/tegg'; +import { RuntimeConfig } from '@eggjs/tegg-common-util'; +import { Runner, MainRunner } from '@eggjs/tegg/standalone'; + +@Runner() +@SingletonProto() +export class Foo implements MainRunner { + @Inject() + runtimeConfig: RuntimeConfig; + + async main(): Promise { + return this.runtimeConfig; + } + +} diff --git a/standalone/standalone/test/fixtures/runtime-config/package.json b/standalone/standalone/test/fixtures/runtime-config/package.json new file mode 100644 index 00000000..53cf3224 --- /dev/null +++ b/standalone/standalone/test/fixtures/runtime-config/package.json @@ -0,0 +1,6 @@ +{ + "name": "runtime-config", + "eggModule": { + "name": "runtime-config" + } +} diff --git a/standalone/standalone/test/index.test.ts b/standalone/standalone/test/index.test.ts index 05a9080e..82ad83ff 100644 --- a/standalone/standalone/test/index.test.ts +++ b/standalone/standalone/test/index.test.ts @@ -65,4 +65,12 @@ describe('test/index.test.ts', () => { assert.deepStrictEqual(configs.get('bar'), bar); }); }); + + describe('runner with runtimeConfig', () => { + it('should work', async () => { + const msg = await main(path.join(__dirname, './fixtures/runtime-config')); + assert.deepEqual(msg, { baseDir: path.join(__dirname, './fixtures/runtime-config') }); + }); + }); + });