From a1e988ee556cec6bd5cbf5e8300535ddd4c8ca12 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 23 Aug 2023 05:38:53 -0300 Subject: [PATCH 1/6] feat: implement RuntimeConfigUtil Get RuntimeConfig through RuntimeConfigUtil It contains - baseDir - env - name --- core/common-util/index.ts | 1 + core/common-util/package.json | 1 + core/common-util/src/RuntimeConfigUtil.ts | 41 +++++++++++++++++++ .../test/RuntimeConfigUtil.test.ts | 34 +++++++++++++++ core/tegg/index.ts | 1 + plugin/tegg/app.ts | 6 +++ standalone/standalone/src/Runner.ts | 5 ++- 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 core/common-util/src/RuntimeConfigUtil.ts create mode 100644 core/common-util/test/RuntimeConfigUtil.test.ts diff --git a/core/common-util/index.ts b/core/common-util/index.ts index f1025197..80425f08 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/RuntimeConfigUtil'; diff --git a/core/common-util/package.json b/core/common-util/package.json index f890c0ad..c7394eaf 100644 --- a/core/common-util/package.json +++ b/core/common-util/package.json @@ -37,6 +37,7 @@ }, "dependencies": { "globby": "^11.1.0", + "is-type-of": "^2.0.1", "js-yaml": "^3.14.0" }, "publishConfig": { diff --git a/core/common-util/src/RuntimeConfigUtil.ts b/core/common-util/src/RuntimeConfigUtil.ts new file mode 100644 index 00000000..6a3353d7 --- /dev/null +++ b/core/common-util/src/RuntimeConfigUtil.ts @@ -0,0 +1,41 @@ +import is from 'is-type-of'; +export type EnvType = 'local' | 'unittest' | 'prod' | string; + +export interface RuntimeConfig { + /** + * Application name + */ + name: string; + + /** + * Application environment + */ + env: EnvType; + + /** + * Application directory + */ + baseDir: string; +} + +export class RuntimeConfigUtil { + static #config: Map; + + static setRuntimeConfig(config: Partial) { + for (const key of Object.keys(config) as Array) { + const val = config[key]; + if (is.nullable(val)) continue; + if (!is.string(val)) throw new Error(`The value of ${key} must be a string`); + this.#config.set(key, val); + } + } + + static getRuntimeConfig(): RuntimeConfig { + return { + name: this.#config.get('name')!, + env: this.#config.get('env') as EnvType, + baseDir: this.#config.get('baseDir')!, + }; + } + +} diff --git a/core/common-util/test/RuntimeConfigUtil.test.ts b/core/common-util/test/RuntimeConfigUtil.test.ts new file mode 100644 index 00000000..ad281ece --- /dev/null +++ b/core/common-util/test/RuntimeConfigUtil.test.ts @@ -0,0 +1,34 @@ +import { RuntimeConfigUtil } from '..'; +import assert from 'assert'; + +describe('test/RuntimeConfigUtil.test.ts', () => { + + it('should work', () => { + RuntimeConfigUtil.setRuntimeConfig({ + baseDir: '/tmp', + env: 'prod', + name: 'test', + }); + assert.deepEqual(RuntimeConfigUtil.getRuntimeConfig(), { + baseDir: '/tmp', + env: 'prod', + name: 'test', + }); + + RuntimeConfigUtil.setRuntimeConfig({ + name: 'test2', + }); + assert.deepEqual(RuntimeConfigUtil.getRuntimeConfig(), { + baseDir: '/tmp', + env: 'prod', + name: 'test2', + }); + }); + + it('should check', () => { + assert.throws(() => { + RuntimeConfigUtil.setRuntimeConfig({ baseDir: 1 as any }); + }, /The value of baseDir must be a string/); + }); + +}); diff --git a/core/tegg/index.ts b/core/tegg/index.ts index 8a85dd44..5d8d51a1 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 { RuntimeConfigUtil } from '@eggjs/tegg-common-util'; diff --git a/plugin/tegg/app.ts b/plugin/tegg/app.ts index 35bc738a..eece9aba 100644 --- a/plugin/tegg/app.ts +++ b/plugin/tegg/app.ts @@ -2,6 +2,7 @@ import './lib/AppLoadUnit'; import './lib/AppLoadUnitInstance'; import './lib/EggCompatibleObject'; import { Application } from 'egg'; +import { RuntimeConfigUtil } from '@eggjs/tegg-common-util'; import { EggContextCompatibleHook } from './lib/EggContextCompatibleHook'; import { CompatibleUtil } from './lib/CompatibleUtil'; import { ModuleHandler } from './lib/ModuleHandler'; @@ -20,6 +21,11 @@ export default class App { } configWillLoad() { + RuntimeConfigUtil.setRuntimeConfig({ + baseDir: this.app.baseDir, + env: this.app.config.env, + name: this.app.config.name, + }); this.app.config.coreMiddleware.push('teggCtxLifecycleMiddleware'); } diff --git a/standalone/standalone/src/Runner.ts b/standalone/standalone/src/Runner.ts index e3608efe..2bd65f93 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, RuntimeConfigUtil } from '@eggjs/tegg-common-util'; import { EggPrototype, LoadUnit, @@ -54,6 +54,9 @@ export class Runner { }], moduleConfig: [], }; + RuntimeConfigUtil.setRuntimeConfig({ + baseDir: cwd, + }); for (const reference of this.moduleReferences) { const absoluteRef = { From 2ebd99258db83f554e10a8febd75b7d04b0da5a5 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 23 Aug 2023 21:22:06 +0800 Subject: [PATCH 2/6] f --- core/common-util/index.ts | 2 +- core/common-util/src/RuntimeConfig.ts | 18 ++++++++ core/common-util/src/RuntimeConfigUtil.ts | 41 ------------------- core/tegg/index.ts | 2 +- standalone/standalone/src/Runner.ts | 13 ++++-- .../standalone/src/StandaloneLoadUnit.ts | 1 - .../test/fixtures/inner-object/foo.ts | 1 + standalone/standalone/test/index.test.ts | 8 ++++ 8 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 core/common-util/src/RuntimeConfig.ts delete mode 100644 core/common-util/src/RuntimeConfigUtil.ts diff --git a/core/common-util/index.ts b/core/common-util/index.ts index 80425f08..cecc82d1 100644 --- a/core/common-util/index.ts +++ b/core/common-util/index.ts @@ -7,4 +7,4 @@ export * from './src/StackUtil'; export * from './src/ProxyUtil'; export * from './src/ModuleConfig'; export * from './src/TimerUtil'; -export * from './src/RuntimeConfigUtil'; +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/common-util/src/RuntimeConfigUtil.ts b/core/common-util/src/RuntimeConfigUtil.ts deleted file mode 100644 index 6a3353d7..00000000 --- a/core/common-util/src/RuntimeConfigUtil.ts +++ /dev/null @@ -1,41 +0,0 @@ -import is from 'is-type-of'; -export type EnvType = 'local' | 'unittest' | 'prod' | string; - -export interface RuntimeConfig { - /** - * Application name - */ - name: string; - - /** - * Application environment - */ - env: EnvType; - - /** - * Application directory - */ - baseDir: string; -} - -export class RuntimeConfigUtil { - static #config: Map; - - static setRuntimeConfig(config: Partial) { - for (const key of Object.keys(config) as Array) { - const val = config[key]; - if (is.nullable(val)) continue; - if (!is.string(val)) throw new Error(`The value of ${key} must be a string`); - this.#config.set(key, val); - } - } - - static getRuntimeConfig(): RuntimeConfig { - return { - name: this.#config.get('name')!, - env: this.#config.get('env') as EnvType, - baseDir: this.#config.get('baseDir')!, - }; - } - -} diff --git a/core/tegg/index.ts b/core/tegg/index.ts index 5d8d51a1..e913b9a8 100644 --- a/core/tegg/index.ts +++ b/core/tegg/index.ts @@ -7,4 +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 { RuntimeConfigUtil } from '@eggjs/tegg-common-util'; +export { RuntimeConfig } from '@eggjs/tegg-common-util'; diff --git a/standalone/standalone/src/Runner.ts b/standalone/standalone/src/Runner.ts index 2bd65f93..65738146 100644 --- a/standalone/standalone/src/Runner.ts +++ b/standalone/standalone/src/Runner.ts @@ -1,4 +1,4 @@ -import { ModuleConfigUtil, ModuleReference, RuntimeConfigUtil } from '@eggjs/tegg-common-util'; +import { ModuleConfigUtil, ModuleReference, RuntimeConfig } from '@eggjs/tegg-common-util'; import { EggPrototype, LoadUnit, @@ -54,9 +54,14 @@ export class Runner { }], moduleConfig: [], }; - RuntimeConfigUtil.setRuntimeConfig({ - baseDir: cwd, - }); + + const runtimeConfig: Partial = { + baseDir: this.cwd, + }; + // Inject runtimeConfig + this.innerObjects.runtimeConfig = [{ + obj: runtimeConfig, + }]; for (const reference of this.moduleReferences) { const absoluteRef = { 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/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') }); + }); + }); + }); From db47e9dbad2508edb7018790b8db54559ceaee33 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 23 Aug 2023 21:22:15 +0800 Subject: [PATCH 3/6] f --- .../test/fixtures/runtime-config/foo.ts | 15 +++++++++++++++ .../test/fixtures/runtime-config/package.json | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 standalone/standalone/test/fixtures/runtime-config/foo.ts create mode 100644 standalone/standalone/test/fixtures/runtime-config/package.json 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..81153947 --- /dev/null +++ b/standalone/standalone/test/fixtures/runtime-config/package.json @@ -0,0 +1,6 @@ +{ + "name": "innerobject", + "eggModule": { + "name": "innerobject" + } +} From 1db032811726c6a1c1f2fcd7a572a8d006e76907 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 23 Aug 2023 21:24:09 +0800 Subject: [PATCH 4/6] f --- core/common-util/package.json | 1 - .../test/RuntimeConfigUtil.test.ts | 34 ------------------- .../test/fixtures/runtime-config/package.json | 4 +-- 3 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 core/common-util/test/RuntimeConfigUtil.test.ts diff --git a/core/common-util/package.json b/core/common-util/package.json index c7394eaf..f890c0ad 100644 --- a/core/common-util/package.json +++ b/core/common-util/package.json @@ -37,7 +37,6 @@ }, "dependencies": { "globby": "^11.1.0", - "is-type-of": "^2.0.1", "js-yaml": "^3.14.0" }, "publishConfig": { diff --git a/core/common-util/test/RuntimeConfigUtil.test.ts b/core/common-util/test/RuntimeConfigUtil.test.ts deleted file mode 100644 index ad281ece..00000000 --- a/core/common-util/test/RuntimeConfigUtil.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { RuntimeConfigUtil } from '..'; -import assert from 'assert'; - -describe('test/RuntimeConfigUtil.test.ts', () => { - - it('should work', () => { - RuntimeConfigUtil.setRuntimeConfig({ - baseDir: '/tmp', - env: 'prod', - name: 'test', - }); - assert.deepEqual(RuntimeConfigUtil.getRuntimeConfig(), { - baseDir: '/tmp', - env: 'prod', - name: 'test', - }); - - RuntimeConfigUtil.setRuntimeConfig({ - name: 'test2', - }); - assert.deepEqual(RuntimeConfigUtil.getRuntimeConfig(), { - baseDir: '/tmp', - env: 'prod', - name: 'test2', - }); - }); - - it('should check', () => { - assert.throws(() => { - RuntimeConfigUtil.setRuntimeConfig({ baseDir: 1 as any }); - }, /The value of baseDir must be a string/); - }); - -}); diff --git a/standalone/standalone/test/fixtures/runtime-config/package.json b/standalone/standalone/test/fixtures/runtime-config/package.json index 81153947..53cf3224 100644 --- a/standalone/standalone/test/fixtures/runtime-config/package.json +++ b/standalone/standalone/test/fixtures/runtime-config/package.json @@ -1,6 +1,6 @@ { - "name": "innerobject", + "name": "runtime-config", "eggModule": { - "name": "innerobject" + "name": "runtime-config" } } From 4af9c00ab0bff77e81ecdb888482f79518927df2 Mon Sep 17 00:00:00 2001 From: popomore Date: Thu, 24 Aug 2023 12:23:19 +0800 Subject: [PATCH 5/6] f --- plugin/tegg/app.ts | 6 ------ plugin/tegg/app/extend/application.ts | 13 ++++++++++++- plugin/tegg/test/EggCompatible.test.ts | 7 +++++++ .../modules/multi-module-service/ConfigService.ts | 14 +++++++++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/plugin/tegg/app.ts b/plugin/tegg/app.ts index eece9aba..35bc738a 100644 --- a/plugin/tegg/app.ts +++ b/plugin/tegg/app.ts @@ -2,7 +2,6 @@ import './lib/AppLoadUnit'; import './lib/AppLoadUnitInstance'; import './lib/EggCompatibleObject'; import { Application } from 'egg'; -import { RuntimeConfigUtil } from '@eggjs/tegg-common-util'; import { EggContextCompatibleHook } from './lib/EggContextCompatibleHook'; import { CompatibleUtil } from './lib/CompatibleUtil'; import { ModuleHandler } from './lib/ModuleHandler'; @@ -21,11 +20,6 @@ export default class App { } configWillLoad() { - RuntimeConfigUtil.setRuntimeConfig({ - baseDir: this.app.baseDir, - env: this.app.config.env, - name: this.app.config.name, - }); this.app.config.coreMiddleware.push('teggCtxLifecycleMiddleware'); } diff --git a/plugin/tegg/app/extend/application.ts b/plugin/tegg/app/extend/application.ts index 90d10c41..c33ab12b 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 { 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; + } + } From 7ee310d16ac2a658a1f8e9fc97e2b898b4ce80f1 Mon Sep 17 00:00:00 2001 From: popomore Date: Thu, 24 Aug 2023 12:25:39 +0800 Subject: [PATCH 6/6] f --- plugin/tegg/app/extend/application.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/tegg/app/extend/application.ts b/plugin/tegg/app/extend/application.ts index c33ab12b..9e4c7667 100644 --- a/plugin/tegg/app/extend/application.ts +++ b/plugin/tegg/app/extend/application.ts @@ -17,7 +17,7 @@ import { } from '@eggjs/tegg-runtime'; import { LoaderFactory } from '@eggjs/tegg-loader'; import { EggProtoImplClass, PrototypeUtil, IdenticalUtil, RuntimeConfig } from '@eggjs/tegg'; -import { Application } from 'egg'; +import type { Application } from 'egg'; export default { // @eggjs/tegg-metadata should not depend by other egg plugins.