forked from botwillacceptanything/botwillacceptanything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.loader.js
67 lines (57 loc) · 1.78 KB
/
config.loader.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
(function () {
'use strict';
var define = require('amdefine')(module);
var fs = require('fs');
var fse = require('fs-extra');
var deps = [
'./configs/custom.js',
'lodash',
'./configs/template.js',
];
var err;
// If configs/custom.js exists, load it immediately. We don't need to
// restructure the configs.
if (fs.existsSync('./configs/custom.js') === true) {
if (fs.existsSync('./config.js') === false) { updateLoader(); }
return prepareConfig();
}
// If config.js doesn't exist right now, copy the template.
if (fs.existsSync('./config.js') === false) {
fse.copySync('./configs/template.js', './configs/custom.js');
} else {
// Otherwise, move the current config into the correct location.
err = fs.renameSync('./config.js', './configs/custom.js');
// If an error occurred, load the original config.js.
if (err) {
console.error(err);
console.error(err.stack);
deps[0] = './config.js';
return prepareConfig();
}
}
// Update the loader and then prepare the config for merging.
updateLoader();
prepareConfig();
function updateLoader() {
// Copy this loader into config.js for backwards compatibility.
err = fse.copySync(__filename, './config.js');
if (err) {
console.error(err);
console.error(err.stack);
}
}
function prepareConfig() {
var exists = fs.existsSync('./configs/' + process.env.BUILD_ENVIRONMENT + '.js');
if (exists === true) {
deps.push('./configs/' + process.env.BUILD_ENVIRONMENT + '.js');
}
mergeConfig();
}
function mergeConfig() {
define(deps, function (config, _, template, env) {
if (typeof env === 'undefined') { env = {}; }
_.merge(template, config, env);
module.exports = template;
});
}
}());