@golavr/ng-config v0.1.0
ng-config
Configuration library for Angular handling environments in continuous delivery systems.
Why using ng-config?
Angular comes with built-in mechanism of environment files but this mechanism requires you to build for each environment. This approach doesn't fit to modern development paradigm which advocate for build once - deploy anywhere.
Getting started
Installation
npm install @golavr/ng-config --saveCreate files
- Create
assets/configsfolder. - Create
assets/configs/env.jsonfile. - Create
assets/configs/config.jsonfile. - Create
assets/configs/config.prod.jsonfile. - Add more
assets/configs/config.$$$.jsonfiles to support all environments.
You should have this folder structure:
- assets
- configs
- config.json
- config.prod.json
- env.jsonNote: Library depends on the above folder and file names.
env.json file
Open assets/configs/env.json and add this content:
{
"env": "prod"
}In continues delivery system you should edit the env value to target the selected environment.
Note: you can replace
prodwith other environment$$$as long as you haveassets/configs/config.$$$.jsonfile for it.
How to use?
Lets set two configuration values for demonstration purpose.
Open assets/configs/config.json file and add:
{
"baseValue": "other configurations will inherit this value",
"overrideValue": "base"
}Open assets/configs/config.prod.json file and add:
{
"overrideValue": "prod"
}Create interface reflecting the configuration:
export interface MyConfig {
baseValue: string;
overrideValue: string;
}Inject it and use it:
import { Component } from '@angular/core';
import { ConfigService } from 'ng-config';
import { MyConfig } from './my-config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(config: ConfigService) {
config.get<MyConfig>().subscribe(conf => console.log(conf.overrideValue));
}
}How does it work?
Each request will read config values during the call, so changes can be made to config files while the app is running.
The returned config object will be composed from config.json and will be overridden with env config.
In continues delivery system you need to edit env.json and set which env config (config.prod.json) will be selected at runtime.
You can have multiple env config files like config.integration.json etc.
6 years ago