-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcfenv-wrapper.js
More file actions
executable file
·173 lines (149 loc) · 4.27 KB
/
cfenv-wrapper.js
File metadata and controls
executable file
·173 lines (149 loc) · 4.27 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright IBM Corp. 2014 All Rights Reserved */
/* Module that provides a simple wrapper to cfenv. If running locally,
* it will try to load env.json/env_custom.json from the working directory and
* pass that info along to cfenv. (If the JSON files are not present, it try to load
* env.log.)
*/
var cfenv = require('cfenv');
var fs = require('fs-sync');
var properties = require ('properties-parser');
module.exports = (function() {
var ENV_LOG_FILE = 'env.log';
var ENV_JSON_FILE = 'env.json';
var ENV_CUSTOM_JSON_FILE = 'env_custom.json';
var appEnv;
var envVars;
// Short utility function to read in JSON
// file.
function getJSONFile(filename) {
var parsedJSON;
if (fs.exists(filename)) {
parsedJSON = fs.readJSON(filename);
}
return parsedJSON;
}
// Short utility function to read env.log file out of
// base working directory
function getEnvLog() {
return properties.read(ENV_LOG_FILE);
}
// Initialize based on whether running locally or in cloud.
function init() {
if (!process.env.VCAP_SERVICES) {
// running locally
// First try to load env.json
if (fs.exists(ENV_JSON_FILE)) {
try {
var envJson = getJSONFile(ENV_JSON_FILE);
if (fs.exists(ENV_CUSTOM_JSON_FILE)) {
try {
envVars = getJSONFile(ENV_CUSTOM_JSON_FILE);
} catch (err) {
// Some kind of problem reading the file or parsing the JSON
console.error('Could not read configuration file ' + ENV_CUSTOM_JSON_FILE + ': ' + err);
}
}
var envOptions = {
// provide values for the VCAP_SERVICES value in
// env.json
vcap: {
services: envJson.VCAP_SERVICES
}
};
appEnv = cfenv.getAppEnv(envOptions);
} catch (err) {
// Some kind of problem reading the file or parsing the JSON
console.error('Could not read configuration file ' + ENV_JSON_FILE + ': ' + err);
}
}
if (!appEnv) {
// If no luck getting env.json, then try env.log
try {
envVars = getEnvLog();
var options = {
// provide values for the VCAP_APPLICATION and VCAP_SERVICES environment
// variables based on parsing values in the env.log file
vcap: {
application: JSON.parse(envVars.VCAP_APPLICATION),
services: JSON.parse(envVars.VCAP_SERVICES)
}
};
appEnv = cfenv.getAppEnv(options);
} catch (err) {
// Some kind of problem reading the file or parsing the JSON
console.warn('Could not read configuration file: ' + err);
}
}
}
if (!appEnv) {
// We're either running in the cloud or env.log could not be
// loaded. So, just let cfenv process VCAP_SERVICES and
// VCAP_APPLICATION for us
appEnv = cfenv.getAppEnv();
}
}
init();
/*
* Expose a getAppEnv function that returns a wrapped cfenv.
*/
return {
getAppEnv: function() {
if (appEnv) {
return {
app: appEnv.app,
services: appEnv.services,
isLocal: appEnv.isLocal,
name: appEnv.name,
port: appEnv.port,
bind: appEnv.bind,
urls: appEnv.urls,
url: appEnv.url,
getServices: function() {
// pass-through to cfenv
return appEnv.getServices();
},
getService: function(name) {
// pass-through to cfenv
return appEnv.getService(name);
},
getServiceURL: function(name, replacements) {
// pass-through to cfenv
return appEnv.getServiceURL(name, replacements);
},
getServiceCreds: function(spec) {
// pass-through to cfenv
return appEnv.getServiceCreds(spec);
},
/* Unlike the others, these functions don't wrapper
* cfenv function. If we're runnning locally, first try to
* get the value(s) from env.log data. Otherwise (or if
* not found), look at process.env.
*/
getEnvVars: function() {
var value;
if (envVars) {
value = envVars;
}
if (!value) {
value = process.env;
}
return value;
},
getEnvVar: function(name) {
var value;
if (envVars) {
value = envVars[name];
}
if (!value) {
value = process.env[name];
}
return value;
}
};
} else {
// Problem getting the environment
return null;
}
}
};
}());