Skip to content

Commit 4b8fecb

Browse files
committed
Use env mapping to extend configs
1 parent 89b0f1f commit 4b8fecb

File tree

4 files changed

+89
-42
lines changed

4 files changed

+89
-42
lines changed

lib/configs/default.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ var path = require('path');
66
module.exports = function(options) {
77
options = _.merge(options, {
88
// Debug
9-
'debug': !!process.env.DEBUG,
9+
'debug': false,
1010

1111
// Port for running the webserver
12-
'port': process.env.PORT || 3000,
12+
'port': 3000,
1313

1414
// Root folder
1515
'root': process.cwd(),
@@ -49,6 +49,9 @@ module.exports = function(options) {
4949
'email': data.email
5050
};
5151
},
52+
'events': undefined,
53+
'settings.get': undefined,
54+
'settings.set': undefined
5255
},
5356

5457
// Packages

lib/configs/env.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var _ = require('lodash');
2+
3+
var alias = {
4+
'PORT': 'CODEBOX_PORT'
5+
}
6+
7+
var parseEnv = function (env, parent, prefix) {
8+
var k, v, envVar, parsedEnv;
9+
10+
if (!parent) {
11+
parent = {};
12+
}
13+
14+
for(k in parent) {
15+
v = parent[k];
16+
17+
envVar = prefix? prefix + '_': '';
18+
envVar += k.toUpperCase();
19+
20+
if (_.isObject(v) && !_.isArray(v) && !_.isFunction(v)) {
21+
parseEnv(env, v, envVar);
22+
}
23+
else {
24+
if (envVar in env) {
25+
if (_.isArray(v) && (v.length == 0 || _.isString(v[0]))) {
26+
parent[k] = _.compact(env[envVar].split(","));
27+
} else if (_.isNumber(v)) {
28+
parent[k] = parseInt(env[envVar]);
29+
} else if (_.isBoolean(v)) {
30+
parent[k] = (env[envVar] == "false"? false : true);
31+
} else {
32+
parent[k] = env[envVar]
33+
}
34+
}
35+
}
36+
}
37+
38+
return parent;
39+
};
40+
41+
42+
// Extend configuration with environment variables
43+
module.exports = function(options) {
44+
var env = _.clone(process.env);
45+
_.each(alias, function(to, from) {
46+
env[to] = env[from] || env[to];
47+
})
48+
49+
return parseEnv(env, options, 'CODEBOX');
50+
};

lib/configs/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ var Q = require('q');
33

44
var TEMPLATES = {
55
'default': require('./default'),
6-
'local': require('./local')
6+
'local': require('./local'),
7+
'env': require('./env')
78
};
89

910
// Generate a complete config from templates
1011
module.exports = function(options) {
11-
var templates = _.unique(["default"].concat((options.templates || "local").split(",")));
12+
var templates = _.unique(["default"].concat((options.templates || "local,env").split(",")));
1213

1314
return _.reduce(templates, function(prev, template) {
1415
return prev.then(function(_options) {

lib/configs/local.js

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,44 @@ var wrench = require('wrench');
66

77
var logger = require("../utils/logger")("local");
88

9-
var LOCAL_SETTINGS_DIR = process.env.WORKSPACE_CODEBOX_DIR || path.join(
10-
process.env.HOME,
11-
'.codebox'
12-
);
13-
9+
var LOCAL_SETTINGS_DIR = process.env.CODEBOX_LOCAL_FOLDER || path.join(process.env.HOME,'.codebox')
1410
var SETTINGS_FILE = path.join(LOCAL_SETTINGS_DIR, 'settings.json');
1511

1612
// Base structure for a local workspace
1713
// Store the workspace configuration in a file, ...
1814
module.exports = function(options) {
19-
options = _.defaults(options, {
20-
21-
});
22-
23-
options.hooks = _.defaults(options.hooks, {
24-
'settings.get': function(args) {
25-
return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8")
26-
.then(JSON.parse)
27-
.fail(_.constant({}))
28-
.then(function(config) {
29-
if (!config[options.id]) config[options.id] = {};
30-
return config[options.id][args.user] || {};
31-
});
15+
options = _.merge(options, {
16+
'hooks': {
17+
'settings.get': function(args) {
18+
return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8")
19+
.then(JSON.parse)
20+
.fail(_.constant({}))
21+
.then(function(config) {
22+
if (!config[options.id]) config[options.id] = {};
23+
return config[options.id][args.user] || {};
24+
});
25+
},
26+
27+
'settings.set': function(args) {
28+
return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8")
29+
.then(JSON.parse)
30+
.fail(_.constant({}))
31+
.then(function(config) {
32+
if (!config[options.id]) config[options.id] = {};
33+
config[options.id][args.user] = args.settings;
34+
35+
return Q.nfcall(fs.writeFile, SETTINGS_FILE, JSON.stringify(config))
36+
.thenResolve(config);
37+
})
38+
.then(function(config) {
39+
return config[options.id][args.user] || {};
40+
});
41+
}
3242
},
33-
34-
'settings.set': function(args) {
35-
return Q.nfcall(fs.readFile, SETTINGS_FILE, "utf-8")
36-
.then(JSON.parse)
37-
.fail(_.constant({}))
38-
.then(function(config) {
39-
if (!config[options.id]) config[options.id] = {};
40-
config[options.id][args.user] = args.settings;
41-
42-
return Q.nfcall(fs.writeFile, SETTINGS_FILE, JSON.stringify(config))
43-
.thenResolve(config);
44-
})
45-
.then(function(config) {
46-
return config[options.id][args.user] || {};
47-
});
43+
packages:{
44+
'root': path.resolve(LOCAL_SETTINGS_DIR, 'packages')
4845
}
49-
});
50-
51-
options.packages = _.defaults(options.packages, {
52-
'root': process.env.WORKSPACE_ADDONS_DIR || path.resolve(LOCAL_SETTINGS_DIR, 'packages')
53-
});
46+
}, _.defaults);
5447

5548
// Create .codebox folder
5649
logger.log("Creating", LOCAL_SETTINGS_DIR);

0 commit comments

Comments
 (0)