Created
September 11, 2017 12:04
-
-
Save masimplo/c6dbedf7acd16d201c0405c36bb5e5ee to your computer and use it in GitHub Desktop.
Using environments in Ionic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EnvironmentsModule } from '../environment/environment.module'; | |
@NgModule({ | |
... | |
imports: [ | |
... | |
EnvironmentsModule, | |
... | |
] | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IEnvironmentalVariables } from './env.variables'; | |
export const devConfig: IEnvironmentalVariables = { | |
apiUrl: 'https://...', | |
googleOAuth: '6047....apps.googleusercontent.com', | |
kmsApiKey: '7a...1e', | |
logToConsole: true | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IEnvironmentalVariables } from './env.variables'; | |
export const productionConfig: IEnvironmentalVariables = { | |
apiUrl: 'https://...', | |
googleOAuth: '6047....apps.googleusercontent.com', | |
kmsApiKey: '7a...1e', | |
logToConsole: true | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IEnvironmentalVariables } from './env.variables'; | |
export const uatConfig: IEnvironmentalVariables = { | |
apiUrl: 'https://...', | |
googleOAuth: '6047....apps.googleusercontent.com', | |
kmsApiKey: '7a...1e', | |
logToConsole: true | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface IEnvironmentalVariables { | |
apiUrl: string; | |
googleOAuth: string; | |
kmsApiKey: string; | |
logToConsole: boolean; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OpaqueToken } from '@angular/core'; | |
// tslint:disable-next-line:variable-name | |
export const Environment = new OpaqueToken('environment'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
constructor(@Inject(Environment) private _env: IEnvironmentalVariables){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'