-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
125 lines (100 loc) · 3.41 KB
/
config.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
'use strict';
const {app} = require('electron');
const fs = require('fs');
const {utils} = require('./utils');
const moment = require('moment-timezone');
const AutoLaunch = require('auto-launch');
const configPath = app.getPath('userData') + '/config.json';
const autoLaunch = process.execPath.match(/node_modules/) ? null : new AutoLaunch({name: 'Everytime', path: process.execPath});
if(autoLaunch === null)
console.warn('AutoLaunch option will be ignored when running as a developer');
const validTimezones = new Set(
utils.listAllZones().map(tz => tz.code),
);
exports.autoLaunch = false;
exports.timeFormat = 12;
exports.offsetDisplay = 'utc';
exports.timezones = [{code: 'UTC', label: 'UTC'}];
exports.loadConfig = function() {
let rawConfig = {};
try {
rawConfig = JSON.parse(fs.readFileSync(configPath).toString());
}
catch(e) {} //don't care
exports.autoLaunch = rawConfig.autoLaunch;
exports.timeFormat = rawConfig.timeFormat;
exports.offsetDisplay = rawConfig.offsetDisplay;
exports.timezones = rawConfig.timezones;
exports.saveConfig();
};
exports.serialize = function() {
return {
autoLaunch: exports.autoLaunch,
timeFormat: exports.timeFormat,
offsetDisplay: exports.offsetDisplay,
timezones: exports.timezones,
};
};
function sanitizeConfig() {
let cleanConfig = {
autoLaunch: false,
timeFormat: 12, //either 12 or 24
offsetDisplay: 'utc', //one of: utc, local, both
timezones: [],
};
if(exports.autoLaunch === true)
cleanConfig.autoLaunch = true;
if(exports.timeFormat === 12 || exports.timeFormat === 24)
cleanConfig.timeFormat = exports.timeFormat;
if(['utc', 'local', 'both', 'none'].indexOf(exports.offsetDisplay) >= 0)
cleanConfig.offsetDisplay = exports.offsetDisplay;
if(Array.isArray(exports.timezones)) {
let tzNames = new Set();
for(let i = 0; i < exports.timezones.length; i++) {
let rawTz = exports.timezones[i];
if(!rawTz.code || 'string' !== typeof rawTz.code)
continue;
let zone = moment.tz.zone(rawTz.code);
if(zone === null || tzNames.has(zone.name) || !validTimezones.has(zone.name))
continue;
let cleanTz = {
code: zone.name,
label: (('string' === typeof rawTz.label) ? rawTz.label : ''),
};
tzNames.add(cleanTz.name);
cleanConfig.timezones.push(cleanTz);
}
}
//if we don't have any timezones, at least show UTC
if(cleanConfig.timezones.length === 0)
cleanConfig.timezones.push({code: 'UTC', label: 'UTC'});
exports.autoLaunch = cleanConfig.autoLaunch;
exports.timeFormat = cleanConfig.timeFormat;
exports.offsetDisplay = cleanConfig.offsetDisplay;
exports.timezones = cleanConfig.timezones;
utils.sortTimeZones(exports.timezones);
}
exports.saveConfig = function() {
sanitizeConfig();
let rawConfigJson = '';
try {
rawConfigJson = fs.readFileSync(configPath).toString();
}
catch(e) {} //don't care
let cleanConfig = {
autoLaunch: exports.autoLaunch,
timeFormat: exports.timeFormat,
offsetDisplay: exports.offsetDisplay,
timezones: exports.timezones,
};
let cleanConfigJson = JSON.stringify(cleanConfig, null, 2);
if(cleanConfigJson !== rawConfigJson)
fs.writeFileSync(configPath, cleanConfigJson);
if(autoLaunch !== null) {
if(exports.autoLaunch)
autoLaunch.enable();
else
autoLaunch.disable();
}
};
exports.loadConfig();