-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathsettings.js
More file actions
219 lines (187 loc) · 5.44 KB
/
settings.js
File metadata and controls
219 lines (187 loc) · 5.44 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
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
'use strict';
const fs = require('fs');
const path = require('path');
const extend = require('xtend');
const _ = require('lodash');
let settings = {
apiUrl: 'https://api.particle.io',
get isStaging() {
return this.apiUrl.includes('staging');
},
clientId: 'CLI2',
access_token: null,
username: null,
minimumApiDelay: 500,
useSudoForDfu: false,
flashWarningShownOn: null,
// TODO set to false once we give flags to control this
disableUpdateCheck: envValueBoolean('PARTICLE_DISABLE_UPDATE', false),
updateCheckInterval: 4 * 60 * 60 * 1000, // 4 hours
updateCheckTimeout: 3000,
//10 megs -- this constant here is arbitrary
MAX_FILE_SIZE: 1024 * 1024 * 10,
wirelessSetupFilter: /^Photon-.*$/,
serial_follow_delay: 250,
manifestHost: envValue('PARTICLE_MANIFEST_HOST','binaries.particle.io'),
notSourceExtensions: [
'.ds_store',
'.jpg',
'.gif',
'.png',
'.include',
'.ignore',
'.ds_store',
'.git',
'.bin'
],
showIncludedSourceFiles: true,
cloudKnownApps: {
'tinker': true
},
tachyonMeta: 'https://linux-dist.particle.io/meta',
tachyonCacheLimitGB: 10
};
function envValue(varName, defaultValue) {
const value = process.env[varName];
return (typeof value === 'undefined') ? defaultValue : value;
}
function envValueBoolean(varName, defaultValue) {
const value = envValue(varName);
if (value === 'true' || value === 'TRUE' || value === '1') {
return true;
} else if (value === 'false' || value === 'FALSE' || value === '0') {
return false;
} else {
return defaultValue;
}
}
settings.findHomePath = () => {
const envVars = [
'home',
'HOME',
'HOMEPATH',
'USERPROFILE'
];
for (let i = 0;i < envVars.length;i++) {
const dir = process.env[envVars[i]];
if (dir && fs.existsSync(dir)) {
return dir;
}
}
return __dirname;
};
settings.ensureFolder = () => {
const particleDir = path.join(settings.findHomePath(), '.particle');
if (!fs.existsSync(particleDir)) {
fs.mkdirSync(particleDir);
}
return particleDir;
};
settings.findOverridesFile = (profile) => {
profile = profile || settings.profile || 'particle';
const particleDir = settings.ensureFolder();
return path.join(particleDir, profile + '.config.json');
};
settings.loadOverrides = (profile) => {
profile = profile || settings.profile || 'particle';
try {
const filename = settings.findOverridesFile(profile);
if (fs.existsSync(filename)) {
settings.overrides = JSON.parse(fs.readFileSync(filename));
// need to do an in-situ extend since external clients may have already obtained the settings object
// settings = extend(settings, settings.overrides);
_.extend(settings, settings.overrides);
}
} catch (ex) {
console.error('There was an error reading ' + settings.overrides + ': ', ex);
}
return settings;
};
settings.whichProfile = () => {
settings.profile = 'particle';
settings.readProfileData();
};
/**
* in another file in our user dir, we store a profile name that switches between setting override files
*/
settings.switchProfile = (profileName) => {
if (!settings.profile_json) {
settings.profile_json = {};
}
settings.profile_json.name = profileName;
settings.saveProfileData();
};
settings.readProfileData = () => {
const particleDir = settings.ensureFolder();
const proFile = path.join(particleDir, 'profile.json'); //proFile, get it?
if (fs.existsSync(proFile)) {
try {
const data = JSON.parse(fs.readFileSync(proFile));
settings.profile = (data) ? data.name : 'particle';
settings.profile_json = data;
} catch (err) {
throw new Error('Error parsing file ' + proFile + ': ' + err);
}
} else {
settings.profile = 'particle';
settings.profile_json = {};
}
};
settings.saveProfileData = () => {
const particleDir = settings.ensureFolder();
const proFile = path.join(particleDir, 'profile.json'); //proFile, get it?
fs.writeFileSync(proFile, JSON.stringify(settings.profile_json, null, 2), { mode: '600' });
};
settings.ssoAuthConfig = () => {
const isProduction = settings.apiUrl === 'https://api.particle.io';
if (isProduction) {
return {
ssoAuthUri: 'https://id.particle.io/oauth2/default/v1',
ssoClientId: '0oa19uiy26XIs3XW55d7'
};
} else {
return {
ssoAuthUri: 'https://id.staging.particle.io/oauth2/default/v1',
ssoClientId: '0oa19umyki69O4Kvb5d7'
};
}
};
// this is here instead of utilities to prevent a require-loop
// when files that utilties requires need settings
function matchKey(needle, obj, caseInsensitive) {
needle = (caseInsensitive) ? needle.toLowerCase() : needle;
for (const key in obj) {
const keyCopy = (caseInsensitive) ? key.toLowerCase() : key;
if (keyCopy === needle) {
//return the original
return key;
}
}
return null;
}
settings.override = (profile, key, value) => {
if (!settings.overrides) {
settings.overrides = {};
}
if (!settings[key]) {
// find any key that matches our key, regardless of case
const realKey = 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 {
const 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);
}
};
module.exports = settings;