-
Notifications
You must be signed in to change notification settings - Fork 38
/
settings.js
191 lines (158 loc) · 5.07 KB
/
settings.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
/**
******************************************************************************
* @file settings.js
* @author David Middlecamp (david@spark.io)
* @company Spark ( https://www.spark.io/ )
* @source https://github.com/spark/spark-cli
* @version V1.0.0
* @date 14-February-2014
* @brief Setting module
******************************************************************************
Copyright (c) 2014 Spark Labs, Inc. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
var fs = require('fs');
var path = require('path');
var extend = require('xtend');
var utilities = require('./lib/utilities.js');
var settings = {
commandPath: "./commands/",
apiUrl: "https://api.spark.io",
clientId: "CLI2",
access_token: null,
minimumApiDelay: 500,
//useOpenSSL: true,
useSudoForDfu: false,
//2 megs -- this constant here is arbitrary
MAX_FILE_SIZE: 1024 * 1024 * 2,
overridesFile: null,
wirelessSetupFilter: /^Photon-.*$/,
notSourceExtensions: [
".ds_store",
".jpg",
".gif",
".png",
".include",
".ignore",
".ds_store",
".git",
".bin"
],
showIncludedSourceFiles: true,
dirIncludeFilename: "spark.include",
dirExcludeFilename: "spark.ignore",
knownApps: {
"deep_update_2014_06": "binaries/deep_update_2014_06.bin",
"cc3000": "binaries/cc3000-patch-programmer.bin",
"cc3000_1_14": "binaries/cc3000-patch-programmer_1_14.bin",
"tinker": "binaries/spark_tinker.bin",
"voodoo": "binaries/voodoospark.bin"
},
commandMappings: path.join(__dirname, "mappings.json")
};
settings.commandPath = __dirname + "/commands/";
//fix the paths on the known apps mappings
for(var name in settings.knownApps) {
settings.knownApps[name] = path.join(__dirname, settings.knownApps[name]);
}
settings.findHomePath = function() {
var envVars = [
'home',
'HOME',
'HOMEPATH',
'USERPROFILE'
];
for(var i=0;i<envVars.length;i++) {
var dir = process.env[envVars[i]];
if (dir && fs.existsSync(dir)) {
return dir;
}
}
return __dirname;
};
settings.ensureFolder = function() {
var sparkDir = path.join(settings.findHomePath(), ".spark");
if (!fs.existsSync(sparkDir)) {
fs.mkdirSync(sparkDir);
}
return sparkDir;
};
settings.findOverridesFile = function(profile) {
profile = profile || settings.profile || "spark";
var sparkDir = settings.ensureFolder();
return path.join(sparkDir, profile + ".config.json");
};
settings.loadOverrides = function (profile) {
profile = profile || settings.profile || "spark";
try {
var filename = settings.findOverridesFile(profile);
if (fs.existsSync(filename)) {
settings.overrides = JSON.parse(fs.readFileSync(filename));
settings = extend(settings, settings.overrides);
}
}
catch (ex) {
console.error('There was an error reading ' + settings.overrides + ': ', ex);
}
return settings;
};
settings.whichProfile = function() {
settings.profile = "spark";
var sparkDir = settings.ensureFolder();
var proFile = path.join(sparkDir, "profile.json"); //proFile, get it?
if (fs.existsSync(proFile)) {
var data = JSON.parse(fs.readFileSync(proFile));
settings.profile = (data) ? data.name : "spark";
settings.profile_json = data;
}
};
/**
* in another file in our user dir, we store a profile name that switches between setting override files
*/
settings.switchProfile = function(profileName) {
var sparkDir = settings.ensureFolder();
var proFile = path.join(sparkDir, "profile.json"); //proFile, get it?
var data = {
name: profileName
};
fs.writeFileSync(proFile, JSON.stringify(data, null, 2), { mode: '600' });
};
settings.override = function (profile, key, value) {
if (!settings.overrides) {
settings.overrides = {};
}
if (!settings[key]) {
// find any key that matches our key, regardless of case
var realKey = utilities.matchKey(key, settings, true);
if (realKey) {
//console.log("Using the setting \"" + realKey + "\" instead ");
key = realKey;
}
}
//store the new value (redundant)
settings[key] = value;
//store that in overrides
settings.overrides[key] = value;
//make sure our overrides are in sync
settings = extend(settings, settings.overrides);
try {
var filename = settings.findOverridesFile(profile);
fs.writeFileSync(filename, JSON.stringify(settings.overrides, null, 2), { mode: '600' });
}
catch (ex) {
console.error('There was an error writing ' + settings.overrides + ': ', ex);
}
};
settings.whichProfile();
settings.loadOverrides();
module.exports = settings;