-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserSettings.js
160 lines (136 loc) · 3.87 KB
/
userSettings.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
"use strict";
const fs = require('fs');
const _ = require('lodash');
const childProcess = require('child_process');
const Promise = require('bluebird');
const log = require("debug")('common:userSettings');
const os = require("./os");
var contents;
/**
* Load config data from file
* @param configFile the file path of the config file.
*/
exports.load = function(configFile) {
if(contents) {
log("userSettings already loaded");
return;
}
load(configFile);
};
exports.configFilePath = function() {
return os.userDataPath() + '/SidekickConfig.json';
};
exports.configFilePath = function() {
return os.userDataPath() + '/SidekickConfig.json';
};
function load() {
var filePath = exports.configFilePath();
log('loading user config file: ' + filePath);
if(fs.existsSync(filePath)) {
log("config existed");
var tempContents = fs.readFileSync(filePath);
contents = JSON.parse(tempContents);
log('userSettings: ' + JSON.stringify(contents));
upgradeFileContents(contents);
} else {
log("no config file");
contents = createDefaultFile();
}
}
/**
* reloads the config file
*/
exports.reload = load;
/**
* Save current config to file
*
* Danger - SYNCHRONOUS
*
* this is only called from the GUI side
*/
exports.save = function() {
var contentsAsString = JSON.stringify(contents, null, 4);
log('save: ' + contentsAsString);
fs.writeFileSync(exports.configFilePath(), contentsAsString);
};
/**
* Gets the named property specified.
* @param propertyName the property to fetch update
*/
exports.getProperty = function(propertyName){
if(!contents) {
console.trace();
throw new Error("userSettings not loaded - ensure it's loaded in a process before use");
}
return contents[propertyName];
};
/**
* for remote side - better to avoid proxy objects
*/
exports.getPropertyJson = function(propertyName){
return JSON.stringify(contents[propertyName]);
};
/**
* Sets the named property to the value specified. Changes are not written to file until 'save' is called.
* @param propertyName the property to update
* @param newValue the new value to set the property to
*/
exports.setProperty = function(propertyName, newValue){
if(!contents){
contents = createDefaultFile();
}
contents[propertyName] = newValue;
};
exports.getGitBin = function() {
// either user has configured it, or it's in a standard location
return exports.getProperty("gitBin") || "git";
};
exports.isGitReachable = function(){
var property = exports.getProperty("gitBin");
log('gitBin: ' + property);
return validGit(property || "git");
};
function validGit(gitPath) {
return new Promise(function(resolve, reject) {
childProcess.exec(gitPath + " --version", function(err, stdout) {
if(err) {
reject(err)
} else {
var isGit = /\bgit\b/.test(stdout);
// if we're here, something is either git or pretending to be git :)
isGit ? resolve() : reject(new Error("not git"));
}
});
});
}
function createDefaultFile() {
//add default entries here
return {
userSettings: {
keyBindings: 'default',
editorTheme: 'default',
sidebarTheme: {colour: 'pale-elm', contrast: 'sk-menu-light'},
collapseCode: true,
useTabChar: true,
numSpacesForTab: 2,
currentPush: {
showUnstagedFiles: true,
}
},
repos: [],
};
}
function upgradeFileContents(contents) {
//add any settings that are in our current default but not in the loaded config
_.defaults(contents.userSettings, createDefaultFile().userSettings);
if(!contents.repos) {
contents.repos = [];
}
if(!contents.userSettings.currentPush) {
contents.userSettings.currentPush = {showUnstagedFiles: true};
}
//pre 0.3.18 sidebar colour upgrade
if(!contents.userSettings.sidebarTheme.colour) {
contents.userSettings.sidebarTheme = {colour: 'pale-elm', contrast: 'sk-menu-light'};
}
}