-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.js
More file actions
131 lines (106 loc) · 4 KB
/
configuration.js
File metadata and controls
131 lines (106 loc) · 4 KB
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
'use strict'
var fs = require('fs');
var path = require('path');
const os = require('os');
var baseGlobalSettings = {
userToken: null,
apiUrl: 'https://codealike.com/api/v2'
};
var basePluginSettings = {
idleCheckInterval: 30000, // in milliseconds
idleMaxPeriod: 60000, // in milliseconds
flushInterval: 300000 // in milliseconds
};
var Configuration = {
codealikeBasePath: null,
instancePath: null, // path where current running instance related stuff is saved
cachePath: null,
historyPath: null,
globalSettings: {
userToken: null,
apiUrl: 'https://codealike.com/api/v2',
},
pluginSettings: {
idleCheckInterval: 30000, // in milliseconds
idleMaxPeriod: 60000, // in milliseconds
flushInterval: 300000 // in milliseconds
},
instanceSettings: {
clientId: 'defaultClient',
clientVersion: '0.0.0.1',
instanceId: 0
},
initialize: function(clientId, clientVersion, instanceId) {
// verify required folder structure exists
this.createRequiredPaths(clientId, instanceId);
// store current instance settings
this.instanceSettings.clientId = clientId;
this.instanceSettings.clientVersion = clientVersion;
this.instanceSettings.instanceId = instanceId;
},
// plugin settings can be injected from outside
loadPluginSettings: function(settings) {
Configuration.pluginSettings = Object.assign({}, basePluginSettings, settings);
},
/*
* loadCodealikeSettings:
* This method loads user settings stored in codealike user folder
* After this method call userToken and user profile information
* should been loaded
*/
loadGlobalSettings: function() {
let codealikeSettingsFile = path.join(this.codealikeBasePath, 'user.json');
if (fs.existsSync(codealikeSettingsFile)) {
let existingConfiguration = JSON.parse(fs.readFileSync(codealikeSettingsFile, 'utf8'));
if (existingConfiguration) {
this.globalSettings = Object.assign({}, baseGlobalSettings, existingConfiguration);
}
else {
this.globalSettings = Object.assign({}, baseGlobalSettings);
}
}
},
/*
* saveCodealikeGlobalSettings
* This method saves user settings configured in current configuration instance
* to the codealike user folder
*/
savelGlobalSettings: function(settings) {
let codealikeSettingsFile = path.join(this.codealikeBasePath, 'user.json');
// convert object to string
let jsonString = JSON.stringify(this.globalSettings);
// if registered, save configuration file
// have to save configuration file
fs.writeFile(codealikeSettingsFile, jsonString, 'utf8',
function(error) {
if (error) {
throw new Error('Could not save global settings file.');
}
});
},
setUserToken: function(userToken) {
this.globalSettings.userToken = userToken;
},
getUserToken: function() {
return this.globalSettings.userToken;
},
ensurePathExists: function(path) {
// ensure log and trace paths exists
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
},
createRequiredPaths: function(clientId, instanceId) {
this.codealikeBasePath = path.join(os.homedir(), '.codealike');
this.ensurePathExists(this.codealikeBasePath);
let clientPath = path.join(this.codealikeBasePath, clientId);
this.ensurePathExists(clientPath);
this.instancePath = path.join(clientPath, instanceId);
this.ensurePathExists(this.instancePath);
this.cachePath = path.join(this.codealikeBasePath, 'cache');
this.ensurePathExists(this.cachePath);
this.historyPath = path.join(this.codealikeBasePath, 'history');
this.ensurePathExists(this.historyPath);
}
}
module.exports = Configuration;