Skip to content

Commit e955c62

Browse files
authored
Merge pull request #1026 from mgechev/env-config
feat(config): add app environment specific config
2 parents 589eadc + 2fba3c7 commit e955c62

File tree

7 files changed

+54
-5
lines changed

7 files changed

+54
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ Configure at runtime
8585
npm start -- --port 8080 --reload-port 4000 --base /my-app/
8686
```
8787

88+
## Environment configuration
89+
90+
If you have different environments and you need to configure them to use different end points, settings, etc. you can use the `./tools/env/config.json` file. The keys in the file are the different environments.
91+
92+
The can be specified by using:
93+
94+
```bash
95+
npm start -- --config-env ENV_NAME
96+
```
97+
98+
Currently the `ENV_NAME`s are `dev`, `prod`, `staging`, but you can simply add different key-value pairs to the `config.json` file in order to alter extra such environments.
99+
88100
# Tools documentation
89101

90102
A documentation of the provided tools can be found in [tools/README.md](tools/README.md).

src/client/app/app.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component } from '@angular/core';
22
import { ROUTER_DIRECTIVES } from '@angular/router';
33
import { HTTP_PROVIDERS } from '@angular/http';
44

5-
import { NameListService, NavbarComponent, ToolbarComponent } from './shared/index';
5+
import { Config, NameListService, NavbarComponent, ToolbarComponent } from './shared/index';
66

77
/**
88
* This class represents the main application component. Within the @Routes annotation is the configuration of the
@@ -15,4 +15,8 @@ import { NameListService, NavbarComponent, ToolbarComponent } from './shared/ind
1515
templateUrl: 'app.component.html',
1616
directives: [ROUTER_DIRECTIVES, NavbarComponent, ToolbarComponent]
1717
})
18-
export class AppComponent {}
18+
export class AppComponent {
19+
constructor() {
20+
console.log('Environment config', Config);
21+
}
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Feel free to extend this interface
2+
// depending on your app specific config.
3+
export interface IConfig {
4+
API: string;
5+
}
6+
7+
export const Config: IConfig = JSON.parse('<%= ENV_CONFIG %>');

src/client/app/shared/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
export * from './name-list/index';
55
export * from './navbar/index';
66
export * from './toolbar/index';
7+
export * from './config/env.config';

tools/config/seed.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class SeedConfig {
7676
* The base path of node modules.
7777
* @type {string}
7878
*/
79-
NPM_BASE = join(this.APP_BASE, '/node_modules/');
79+
NPM_BASE = join(this.APP_BASE, 'node_modules/');
8080

8181
/**
8282
* The flag to include templates into JS app prod file.
@@ -420,7 +420,9 @@ export class SeedConfig {
420420
[`${this.APP_BASE.replace(/\/$/, '')}`]: this.APP_DEST
421421
}
422422
}
423-
}
423+
},
424+
// Note: you can customize the location of the file
425+
'environment-config': require('../env/config.json')
424426
};
425427

426428
/**

tools/env/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"dev": {
3+
"API": "https://demo.com"
4+
},
5+
"prod": {
6+
"API": "https://prod.com"
7+
},
8+
"staging": {
9+
"API": "https://staging.com"
10+
}
11+
}

tools/utils/seed/template_locals.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import { argv } from 'yargs';
12
import * as CONFIG from '../../config';
23

34
/**
45
* Returns the project configuration (consisting of the base configuration provided by see.config.ts and the additional
56
* project specific overrides as defined in project.config.ts)
67
*/
78
export function templateLocals() {
8-
return CONFIG;
9+
const configEnvName = argv['config-env'] || 'dev';
10+
const configEnv = CONFIG.getPluginConfig('environment-config')[configEnvName];
11+
12+
if (!configEnv) {
13+
throw new Error('Invalid configuration name');
14+
}
15+
16+
const config = {
17+
ENV_CONFIG: JSON.stringify(configEnv)
18+
};
19+
20+
return Object.assign(config, CONFIG);
921
}

0 commit comments

Comments
 (0)