Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import { Injectable, Logger } from '@nestjs/common';
  2. import * as dotenv from 'dotenv';
  3. import * as fs from 'fs';
  4. dotenv.config();
  5.  
  6. @Injectable()
  7. export class ConfigService {
  8. private readonly envConfig: { [key: string]: string };
  9.  
  10. constructor(filePath: string) {
  11. try {
  12. this.envConfig = dotenv.parse(fs.readFileSync(filePath));
  13. } catch (e) {
  14. Logger.error(`File ${filePath} not found, app will use process.env`);
  15. }
  16. }
  17.  
  18. get(key: string): string {
  19. if (this.envConfig) return this.envConfig[key];
  20. return process.env[key];
  21. }
  22.  
  23. getInt(key: string): number {
  24. if (this.envConfig) return parseInt(this.envConfig[key], 10);
  25. return parseInt(process.env[key], 10);
  26. }
  27.  
  28. getBoolean(key: string): boolean {
  29. if (this.envConfig) return this.envConfig[key] === 'true';
  30. return process.env[key] === 'true';
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement