Skip to content

Instantly share code, notes, and snippets.

@masimplo
Created September 11, 2017 12:04
Show Gist options
  • Save masimplo/c6dbedf7acd16d201c0405c36bb5e5ee to your computer and use it in GitHub Desktop.
Save masimplo/c6dbedf7acd16d201c0405c36bb5e5ee to your computer and use it in GitHub Desktop.
Using environments in Ionic
import { EnvironmentsModule } from '../environment/environment.module';
@NgModule({
...
imports: [
...
EnvironmentsModule,
...
]
...
}
import { IEnvironmentalVariables } from './env.variables';
export const devConfig: IEnvironmentalVariables = {
apiUrl: 'https://...',
googleOAuth: '6047....apps.googleusercontent.com',
kmsApiKey: '7a...1e',
logToConsole: true
};
import { IEnvironmentalVariables } from './env.variables';
export const productionConfig: IEnvironmentalVariables = {
apiUrl: 'https://...',
googleOAuth: '6047....apps.googleusercontent.com',
kmsApiKey: '7a...1e',
logToConsole: true
};
import { IEnvironmentalVariables } from './env.variables';
export const uatConfig: IEnvironmentalVariables = {
apiUrl: 'https://...',
googleOAuth: '6047....apps.googleusercontent.com',
kmsApiKey: '7a...1e',
logToConsole: true
};
export interface IEnvironmentalVariables {
apiUrl: string;
googleOAuth: string;
kmsApiKey: string;
logToConsole: boolean;
}
import { NgModule } from '@angular/core';
import { Environment } from './environment.token';
import { uatConfig } from './env.uat';
import { productionConfig } from './env.production';
import { developConfig } from './env.develop';
export function environmentFactory() {
switch (process.env.NODE_ENV) {
case 'uat':
return uatConfig;
case 'dev':
return developConfig;
case 'prod':
return productionConfig;
default:
throw new Error('Enviroment not set');
}
}
@NgModule({
providers: [
{
provide: Environment,
useFactory: environmentFactory
}
]
})
export class EnvironmentsModule { }
import { OpaqueToken } from '@angular/core';
// tslint:disable-next-line:variable-name
export const Environment = new OpaqueToken('environment');
class MyClass {
constructor(@Inject(Environment) private _env: IEnvironmentalVariables){
}
}
@masimplo
Copy link
Author

To set the enviroment you then need to run ionic commands having set NODE_ENV like:
NODE_ENV=uat bash -c 'ionic cordova build android --prod --release'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment