Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / environmentManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*****************************************
 *   Environment Manager
 *****************************************
 *   Author:  mikael.lofjard@gmail.com
 *   Website: http://lofjard.se
 *   License: MIT License
 ****************************************/
  
var EnvironmentManager = (function() {
  
  var fs = require('fs');
    
  var misc = require('./misc').Misc;
  
  var currentEnvironment = null; // development or production
  var trackingCodeScript = null;
    
  return {
      
    init: function(trackingCodePath) {
      var cwdSplit = process.cwd().split('/');
      var cwdBase = cwdSplit[cwdSplit.length - 1];
      console.log('EnvironmentManager: Base dir is ' + cwdBase);
        
      if (cwdBase === 'dev') {
        currentEnvironment = 'development';
      } else {
        currentEnvironment = 'production';
        trackingCodeScript = fs.readFileSync(trackingCodePath);
          
        process.on('uncaughtException', function(err) {
          var log = fs.createWriteStream('exceptions.log', {'flags': 'a'});
          log.write(misc.getDateTimeNow() + ' - ' + err + '\n');
        });
      }
    },
      
    info: function(logMessage) {
      if (currentEnvironment === 'development') {
        console.log(misc.getDateTimeNow() + ' ' + logMessage);
      }
      return;
    },
      
    trackingCode: function() {
      if (currentEnvironment === 'production') {
        return trackingCodeScript;
      }
      return '';
    },
          
        inProduction: function() {
            return (currentEnvironment === 'production');
        },
      
    httpPort: function() {
      return (currentEnvironment === 'development') ? 8080 : 8080;
    },
      
    httpsPort: function() {
      return (currentEnvironment === 'development') ? 8081 : 8081;
    }
  };
  
}());
  
typeof(exports) != 'undefined' ? exports.EnvironmentManager = EnvironmentManager : null;