/
storage.ts
296 lines (260 loc) · 7.65 KB
/
storage.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import type { Disposable, Event, ExtensionContext, SecretStorageChangeEvent } from 'vscode';
import { EventEmitter } from 'vscode';
import type { ViewShowBranchComparison } from './config';
import type { StoredSearchQuery } from './git/search';
import type { Subscription } from './subscription';
import type { TrackedUsage, TrackedUsageKeys } from './usageTracker';
import type { CompletedActions } from './webviews/home/protocol';
export type StorageChangeEvent =
| {
/**
* The key of the stored value that has changed.
*/
readonly key: GlobalStoragePath;
readonly workspace: false;
}
| {
/**
* The key of the stored value that has changed.
*/
readonly key: WorkspaceStoragePath;
readonly workspace: true;
};
export class Storage implements Disposable {
private _onDidChange = new EventEmitter<StorageChangeEvent>();
get onDidChange(): Event<StorageChangeEvent> {
return this._onDidChange.event;
}
private _onDidChangeSecrets = new EventEmitter<SecretStorageChangeEvent>();
get onDidChangeSecrets(): Event<SecretStorageChangeEvent> {
return this._onDidChangeSecrets.event;
}
private readonly _disposable: Disposable;
constructor(private readonly context: ExtensionContext) {
this._disposable = this.context.secrets.onDidChange(e => this._onDidChangeSecrets.fire(e));
}
dispose(): void {
this._disposable.dispose();
}
get<T extends GlobalStoragePath>(key: T): GlobalStoragePathValue<T>;
get<T extends GlobalStoragePath>(
key: T,
defaultValue: NonNullable<GlobalStoragePathValue<T>>,
): NonNullable<GlobalStoragePathValue<T>>;
get<T extends GlobalStoragePath>(
key: T,
defaultValue?: GlobalStoragePathValue<T>,
): GlobalStoragePathValue<T> | undefined {
return this.context.globalState.get(`gitlens:${key}`, defaultValue);
}
async delete<T extends GlobalStoragePath>(key: T): Promise<void> {
await this.context.globalState.update(`gitlens:${key}`, undefined);
this._onDidChange.fire({ key: key, workspace: false });
}
async store<T extends GlobalStoragePath>(key: T, value: GlobalStoragePathValue<T>): Promise<void> {
await this.context.globalState.update(`gitlens:${key}`, value);
this._onDidChange.fire({ key: key, workspace: false });
}
async getSecret(key: SecretKeys): Promise<string | undefined> {
return this.context.secrets.get(key);
}
async deleteSecret(key: SecretKeys): Promise<void> {
return this.context.secrets.delete(key);
}
async storeSecret(key: SecretKeys, value: string): Promise<void> {
return this.context.secrets.store(key, value);
}
getWorkspace<T extends WorkspaceStoragePath>(key: T): WorkspaceStoragePathValue<T>;
getWorkspace<T extends WorkspaceStoragePath>(
key: T,
defaultValue: NonNullable<WorkspaceStoragePathValue<T>>,
): NonNullable<WorkspaceStoragePathValue<T>>;
getWorkspace<T extends WorkspaceStoragePath>(
key: T,
defaultValue?: WorkspaceStoragePathValue<T>,
): WorkspaceStoragePathValue<T> | undefined {
return this.context.workspaceState.get(`gitlens:${key}`, defaultValue);
}
async deleteWorkspace<T extends WorkspaceStoragePath>(key: T): Promise<void> {
await this.context.workspaceState.update(`gitlens:${key}`, undefined);
this._onDidChange.fire({ key: key, workspace: true });
}
async storeWorkspace<T extends WorkspaceStoragePath>(key: T, value: WorkspaceStoragePathValue<T>): Promise<void> {
await this.context.workspaceState.update(`gitlens:${key}`, value);
this._onDidChange.fire({ key: key, workspace: true });
}
}
export type SecretKeys = string;
export const enum DeprecatedStorageKeys {
/** @deprecated */
DisallowConnectionPrefix = 'gitlens:disallow:connection:',
}
export const enum SyncedStorageKeys {
Version = 'gitlens:synced:version',
PreReleaseVersion = 'gitlens:synced:preVersion',
HomeViewWelcomeVisible = 'gitlens:views:welcome:visible',
}
export interface GlobalStorage {
avatars?: [string, StoredAvatar][];
provider: {
authentication: {
skip: Record<string, boolean>;
};
};
home: {
actions: {
completed?: CompletedActions[];
};
steps: {
completed?: string[];
};
sections: {
dismissed?: string[];
};
};
pendingWelcomeOnFocus?: boolean;
pendingWhatsNewOnFocus?: boolean;
plus: {
migratedAuthentication?: boolean;
};
// Don't change this key name ('premium`) as its the stored subscription
premium: {
subscription?: Stored<Subscription>;
};
synced: {
version?: string;
// Keep the pre-release version separate from the released version
preVersion?: string;
};
usages?: Record<TrackedUsageKeys, TrackedUsage>;
version?: string;
// Keep the pre-release version separate from the released version
preVersion?: string;
views: {
welcome: {
visible?: boolean;
};
};
}
export interface WorkspaceStorage {
assumeRepositoriesOnStartup?: boolean;
branch: {
comparisons?: StoredBranchComparisons;
};
connected: Record<string, boolean>;
gitComandPalette: {
usage?: RecentUsage;
};
gitPath?: string;
graph: {
banners: {
dismissed?: Record<string, boolean>;
};
columns?: Record<string, StoredGraphColumn>;
};
remote: {
default?: string;
};
starred: {
branches?: StoredStarred;
repositories?: StoredStarred;
};
views: {
repositories: {
autoRefresh?: boolean;
};
searchAndCompare: {
keepResults?: boolean;
pinned?: StoredPinnedItems;
};
commitDetails: {
autolinksExpanded?: boolean;
filesAsTree?: boolean;
};
};
pinned: {
/** @deprecated use `gitlens:views:searchAndCompare:pinned` */
comparisons?: DeprecatedPinnedComparisons;
};
}
export interface Stored<T, SchemaVersion extends number = 1> {
v: SchemaVersion;
data: T;
}
export interface StoredAvatar {
uri: string;
timestamp: number;
}
export interface StoredBranchComparison {
ref: string;
notation: '..' | '...' | undefined;
type: Exclude<ViewShowBranchComparison, false> | undefined;
}
export interface StoredBranchComparisons {
[id: string]: string | StoredBranchComparison;
}
export interface StoredGraphColumn {
isHidden?: boolean;
width?: number;
}
export interface StoredNamedRef {
label?: string;
ref: string;
}
export interface StoredPinnedComparison {
type: 'comparison';
timestamp: number;
path: string;
ref1: StoredNamedRef;
ref2: StoredNamedRef;
notation?: '..' | '...';
}
export interface StoredPinnedSearch {
type: 'search';
timestamp: number;
path: string;
labels: {
label: string;
queryLabel:
| string
| {
label: string;
resultsType?: { singular: string; plural: string };
};
};
search: StoredSearchQuery;
}
export type StoredPinnedItem = StoredPinnedComparison | StoredPinnedSearch;
export type StoredPinnedItems = Record<string, StoredPinnedItem>;
export type StoredStarred = Record<string, boolean>;
export type RecentUsage = Record<string, number>;
interface DeprecatedPinnedComparison {
path: string;
ref1: StoredNamedRef;
ref2: StoredNamedRef;
notation?: '..' | '...';
}
interface DeprecatedPinnedComparisons {
[id: string]: DeprecatedPinnedComparison;
}
type SubPath<T, Key extends keyof T> = Key extends string
? T[Key] extends Record<string, any>
?
| `${Key}:${SubPath<T[Key], Exclude<keyof T[Key], keyof any[]>>}`
| `${Key}:${Exclude<keyof T[Key], keyof any[]> & string}`
: never
: never;
type Path<T> = SubPath<T, keyof T> | keyof T;
type PathValue<T, P extends Path<T>> = P extends `${infer Key}:${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: never
: P extends keyof T
? T[P]
: never;
type GlobalStoragePath = Path<GlobalStorage>;
type GlobalStoragePathValue<P extends GlobalStoragePath> = PathValue<GlobalStorage, P>;
type WorkspaceStoragePath = Path<WorkspaceStorage>;
type WorkspaceStoragePathValue<P extends WorkspaceStoragePath> = PathValue<WorkspaceStorage, P>;