forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
174 lines (148 loc) · 5.89 KB
/
config.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
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
174
const uglifycss = require('uglifycss');
const escape = require('html-entities').AllHtmlEntities;
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const getConfig = () => {
let config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config', 'settings.json'), 'utf8'));
const localConfigFilePath = path.join(__dirname, '../config', 'settings-local.json');
// Check for local config file and merge with base settings
if(fs.existsSync(localConfigFilePath)){
const localConfigFile = JSON.parse(fs.readFileSync(localConfigFilePath, 'utf8'));
config = Object.assign(config, localConfigFile);
}
// Override from env.yaml environment file
Object.keys(config).forEach((configKey) => {
if(process.env[configKey]){
config[configKey] = process.env[configKey];
}
});
config.customCss = typeof config.customCss !== 'undefined' ? escape.decode(config.customCss) : null;
config.footerHtml = typeof config.footerHtml !== 'undefined' ? escape.decode(config.footerHtml) : null;
config.googleAnalytics = typeof config.googleAnalytics !== 'undefined' ? escape.decode(config.googleAnalytics) : null;
// setup theme
config.themeViews = '';
if(typeof config.theme === 'undefined' || config.theme === ''){
config.theme = 'Cloth'; // Default to Cloth theme
}
config.themeViews = `../views/themes/${config.theme}/`;
// set the environment for files
config.env = '.min';
if(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === undefined){
config.env = '';
}
// load modules
try{
config.modules.loaded = {};
Object.keys(config.modules.enabled).forEach((mod) => {
config.modules.loaded[mod] = require(`./modules/${config.modules.enabled[mod]}`);
});
}catch(ex){
console.log('Could not load modules, check your config.', ex);
process.exit(1);
}
return config;
};
const getPaymentConfig = (gateway) => {
const siteConfig = getConfig();
// Read the Gateway config
const config = {};
_.forEach(siteConfig.paymentGateway, (gateway) => {
const gateConfigFile = path.join(__dirname, '../config', 'payment', 'config', `${gateway}.json`);
if(fs.existsSync(gateConfigFile)){
config[gateway] = JSON.parse(fs.readFileSync(gateConfigFile, 'utf8'));
}
// Override from env.yaml environment file
Object.keys(config[gateway]).forEach((configKey) => {
if(process.env[gateway] && process.env[gateway][configKey]){
config[gateway][configKey] = process.env[gateway][configKey];
}
});
});
// If Gateway supplied, return that Gateway config
if(gateway && config[gateway]){
return config[gateway];
}
return config;
};
const updateConfig = (fields) => {
const settingsFile = getConfig();
_.forEach(fields, (value, key) => {
settingsFile[key] = value;
if(key === 'customCss_input'){
settingsFile.customCss = escape.encode(uglifycss.processString(value));
}
if(key === 'footerHtml_input'){
const footerHtml = typeof value !== 'undefined' || value === '' ? escape.encode(value) : '';
settingsFile.footerHtml = footerHtml;
}
if(key === 'googleAnalytics_input'){
const googleAnalytics = typeof value !== 'undefined' ? escape.encode(value) : '';
settingsFile.googleAnalytics = googleAnalytics;
}
});
// delete any settings
delete settingsFile.customCss_input;
delete settingsFile.footerHtml_input;
delete settingsFile.googleAnalytics_input;
if(fields.emailSecure === 'on'){
settingsFile.emailSecure = true;
}else{
settingsFile.emailSecure = false;
}
if(fields.emailPort){
settingsFile.emailPort = parseInt(fields.emailPort);
}
if(fields.productsPerRow){
settingsFile.productsPerRow = parseInt(fields.productsPerRow);
}
if(fields.productsPerPage){
settingsFile.productsPerPage = parseInt(fields.productsPerPage);
}
// If we have a local settings file (not git tracked) we loop its settings and save
// and changes made to them. All other settings get updated to the base settings file.
const localSettingsFile = path.join(__dirname, '../config', 'settings-local.json');
if(fs.existsSync(localSettingsFile)){
const localSettings = JSON.parse(fs.readFileSync(localSettingsFile));
_.forEach(localSettings, (value, key) => {
if(fields[key]){
localSettings[key] = fields[key];
// Exists in local so remove from main settings file
delete settingsFile[key];
}
});
// Save our local settings
try{
fs.writeFileSync(localSettingsFile, JSON.stringify(localSettings, null, 4));
}catch(exception){
console.log('Failed to save local settings file', exception);
}
}
// write base settings file
const baseSettingsFile = path.join(__dirname, '../config', 'settings.json');
try{
fs.writeFileSync(baseSettingsFile, JSON.stringify(settingsFile, null, 4));
return true;
}catch(exception){
return false;
}
};
const updateConfigLocal = (field) => {
const localSettingsFile = path.join(__dirname, '../config', 'settings-local.json');
try{
let localSettings = {};
if(fs.existsSync(localSettingsFile)){
localSettings = JSON.parse(fs.readFileSync(localSettingsFile));
}
Object.assign(localSettings, field);
fs.writeFileSync(localSettingsFile, JSON.stringify(localSettings, null, 4));
}catch(exception){
console.log('Failed to save local settings file', exception);
}
};
module.exports = {
getConfig,
getPaymentConfig,
updateConfig,
updateConfigLocal
};