-
Notifications
You must be signed in to change notification settings - Fork 2
/
config-editor.js
267 lines (231 loc) · 7.39 KB
/
config-editor.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
262
263
264
265
266
267
'use strict';
// Constants
const createInsertEdit = [{
name: 'hostname',
description: 'Hostname of your Sandbox (without https:// prefix)',
required: true
}, {
name: 'username',
description: 'Username of your Sandbox',
required: true
}, {
name: 'password',
hidden: true,
description: 'Password of your Sandbox (the input won\'t be visible)',
required: true
}, {
name: 'codeversion',
description: 'Code Version (default is version1)',
default: 'version1'
}, {
name: 'client-id',
description: 'Client ID for the OCAPI configuration)',
default: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
}, {
name: 'client-secret',
description: 'Client secret for the OCAPI configuration',
default: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
},{
name: 'indentSize',
description: 'Space indentation size for meta files (2 or 4)',
default: '2'
}, {
name: 'exclude',
description: 'Exclude uploading folders and files. Separate all excludes by space',
default: '**/node_modules/** **/.git/** **/.svn/** **/.sass-cache/**'
}, {
name: 'templateReplace',
description: 'Replace custom tags with build information inside your templates. You can add more files and tags at config file.',
default: '/path/to/cartrige/template/build_info.isml'
}];
/** Modules */
const prompt = require('prompt');
const chalk = require('chalk');
/** Internal modules */
const Davos = require('../index');
const Log = require('./logger');
const Utils = require('./util');
const CartridgeHelper = require('./cartridge-helper');
/**
* A configuration editor managing users profiles
*/
class ConfigEditor {
/**
* Create a configuration editor
* @param {Object} config The configuration object used by Davos
* @param {Object} ConfigManagerInstance The passed ConfigManagerInstance object or newly created
*/
constructor (config) {
const davos = new Davos();
this.ConfigManager = davos.ConfigManager;
this.config = config;
this.workingDir = Utils.getCurrentRoot();
}
/**
* Create an active configuration profile
*/
createConfig () {
const self = this;
const cartridges = CartridgeHelper.getCartridgesFromDir(self.workingDir);
if (self.ConfigManager.checkConfigFile()) {
Log.info(chalk.yellow('\nConfiguration already exists.'));
return;
}
if (cartridges.length < 1) {
Log.info(chalk.yellow(`No cartridges found in ${self.workingDir} and it's subdirectories.`));
return;
}
prompt.start();
prompt.get(createInsertEdit, (err, result) => {
if (err) {
return Utils.promptError(err);
}
self.ConfigManager.saveConfiguration([
{
active: true,
profile: result.hostname.split('-')[0],
config: {
hostname: result.hostname,
username: result.username,
password: result.password,
codeVersion: result.codeversion,
['client-id']: result['client-id'],
['client-secret']: result['client-secret'],
cartridge: cartridges,
indentSize: result.indentSize,
exclude: result.exclude.split(' '),
templateReplace: {
files: [result.templateReplace],
pattern: {
buildVersion: '@DEPLOYMENT_VERSION@'
}
}
}
}
]);
});
}
/**
* Insert non active profile in the configuration manager
*/
insertProfile () {
const self = this;
prompt.start();
prompt.get(createInsertEdit, (err, result) => {
if (err) {
return Utils.promptError(err);
}
const cartridges = CartridgeHelper.getCartridgesFromDir(self.workingDir);
const profiles = self.ConfigManager.getProfiles();
const len = profiles.length;
const newProfile = result.hostname.split('-')[0];
for (let i = 0; i < len; i++) {
if (profiles[i].profile === newProfile) {
Log.info(chalk.yellow(`\nProfile ${newProfile} exists in your current configuration.`));
return;
}
}
profiles.push({
active: false,
profile: result.hostname.split('-')[0],
config: {
hostname: result.hostname,
username: result.username,
password: result.password,
codeVersion: result.codeversion,
['client-id']: result['client-id'],
['client-secret']: result['client-secret'],
cartridge: cartridges,
indentSize: result.indentSize,
exclude: result.exclude.split(' '),
templateReplace: {
files: [result.templateReplace],
pattern: {
buildVersion: '@DEPLOYMENT_VERSION@'
}
}
}
});
self.ConfigManager.saveConfiguration(profiles);
Log.info(chalk.cyan(`\n${newProfile} inserted successfuly.`));
});
}
/**
* Edit saved profile in the configuration manager
*/
editProfile () {
const self = this;
const profiles = self.ConfigManager.getProfiles();
const foundProfile = self.ConfigManager.getActiveProfile();
const len = profiles.length;
if (foundProfile === undefined) {
Log.info(chalk.red('\nCannot find profile'));
return;
}
prompt.start();
prompt.get(createInsertEdit, (err, result) => {
if (err) {
return Utils.promptError(err);
}
const cartridges = CartridgeHelper.getCartridgesFromDir(self.workingDir);
const newList = [];
for (let i = 0; i < len; i++) {
const currentProfile = profiles[i];
if (currentProfile === foundProfile) {
currentProfile.profile = result.hostname.split('-')[0];
currentProfile.config = {
hostname: result.hostname,
username: result.username,
password: result.password,
cartridge: cartridges,
codeVersion: result.codeversion,
indentSize: result.indentSize,
exclude: result.exclude.split(' ')
};
}
newList.push(currentProfile);
}
self.ConfigManager.saveConfiguration(newList);
Log.info(chalk.cyan('\nSuccessfuly updated profile'));
});
}
/**
* List all profiles from the configuration manager
*/
listProfiles () {
const profiles = this.ConfigManager.getProfiles();
const activeProfile = this.ConfigManager.getActiveProfile();
const len = profiles.length;
let result;
for (let i = 0; i < len; i++) {
const currentProfile = profiles[i];
result = chalk.bgWhite(chalk.black(currentProfile.profile));
if (currentProfile === activeProfile) {
result += chalk.cyan(' <--- active');
}
Log.info(`\n${result}`);
}
}
/**
* Switch the active profile in the configuration manager
*/
switchProfile (name) {
const self = this;
const profiles = self.ConfigManager.getProfiles();
const foundProfile = self.ConfigManager.getProfile(name);
const len = profiles.length;
const newList = [];
if (foundProfile === undefined) {
Log.info(chalk.red(`\nCannot find ${name} profile.`));
return;
}
for (let i = 0; i < len; i++) {
const currentProfile = profiles[i];
currentProfile.SetActive(currentProfile === foundProfile);
newList.push(currentProfile);
}
self.ConfigManager.saveConfiguration(newList);
Log.info(chalk.cyan(`\nSwitched to ${foundProfile.profile}. It is now your active profile.`));
}
}
module.exports = ConfigEditor;