-
Notifications
You must be signed in to change notification settings - Fork 2
/
config-manager.js
261 lines (220 loc) · 7.08 KB
/
config-manager.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict'
/** Modules */
const fs = require('fs');
const chalk = require('chalk');
const Constants = require('./constants');
/** Internal modules */
const Log = require('./logger');
const Profile = require('./profile');
/**
* Class Config Manager
*
* Represents a manager class to perform Operations on Profiles || Configurations
*/
class ConfigManager {
constructor(config) {
this.activeConfig = '';
this.profiles = [];
this.profileFactory(config);
}
/**
* Creates profiles and sets up their configuration objects for ConfigManager
*
* @param {object} Configuration object from the initialization of Davos
*/
profileFactory(config) {
if (this.hasRequiredProperties(config)) {
this.profiles.push(new Profile(config, true, 'default'));
} else {
this.setActiveConfig();
const configFile = this.getConfigFile();
if (!configFile) {
/** create empty configuration */
Log.warn('No configuration file present, creating a virtual configuration.');
Log.warn('Limited functionality will be available.');
this.profiles.push(new Profile({}, true, 'default'));
} else {
/** if array, then it is davos.json */
if (Array.isArray(configFile)) {
configFile.forEach((profile) => {
const nonVitalConfigProperties = this.getNonVitalConfigProperties(profile.config);
const newProfile = new Profile(profile.config, profile.active, profile.profile);
this.addNonVitalConfigPropertiesToProfile(newProfile, nonVitalConfigProperties);
if (Object.keys(config).length) {
this.addNonVitalConfigPropertiesToProfile(newProfile, config);
}
this.profiles.push(newProfile);
});
} else {
const nonVitalConfigProperties = this.getNonVitalConfigProperties(configFile);
const newProfile = new Profile(configFile, true, 'default');
this.addNonVitalConfigPropertiesToProfile(newProfile, nonVitalConfigProperties);
if (Object.keys(config).length) {
this.addNonVitalConfigPropertiesToProfile(newProfile, config);
}
this.profiles.push(newProfile);
}
}
}
}
/**
* Checks for the required properties
*
* @param {object} Configuration object from initialization of Davos
*/
hasRequiredProperties(config) {
if (!config) return false;
const { required } = Constants.CONFIG_PROPERTIES;
return Object.keys(config).filter(prop => required.includes(prop)).length === required.length;
}
/**
* Checks for the optional properties
*
* @param {object} Configuration object from initialization of Davos
*/
hasOptionalProperties(config) {
if (!config) return false;
const { optional } = Constants.CONFIG_PROPERTIES;
return Object.keys(config).filter(prop => optional.includes(prop)).length > 0;
}
/**
* Checks if a given property is an required one
*
* @param {string} Property to check
*/
isRequiredProperty(property) {
const { required } = Constants.CONFIG_PROPERTIES;
return required.includes(property);
}
/**
* Checks if a given property is an optional one
*
* @param {string} Property to check
*/
isOptionalProperty(property) {
const { optional } = Constants.CONFIG_PROPERTIES;
return optional.includes(property);
}
/**
* Returns an object, containing non-essential properties.
* These might include:
* [git], [pattern] etc...
*
* @param {object} Configuration object from initialization of Davos
*/
getNonVitalConfigProperties(config) {
const nonVitalConfigProperties = {};
if (!config) {
return nonVitalConfigProperties;
}
Object.keys(config).forEach((key) => {
if (!this.isRequiredProperty(key) && !this.isOptionalProperty(key)) {
nonVitalConfigProperties[key] = config[key];
}
});
return nonVitalConfigProperties;
}
addNonVitalConfigPropertiesToProfile(profile, nonVitalAttributes) {
Object.keys(nonVitalAttributes).forEach(attr => profile.config.SetProperty(attr, nonVitalAttributes[attr]));
}
/**
* Gets the current configuration file
*
* NOTE: this will work, only if there is an existing configuration file (davos.json || dw.json)
*/
getConfigFile() {
const configName = this.getConfigName();
let fileContents = '';
let json = null;
if (configName) {
try {
fileContents = fs.readFileSync(configName, 'UTF-8');
} catch (e) {
Log.error(chalk.red(`\nConfiguration not found. Error: ${configName} ::: ${e.message}`));
}
try {
json = JSON.parse(fileContents);
} catch (e) {
Log.error(chalk.red(`\nThere was a problem parsing the configuration file : ${configName} ::: ${e.message}`));
}
return json;
}
Log.warn('No configuration file present, please create either davos.json or dw.json');
return null;
}
/**
* Returns all profiles created on initialization or an empty array
*/
getProfiles() {
return this.profiles;
}
/**
* Checks if a certain configuration file exists in the main directory
* davos.json || dw.json
*
* @param {string} Filename
*/
checkConfigFile() {
const configFileName = this.activeConfig || null;
let exists = false;
try {
exists = fs.existsSync(configFileName);
} catch (err) {
Log.error(err);
Log.info(`${configFileName} not present`);
}
return exists;
}
/**
* Sets the active configuration name to the ConfigManager object
* davos.json || dw.json
*
* If none is present and an operation is required to have it, execution will end here.
*/
setActiveConfig() {
this.activeConfig = Constants.DEFAULT_CONFIG_NAME;
let configExists = this.checkConfigFile();
if (!configExists) {
this.activeConfig = Constants.DW_CONFIG_NAME;
configExists = this.checkConfigFile();
}
if (!configExists) {
this.activeConfig = '';
}
}
/**
* Return the current active configuration name
* davos.json || dw.json
*/
getConfigName() {
return this.activeConfig;
}
/**
* Returns the current active profile
* If many are active, the first one will be returned
*/
getActiveProfile() {
const activeProfile = this.profiles.find(x => x.active === true);
if (!activeProfile) {
Log.error(chalk.red('\nThere is no active profile in your configuration.'));
throw new Error('No active profile');
}
return activeProfile;
}
getProfile(name) {
return this.profiles.find(prof => prof.profile === name);
}
/**
* Gets the configuration from the current active profile
*/
getActiveConfig() {
return this.getActiveProfile().config;
}
/** Saves a json configuration to the current active configuration file */
saveConfiguration(json) {
const configFileName = this.getConfigName() || Constants.DEFAULT_CONFIG_NAME;
fs.writeFileSync(configFileName, JSON.stringify(json, null, ' '), 'UTF-8');
Log.info(chalk.cyan(`\n Configuration saved in ${configFileName}`));
}
}
module.exports = ConfigManager;