Skip to content

Commit 13084da

Browse files
author
Umed Khudoiberdiev
committed
added configuration loading from file
1 parent 69639ce commit 13084da

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@
4747
"typeorm": "0.0.9",
4848
"typescript": "^2.2.1",
4949
"winston": "^2.3.1"
50+
},
51+
"dependencies": {
52+
"app-root-path": "^2.0.1"
5053
}
5154
}

src/Microframework.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export class Microframework {
1818
*/
1919
private frameworkConfig?: MicroframeworkConfig;
2020

21+
/**
22+
* Stores configurations from all configuration files provided to microframework.
23+
*/
24+
private allConfiguration: any = {};
25+
2126
/**
2227
* Stores all registered microframework loaders.
2328
*/
@@ -36,8 +41,23 @@ export class Microframework {
3641
/**
3742
* Configs microframework.
3843
*/
39-
config(config: MicroframeworkConfig): this {
40-
this.frameworkConfig = config;
44+
config(config: MicroframeworkConfig|string|string[]): this {
45+
46+
const appRootDir = require("app-root-path").path;
47+
if (config instanceof String) {
48+
this.allConfiguration = require(appRootDir + "/" + config + ".json") || {};
49+
if (this.allConfiguration.microframework)
50+
this.frameworkConfig = this.allConfiguration.microframework;
51+
52+
} else if (config instanceof Array) { // string[]
53+
if (config.length > 0) {
54+
this.allConfiguration = {};
55+
Object.assign(this.allConfiguration, ...config.map(conf => require(appRootDir + "/" + conf + ".json") || {}));
56+
}
57+
} else {
58+
this.frameworkConfig = config;
59+
}
60+
4161
return this;
4262
}
4363

@@ -77,7 +97,7 @@ export class Microframework {
7797
* Bootstraps microframework and loads all loaders.
7898
*/
7999
bootstrap(): Promise<this> {
80-
const settings = new MicroframeworkSettings();
100+
const settings = new MicroframeworkSettings(this.allConfiguration);
81101
const bootstrapTime = +new Date();
82102

83103
return this.generateLogo()

src/MicroframeworkSettings.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export class MicroframeworkSettings {
2121
*/
2222
private shutdownHandlers: ShutdownHandler[] = [];
2323

24+
// -------------------------------------------------------------------------
25+
// Constructor
26+
// -------------------------------------------------------------------------
27+
28+
constructor(private config: any) {
29+
}
30+
2431
// -------------------------------------------------------------------------
2532
// Public Methods
2633
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)