forked from JoilsonOliveira/auto-negotiate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings_migrator.js
49 lines (42 loc) · 1.62 KB
/
settings_migrator.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
"use strict"
const DefaultSettings = {
"AUTO_ACCEPT_THRESHOLD": 100,
"AUTO_REJECT_THRESHOLD": 75,
"UNATTENDED_MANUAL_NEGOTIATE": false,
"DELAY_ACTIONS": true,
"log": false
}
module.exports = function MigrateSettings(from_ver, to_ver, settings) {
if (from_ver === undefined) {
// Migrate legacy config file
return Object.assign(Object.assign({}, DefaultSettings), settings);
} else if (from_ver === null) {
// No config file exists, use default settings
return DefaultSettings;
} else {
// Migrate from older version (using the new system) to latest one
if (from_ver + 1 < to_ver) {
// Recursively upgrade in one-version steps
settings = MigrateSettings(from_ver, from_ver + 1, settings);
return MigrateSettings(from_ver + 1, to_ver, settings);
}
// If we reach this point it's guaranteed that from_ver === to_ver - 1, so we can implement
// a switch for each version step that upgrades to the next version. This enables us to
// upgrade from any version to the latest version without additional effort!
switch(to_ver)
{
// keep old settings, add new ones
default:
let oldsettings = settings
settings = Object.assign(DefaultSettings, {});
for(let option in oldsettings) {
if(settings[option]) {
settings[option] = oldsettings[option]
}
}
if(from_ver < to_ver) console.log('[Negotiator] Your settings have been updated to version ' + to_ver + '. You can edit the new config file after the next relog.')
break;
}
return settings;
}
}