-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPersistentConfigServiceElectron.ts
144 lines (135 loc) · 4.66 KB
/
PersistentConfigServiceElectron.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
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
import Store, { Options } from "electron-store";
import {
PersistentConfigService,
PersistedConfig,
PersistedConfigKeys,
UserSelectedApplication,
} from "../../../core/services";
import { find } from "lodash";
const OPTIONS: Options<Record<string, unknown>> = {
// Defines a validation schema for data inserted into the persistent storage
// if a breaking change is made see migration patterns in elecron-store docs
schema: {
// Deprecated in 6.0.0
[PersistedConfigKeys.AllenMountPoint]: {
type: "string",
},
[PersistedConfigKeys.CsvColumns]: {
type: "array",
items: {
type: "string",
},
},
[PersistedConfigKeys.DisplayAnnotations]: {
type: "array",
items: {
type: "object",
properties: {
annotationDisplayName: {
type: "string",
},
annotationName: {
type: "string",
},
description: {
type: "string",
},
type: {
type: "string",
},
},
},
},
// ImageJExecutable is Deprecated
[PersistedConfigKeys.ImageJExecutable]: {
type: "string",
},
[PersistedConfigKeys.HasUsedApplicationBefore]: {
type: "boolean",
},
[PersistedConfigKeys.UserSelectedApplications]: {
type: "array",
items: {
type: "object",
properties: {
defaultFileKinds: {
type: "array",
items: {
type: "string",
},
},
filePath: {
type: "string",
},
},
},
},
},
migrations: {
// Migrate deprecated ImageJExecutable to new UserSelectedApplication key
">4.3.0": (store) => {
if (store.has(PersistedConfigKeys.ImageJExecutable)) {
const fijiExePath = store.get(PersistedConfigKeys.ImageJExecutable) as string;
const userSelectedApplications = store.get(
PersistedConfigKeys.UserSelectedApplications,
[]
) as UserSelectedApplication[];
const fijiConfig = find(
userSelectedApplications,
(config) => config.filePath === fijiExePath
);
if (!fijiConfig) {
store.set(PersistedConfigKeys.UserSelectedApplications, [
...userSelectedApplications,
{ filePath: fijiExePath, defaultFileKinds: [] },
]);
}
// Once migrated, remove the deprecated path
store.delete(PersistedConfigKeys.ImageJExecutable);
}
},
// Remove PersistedConfigKeys.AllenMountPoint
">5.2.0": (store) => {
if (store.has(PersistedConfigKeys.AllenMountPoint)) {
store.delete(PersistedConfigKeys.AllenMountPoint);
}
},
},
};
interface PersistentConfigServiceElectronOptions {
clearExistingData?: boolean;
}
export default class PersistentConfigServiceElectron implements PersistentConfigService {
private store: Store;
public constructor(options: PersistentConfigServiceElectronOptions = {}) {
this.store = new Store(OPTIONS);
if (options.clearExistingData) {
this.store.clear();
}
}
public get(key: PersistedConfigKeys): any {
return this.store.get(key);
}
public getAll(): PersistedConfig {
return Object.values(PersistedConfigKeys).reduce(
(config: PersistedConfig, key) => ({
...config,
[key as string]: this.get(key),
}),
{}
);
}
public persist(config: PersistedConfig): void;
public persist(key: PersistedConfigKeys, value: any): void;
public persist(arg: PersistedConfigKeys | PersistedConfig, value?: any) {
if (typeof arg === "object") {
Object.entries(arg as PersistedConfig).forEach(([key, value]) => {
this.persist(key as PersistedConfigKeys, value);
});
} else if (value === undefined || value === null) {
this.store.delete(arg);
} else {
this.store.set(arg, value);
}
}
}