Skip to content

Commit

Permalink
fix: deep merge config (#274)
Browse files Browse the repository at this point in the history
* fix: deep merge config

* chore: format code
  • Loading branch information
JerrysShan authored Mar 21, 2024
1 parent a677bd6 commit 41cab61
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ArtusInjectEnum } from './constant';
import { ArtusStdError } from './exception';
import { HookFunction, LifecycleManager } from './lifecycle';
import { LoaderFactory, Manifest } from './loader';
import { mergeConfig } from './loader/utils/merge';
import { Application, ApplicationInitOptions } from './types';
import ConfigurationHandler from './configuration';
import { Logger, LoggerType } from './logger';
Expand Down Expand Up @@ -114,7 +115,7 @@ export class ArtusApplication implements Application {
const newConfig = this.configurationHandler.getMergedConfig() ?? {};
this.container.set({
id: ArtusInjectEnum.Config,
value: Object.assign(oldConfig, newConfig),
value: mergeConfig(oldConfig, newConfig),
});
}
}
9 changes: 9 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ describe("test/config.test.ts", () => {
process.env[ARTUS_SERVER_ENV] = undefined;
});
});


describe("app with manifest should load config ok", () => {
it("should load config ok", async () => {
const { main } = require("./fixtures/app_with_manifest/bootstrap");
const app = await main();
expect(app.config).toEqual({ httpConfig: { key1: 'value1', port: 3000 }, plugin: {} });
});
});
});
29 changes: 29 additions & 0 deletions test/fixtures/app_with_manifest/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from "path";
// import fs from 'fs';
import 'reflect-metadata';
import { ArtusApplication } from "../../../src";

const rootDir = path.resolve(__dirname, "./");

async function main() {
const app = new ArtusApplication();
const metaFilePath = path.resolve(rootDir, 'manifest.json');
// let manifest;
// if (fs.existsSync(metaFilePath)) {
const manifest = require((metaFilePath));
// } else {
// const scanner = new ArtusScanner({
// configDir: 'config',
// needWriteFile: true,
// useRelativePath: true,
// extensions: ['.ts', '.js', '.json'],
// app,
// });
// manifest = await scanner.scan(rootDir);
// }
await app.load(manifest, rootDir);
await app.run();
return app;

}
export { main };
6 changes: 6 additions & 0 deletions test/fixtures/app_with_manifest/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export default {
httpConfig: {
port: 3000,
},
};
8 changes: 8 additions & 0 deletions test/fixtures/app_with_manifest/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '../../../../src/';

@Injectable()
export default class DemoController {
public async index() {
return { code: 0, message: 'ok', data: {} };
}
}
13 changes: 13 additions & 0 deletions test/fixtures/app_with_manifest/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LifecycleHookUnit, ApplicationLifecycle, LifecycleHook, Inject, ArtusApplication, ArtusInjectEnum } from '../../../src';

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
private app: ArtusApplication;

@LifecycleHook()
public async configWillLoad() {
this.app.config.httpConfig = this.app.config.httpConfig ?? {};
this.app.config.httpConfig.key1 = 'value1';
}
}
49 changes: 49 additions & 0 deletions test/fixtures/app_with_manifest/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"version": "2",
"refMap": {
"_app": {
"relativedPath": "",
"pluginConfig": {},
"items": [
{
"path": "config/config.default",
"extname": ".ts",
"filename": "config.default.ts",
"loader": "config",
"source": "app",
"unitName": "_app",
"loaderState": {
"exportNames": []
}
},
{
"path": "controller/home",
"extname": ".ts",
"filename": "home.ts",
"loader": "module",
"source": "app",
"unitName": "_app",
"loaderState": {
"exportNames": [
"default"
]
}
},
{
"path": "lifecycle",
"extname": ".ts",
"filename": "lifecycle.ts",
"loader": "lifecycle-hook-unit",
"source": "app",
"unitName": "_app",
"loaderState": {
"exportNames": [
"default"
]
}
}
]
}
},
"extraPluginConfig": {}
}

0 comments on commit 41cab61

Please sign in to comment.