This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
storage.ts
92 lines (78 loc) · 3.01 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
import { open } from "sqlite";
import * as sqlite3 from "sqlite3";
import * as vscode from "vscode";
import { ExtensionList, ExtensionValue, ProfileList, StorageKey, StorageKeyID, StorageValue } from "./types";
import { environment } from "./utils";
//
export async function getDisabledExtensionsGlobalStorage() {
const db = await open({
filename: `${environment.GLOBAL_STORAGE_PATH}state.vscdb`,
driver: sqlite3.Database,
});
const data = ((await db.get("SELECT key, value FROM ItemTable WHERE key = ?", "extensionsIdentifiers/disabled")) as StorageValue) || undefined;
await db.close();
if (data?.value) {
return JSON.parse(data.value) as ExtensionValue[];
}
return []; // default
}
// VSCode hides disabled extensions
// https://github.com/microsoft/vscode/issues/15466
export function getEnabledExtensions() {
return vscode.extensions.all
.filter((e) => !/.*(?:\\\\|\/)resources(?:\\\\|\/)app(?:\\\\|\/)extensions(?:\\\\|\/).*/i.test(e.extensionPath)) // ignore internal extensions
.map(
(item) =>
({
id: item.id,
uuid: item?.packageJSON.uuid,
label: item?.packageJSON.displayName,
description: item?.packageJSON.description,
} as ExtensionValue)
);
}
export async function getWorkspaceStorageValue(key: "enabled" | "disabled") {
const db = await open({
filename: `${environment.WORKSPACE_STORAGE_PATH_UUID}state.vscdb`,
driver: sqlite3.Database,
});
let data = (await db.get("SELECT key, value FROM ItemTable WHERE key = ?", `extensionsIdentifiers/${key}`)) as StorageValue;
await db.close();
if (data?.value) {
return JSON.parse(data.value) as ExtensionValue[];
}
return []; // default
}
export async function setWorkspaceStorageValue(key: "enabled" | "disabled", extensions: ExtensionValue[]) {
const db = await open({
filename: `${environment.WORKSPACE_STORAGE_PATH_UUID}state.vscdb`,
driver: sqlite3.Database,
});
await db.run("INSERT OR REPLACE INTO ItemTable (key, value) VALUES (?, ?)", `extensionsIdentifiers/${key}`, JSON.stringify(extensions));
return await db.close();
}
/**
* @deprecated use getGlobalStateValue
*/
export async function getGlobalStorageValue(key: StorageKey): Promise<ExtensionList | ProfileList> {
const db = await open({
filename: `${environment.GLOBAL_STORAGE_PATH}state.vscdb`,
driver: sqlite3.Database,
});
let data = (await db.get("SELECT key, value FROM ItemTable WHERE key = ?", key)) as StorageValue;
await db.close();
if (data?.value) {
return JSON.parse(data.value);
}
return {}; // default
}
export async function setGlobalStateValue(ctx: vscode.ExtensionContext, key: StorageKeyID, value: ExtensionList | ProfileList) {
return await ctx.globalState.update(key, value);
}
export async function getGlobalStateValue(ctx: vscode.ExtensionContext, key: StorageKeyID): Promise<ExtensionList | ProfileList> {
const data = ctx.globalState.get<ExtensionList | ProfileList>(key);
if (data !== undefined) {
return data;
}
return {}; // default
}