/
install.ts
95 lines (86 loc) · 2.79 KB
/
install.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
import { BrowserWindow, ipcMain } from "electron";
import { download } from "electron-dl";
import { buildKey } from "#/build-key";
import { DownloadState, ID } from "#/enums";
import { appSettingsStore } from "@/stores";
import { getCaptainData, getCaptainDownloads, getDirectory } from "@/utils/path-helpers";
import { unpack } from "@/utils/unpack";
import { initialize, populateFromDocuments, reset } from "@/utils/vector-store";
ipcMain.on(
buildKey([ID.INSTALL], { suffix: ":start" }),
async (_event, data: { url: string; destination: string; unzip?: boolean }[]) => {
const window_ = BrowserWindow.getFocusedWindow();
if (!window_) {
return;
}
try {
// Iterate over each download object
const items: Promise<void>[] = [];
for (const { url, destination, unzip } of data) {
await download(window_, url, {
directory: getCaptainDownloads(),
onStarted() {
appSettingsStore.set("status", DownloadState.ACTIVE);
window_.webContents.send(
buildKey([ID.INSTALL], { suffix: ":started" }),
true
);
},
onProgress(progress) {
window_.webContents.send(
buildKey([ID.INSTALL], { suffix: ":progress" }),
progress
);
},
onCancel() {
window_.webContents.send(
buildKey([ID.INSTALL], { suffix: ":cancelled" }),
true
);
appSettingsStore.set("status", DownloadState.CANCELLED);
},
async onCompleted(file) {
const targetPath = getCaptainData(destination);
if (unzip) {
// Unpack immediately and send to array to allow awaiting multiple unzips
items.push(
unpack(
getDirectory("7zip", "win", "7za.exe"),
file.path,
targetPath,
true
)
);
}
},
});
}
// Set the state to unpacking and wait until all items are unpacked and ready
window_.webContents.send(buildKey([ID.INSTALL], { suffix: ":unpacking" }), true);
appSettingsStore.set("status", DownloadState.UNPACKING);
await Promise.all(items);
// Everything was downloaded & unpacked
window_.webContents.send(buildKey([ID.INSTALL], { suffix: ":completed" }), true);
appSettingsStore.set("status", DownloadState.DONE);
} catch (error) {
if (error instanceof Error) {
window_.webContents.send(
buildKey([ID.INSTALL], { suffix: ":failed" }),
error.message
);
appSettingsStore.set("status", DownloadState.FAILED);
}
}
}
);
ipcMain.on(buildKey([ID.INSTALL], { suffix: ":initialize" }), async event => {
try {
// Start the vector store and fill it with data
await initialize();
await reset();
await populateFromDocuments();
event.sender.send(buildKey([ID.INSTALL], { suffix: ":initialized" }), true);
} catch (error) {
event.sender.send(buildKey([ID.INSTALL], { suffix: ":error" }), error);
}
});