Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement RuntimeConfig #144

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') });
});
});

});
Loading