Skip to content

Commit

Permalink
feat: implement RuntimeConfig (#144)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

RuntimeConfig can be injected in tegg and standalone

It contains

- baseDir
- env
- name


##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->
  • Loading branch information
popomore authored Aug 24, 2023
1 parent 6638967 commit 0862655
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/common-util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './src/StackUtil';
export * from './src/ProxyUtil';
export * from './src/ModuleConfig';
export * from './src/TimerUtil';
export * from './src/RuntimeConfig';
18 changes: 18 additions & 0 deletions core/common-util/src/RuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions core/tegg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
13 changes: 12 additions & 1 deletion plugin/tegg/app/extend/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions plugin/tegg/test/EggCompatible.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand All @@ -22,4 +25,9 @@ export default class ConfigService {
async getCurrentUserName(): Promise<string> {
return this.user.userName;
}

getRuntimeConfig(): RuntimeConfig {
return this.runtimeConfig;
}

}
10 changes: 9 additions & 1 deletion standalone/standalone/src/Runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModuleConfigUtil, ModuleReference } from '@eggjs/tegg-common-util';
import { ModuleConfigUtil, ModuleReference, RuntimeConfig } from '@eggjs/tegg-common-util';
import {
EggPrototype,
LoadUnit,
Expand Down Expand Up @@ -55,6 +55,14 @@ export class Runner {
moduleConfig: [],
};

const runtimeConfig: Partial<RuntimeConfig> = {
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),
Expand Down
1 change: 0 additions & 1 deletion standalone/standalone/src/StandaloneLoadUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class StandaloneLoadUnit implements LoadUnit {
}
}


containPrototype(proto: EggPrototype): boolean {
return !!(this.protoMap.get(proto.name)?.find(t => t === proto));
}
Expand Down
1 change: 1 addition & 0 deletions standalone/standalone/test/fixtures/inner-object/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export class Foo implements MainRunner<string> {
async main(): Promise<string> {
return this.hello.hello();
}

}
15 changes: 15 additions & 0 deletions standalone/standalone/test/fixtures/runtime-config/foo.ts
Original file line number Diff line number Diff line change
@@ -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<RuntimeConfig> {
@Inject()
runtimeConfig: RuntimeConfig;

async main(): Promise<RuntimeConfig> {
return this.runtimeConfig;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "runtime-config",
"eggModule": {
"name": "runtime-config"
}
}
8 changes: 8 additions & 0 deletions standalone/standalone/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') });
});
});

});

0 comments on commit 0862655

Please sign in to comment.