Add setting environment variables using commandline arguments.

This commit is contained in:
Spencer Ofwiti 2021-03-17 16:39:33 +03:00
parent 12685b32f4
commit 68ff291dd2
4 changed files with 58 additions and 8 deletions

View File

@ -3,7 +3,9 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"config": "node set-env.js",
"start": "ng serve",
"start-dev": "ng serve -o",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",

52
set-env.js Normal file
View File

@ -0,0 +1,52 @@
const fs = require('fs');
const devEnvironment = `
import {NgxLoggerLevel} from 'ngx-logger';
export const environment = {
production: false,
bloxbergChainId: ${process.env.CHAIN_ID || 8996},
level: NgxLoggerLevel.TRACE,
serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: ${process.env.LOGGING_URL || 'http://localhost:8000'},
cicMetaUrl: ${process.env.META_URL || 'https://meta.dev.grassrootseconomics.net'},
publicKeysUrl: ${process.env.KEYS_URL || 'http://localhost:8000/keys.asc'},
cicCacheUrl: ${process.env.META_URL || 'https://cache.dev.grassrootseconomics.net'},
web3Provider: ${process.env.WEB3_PROVIDER || 'ws://localhost:63546'},
cicUssdUrl: ${process.env.USSD_URL || 'https://ussd.dev.grassrootseconomics.net'},
registryAddress: ${process.env.REGISTRY_ADDRESS || '0x6Ca3cB14aA6F761712E1C18646AfBA4d5Ae249E8'},
trustedDeclaratorAddress: ${process.env.TRUSTED_ADDRESS || '0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C'}
};
`;
const prodEnvironment = `
import {NgxLoggerLevel} from 'ngx-logger';
export const environment = {
production: false,
bloxbergChainId: ${process.env.CHAIN_ID || 8996},
level: NgxLoggerLevel.TRACE,
serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: ${process.env.LOGGING_URL || 'http://localhost:8000'},
cicMetaUrl: ${process.env.META_URL || 'https://meta.grassrootseconomics.net'},
publicKeysUrl: ${process.env.KEYS_URL || 'http://localhost:8000/keys.asc'},
cicCacheUrl: ${process.env.META_URL || 'https://cache.grassrootseconomics.net'},
web3Provider: ${process.env.WEB3_PROVIDER || 'ws://localhost:63546'},
cicUssdUrl: ${process.env.USSD_URL || 'https://ussd.grassrootseconomics.net'},
registryAddress: ${process.env.REGISTRY_ADDRESS || '0x6Ca3cB14aA6F761712E1C18646AfBA4d5Ae249E8'},
trustedDeclaratorAddress: ${process.env.TRUSTED_ADDRESS || '0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C'}
};
`;
function setConfigs(configs, envFile) {
fs.writeFile(`./src/environments/` + envFile, configs, function (err) {
if (err) {
throw console.error(err);
} else {
console.log(`Angular ${envFile} file generated`);
}
});
}
setConfigs(devEnvironment, 'environment.ts');
setConfigs(prodEnvironment, 'environment.prod.ts');

View File

@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import {Injectable, isDevMode} from '@angular/core';
import {
HttpRequest,
HttpHandler,
@ -8,7 +8,6 @@ import {
import {Observable, throwError} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {ErrorDialogService} from '@app/_services';
import {environment} from '@src/environments/environment';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
@ -17,7 +16,7 @@ export class ErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(catchError((err: HttpErrorResponse) => {
if (!environment.production) {
if (isDevMode()) {
this.errorDialogService.openDialog({
message: err.error.message || err.statusText || 'Unknown Error',
status: err.status || 0

View File

@ -1,6 +1,5 @@
import {Injectable} from '@angular/core';
import {Injectable, isDevMode} from '@angular/core';
import {NGXLogger} from 'ngx-logger';
import {environment} from '@src/environments/environment';
@Injectable({
providedIn: 'root'
@ -11,9 +10,7 @@ export class LoggingService {
constructor(private logger: NGXLogger) {
// TRACE|DEBUG|INFO|LOG|WARN|ERROR|FATAL|OFF
this.env = environment.production ? 'Production' : 'Development';
if (this.env === 'Development') {
if (isDevMode()) {
this.sendInfoLevelMessage('Dropping into debug mode');
}
}