-
Notifications
You must be signed in to change notification settings - Fork 331
/
migration.ts
45 lines (42 loc) · 1.44 KB
/
migration.ts
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
import { Layerr } from "layerr";
import { naiveClone } from "../../shared/library/clone";
import { AppStartMode, Config } from "../types";
export type ConfigMigration = [name: string, migration: (config: Config) => Config | null];
const MIGRATIONS: Array<ConfigMigration> = [
[
"startInBackground",
(config: Config) => {
if (
config.preferences &&
typeof config.preferences["startInBackground"] === "boolean"
) {
const prefs = { ...config.preferences };
prefs.startMode = config.preferences["startInBackground"]
? AppStartMode.HiddenAlways
: AppStartMode.None;
delete prefs["startInBackground"];
return {
...config,
preferences: prefs
};
}
return null; // No change
}
]
];
export function runConfigMigrations(config: Config): [Config, changed: boolean] {
let current = naiveClone(config),
changed = false;
for (const [name, execute] of MIGRATIONS) {
try {
const result = execute(current);
if (result !== null) {
changed = true;
current = result;
}
} catch (err) {
throw new Layerr(err, `Failed executing config migration: ${name}`);
}
}
return [current, changed];
}