-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathdbContext.tsx
More file actions
509 lines (454 loc) · 15 KB
/
Copy pathdbContext.tsx
File metadata and controls
509 lines (454 loc) · 15 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import electron from "electron";
import * as electronFs from "fs";
import moment from "moment";
import React, { createContext, useContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
import packageJson from "../../package.json";
import {
IMPORT_REPLY_CHANNEL,
JSON_IMPORT_REPLY_FORMAT,
NAVIGATION_CHANNEL,
NAVIGATION_CHANNEL_MESSAGE,
OPEN_DIALOG_CHANNEL,
OPEN_ERROR_DIALOG_CHANNEL,
SAVE_REPLY_CHANNEL,
SQLITE_IMPORT_REPLY_FORMAT,
SQLITE_PATH_FOR_JSON_REQUEST_FORMAT,
SQLITE_SAVE_REPLY_FORMAT,
UTIL_CHANNEL,
} from "../constants/IpcConnection";
import { DEFAULT_ROUTE_ON_IMPORT, ROUTES } from "../constants/routes";
import { NO_RESOURCES_ERROR } from "../constants/errors";
import { AddEntry, GetHistory, RemoveEntry } from "../services/historyStore";
import { WorkbenchDB } from "../services/workbenchDB";
import { isSqliteSchemaOutdated } from "../utils/checks";
import { ScanInfo, parseScanInfo } from "../utils/parsers";
import {
generateLicenseDetectionNavigationUrl,
generatePackageNavigationUrl,
} from "../utils/navigatorQueries";
const { version: workbenchVersion } = packageJson;
const { ipcRenderer } = electron;
export type PathType = "file" | "directory";
interface BasicValueState {
db: WorkbenchDB | null;
initialized: boolean;
importedSqliteFilePath: string | null;
scanInfo: ScanInfo | null;
}
interface WorkbenchContextProperties extends BasicValueState {
currentPath: string | null;
currentPathType: PathType;
loadingStatus: null | number;
processingQuery: boolean;
startImport: () => void;
abortImport: () => void;
closeFile: () => void;
startProcessing: () => void;
endProcessing: () => void;
sqliteParser: (sqliteFilePath: string, preventNavigation?: boolean) => void;
jsonParser: (
jsonFilePath: string,
sqliteFilePath: string,
preventNavigation?: boolean
) => void;
importJsonFile: (jsonFilePath: string) => void;
updateLoadingStatus: React.Dispatch<React.SetStateAction<number | null>>;
updateCurrentPath: (newPath: string, type: PathType) => void;
goToFileInTableView: (path: string) => void;
goToLicenseDetection: (licenseDetectionIdentifier: string) => void;
goToPackage: (packageIdentifier: string) => void;
}
export const defaultWorkbenchContextValue: WorkbenchContextProperties = {
db: null,
initialized: false,
importedSqliteFilePath: null,
scanInfo: null,
loadingStatus: null,
currentPath: null,
currentPathType: "directory",
processingQuery: false,
jsonParser: () => null,
sqliteParser: () => null,
importJsonFile: () => null,
updateLoadingStatus: () => null,
startImport: () => null,
abortImport: () => null,
closeFile: () => null,
startProcessing: () => null,
endProcessing: () => null,
updateCurrentPath: () => null,
goToFileInTableView: () => null,
goToLicenseDetection: () => null,
goToPackage: () => null,
};
const WorkbenchContext = createContext<WorkbenchContextProperties>(
defaultWorkbenchContextValue
);
export const WorkbenchDBProvider = (
props: React.PropsWithChildren<Record<string, unknown>>
) => {
const navigate = useNavigate();
const [loadingStatus, updateLoadingStatus] = useState<number | null>(null);
const [processingQuery, setProcessingQuery] = useState<boolean>(null);
const [value, setValue] = useState<BasicValueState>({
db: null,
initialized: false,
importedSqliteFilePath: null,
scanInfo: null,
});
const [currentPath, setCurrentPath] = useState<string>("");
const [currentPathType, setCurrentPathType] = useState<PathType>("directory");
function updateCurrentPath(path: string, pathType: PathType) {
setCurrentPath(path);
setCurrentPathType(pathType);
}
function changeRouteOnImport() {
navigate(DEFAULT_ROUTE_ON_IMPORT);
}
function goToFileInTableView(path: string) {
updateCurrentPath(path, "file");
navigate("/" + ROUTES.TABLE_VIEW);
}
function goToLicenseDetection(licenseDetectionIdentifier: string) {
navigate(generateLicenseDetectionNavigationUrl(licenseDetectionIdentifier));
}
function goToPackage(packageIdentifier: string) {
navigate(generatePackageNavigationUrl(packageIdentifier));
}
const startImport = () => {
updateLoadingStatus(0);
setProcessingQuery(false);
setValue({
db: null,
initialized: false,
importedSqliteFilePath: null,
scanInfo: null,
});
};
const abortImport = () => updateLoadingStatus(null);
const closeFile = () => {
updateLoadingStatus(null);
setProcessingQuery(false);
setValue({
db: null,
initialized: false,
importedSqliteFilePath: null,
scanInfo: null,
});
navigate(ROUTES.HOME);
ipcRenderer.send(UTIL_CHANNEL.RESET_FILE_TITLE);
};
const updateWorkbenchDB = async (db: WorkbenchDB, sqliteFilePath: string) => {
updateLoadingStatus(100);
setValue({
db,
initialized: true,
importedSqliteFilePath: sqliteFilePath,
scanInfo: parseScanInfo(await db.getScanInfo()),
});
};
function sqliteParser(sqliteFilePath: string, preventNavigation?: boolean) {
startImport();
// Create connection to existing database when importing a sqlite file
const newWorkbenchDB = new WorkbenchDB({
dbName: "workbench_db",
dbStoragePath: sqliteFilePath,
});
updateLoadingStatus(25);
// Check that that the database schema matches current schema.
newWorkbenchDB
.getScanInfo()
.then((infoHeader) => {
// Check that the database has the correct header information.
if (!infoHeader) {
const errTitle = "Invalid SQLite file";
const errMessage = `Invalid SQLite file: ${sqliteFilePath}
The SQLite file is invalid. Try re-importing the ScanCode JSON file and creating a new SQLite file.`;
console.error("Handled invalid sqlite import", {
title: errTitle,
message: errMessage,
});
ipcRenderer.send(OPEN_ERROR_DIALOG_CHANNEL, {
title: errTitle,
message: errMessage,
});
abortImport();
return;
}
updateLoadingStatus(50);
const dbVersion = infoHeader.getDataValue("workbench_version");
if (!dbVersion || isSqliteSchemaOutdated(dbVersion, workbenchVersion)) {
const errTitle = "Old SQLite schema found";
const errMessage =
"Old SQLite schema found at file: " +
sqliteFilePath +
"\n" +
"The SQLite schema has been updated since the last time you loaded this file. \n\n" +
"Some features may not work correctly until you re-import the original" +
"ScanCode JSON file to create an updated SQLite database.";
console.error(
"Handled schema mismatch error when importing sqlite file ",
{
title: errTitle,
message: errMessage,
}
);
ipcRenderer.send(OPEN_ERROR_DIALOG_CHANNEL, {
title: errTitle,
message: errMessage,
});
abortImport();
return;
}
updateLoadingStatus(75);
newWorkbenchDB.sync
.then((db) => db.File.findOne({ where: { parent: "#" } }))
.then(async (root) => {
if (!root) {
throw new Error("Root path not found !!");
}
const defaultPath = root.getDataValue("path");
AddEntry({
sqlite_path: sqliteFilePath,
opened_at: moment().format(),
});
await updateWorkbenchDB(newWorkbenchDB, sqliteFilePath);
if (defaultPath) {
updateCurrentPath(
defaultPath,
root.getDataValue("type") as PathType
);
}
// Update window title
const newlyImportedFileName = sqliteFilePath
.split("\\")
.pop()
.split("/")
.pop();
ipcRenderer.send(
UTIL_CHANNEL.SET_CURRENT_FILE_TITLE,
newlyImportedFileName
);
if (!preventNavigation) changeRouteOnImport();
});
})
.catch((err: Error) => {
abortImport();
const foundInvalidHistoryItem = GetHistory().find(
(historyItem) => historyItem.sqlite_path === sqliteFilePath
);
if (foundInvalidHistoryItem) {
RemoveEntry(foundInvalidHistoryItem);
}
console.error("Err trying to import sqlite:", err);
toast.error(
`Sqlite file is outdated or corrupt\nPlease try importing json file again`
);
});
}
function jsonParser(
jsonFilePath: string,
sqliteFilePath: string,
preventNavigation?: boolean
) {
if (!sqliteFilePath || !jsonFilePath) {
console.error("Sqlite or json file path isn't valid:", sqliteFilePath);
return;
}
startImport();
// Create a new database when importing a json file (Delete any existing data in the sqlite file)
const newWorkbenchDB = new WorkbenchDB({
dbName: "workbench_db",
dbStoragePath: sqliteFilePath,
deleteExisting: true,
});
newWorkbenchDB.sync
.then(() =>
newWorkbenchDB.addFromJson(jsonFilePath, (progress: number) => {
updateLoadingStatus(progress);
})
)
.then(() => {
console.log("JSON parsing completed");
AddEntry({
json_path: jsonFilePath,
sqlite_path: sqliteFilePath,
opened_at: moment().format(),
});
newWorkbenchDB.sync
.then((db) => db.File.findOne({ where: { parent: "#" } }))
.then(async (root) => {
if (!root) {
console.error("Root:", root);
throw new Error("Root path not found !!!!");
}
const defaultPath = root.getDataValue("path");
await updateWorkbenchDB(newWorkbenchDB, sqliteFilePath);
if (defaultPath) {
updateCurrentPath(
defaultPath,
root.getDataValue("type") as PathType
);
}
// Update window title
const newlyImportedFileName = jsonFilePath
.split("\\")
.pop()
.split("/")
.pop();
ipcRenderer.send(
UTIL_CHANNEL.SET_CURRENT_FILE_TITLE,
newlyImportedFileName
);
if (!preventNavigation) changeRouteOnImport();
})
.catch((err) => {
abortImport();
const foundInvalidHistoryItem = GetHistory().find(
(historyItem) => historyItem.sqlite_path === sqliteFilePath
);
if (foundInvalidHistoryItem) {
RemoveEntry(foundInvalidHistoryItem);
}
console.error(err);
toast.error(
`Can't resolve root directory \nPlease check console for more info`
);
});
})
.catch((err: Error) => {
abortImport();
if (err.message === NO_RESOURCES_ERROR) {
toast.error("No resources found in scan\nAborting import");
} else {
console.error(
"Some error parsing json data (caught in dbContext)",
err
);
toast.error(
"Some error parsing json data !! \nPlease check console for more info"
);
}
});
}
function importJsonFile(jsonFilePath: string) {
const payload: SQLITE_PATH_FOR_JSON_REQUEST_FORMAT = { jsonFilePath };
ipcRenderer.send(OPEN_DIALOG_CHANNEL.SQLITE_PATH_FOR_JSON, payload);
}
function removeIpcListeners() {
ipcRenderer.removeAllListeners(NAVIGATION_CHANNEL);
ipcRenderer.removeAllListeners(IMPORT_REPLY_CHANNEL.JSON);
ipcRenderer.removeAllListeners(IMPORT_REPLY_CHANNEL.SQLITE);
ipcRenderer.removeAllListeners(SAVE_REPLY_CHANNEL.SQLITE);
ipcRenderer.removeAllListeners(UTIL_CHANNEL.CLOSE_FILE);
}
useEffect(() => {
removeIpcListeners();
ipcRenderer.on(
NAVIGATION_CHANNEL,
(_, message: NAVIGATION_CHANNEL_MESSAGE) => navigate(message)
);
ipcRenderer.on(
IMPORT_REPLY_CHANNEL.JSON,
(_, message: JSON_IMPORT_REPLY_FORMAT) => {
try {
jsonParser(message.jsonFilePath, message.sqliteFilePath);
} catch (err) {
console.log(
`some error importing json - ${message.jsonFilePath}`,
err
);
abortImport();
toast.error(
`Unexpected error while importing json \nPlease check console for more info`
);
}
}
);
ipcRenderer.on(
IMPORT_REPLY_CHANNEL.SQLITE,
(_, message: SQLITE_IMPORT_REPLY_FORMAT) => {
try {
sqliteParser(message.sqliteFilePath);
} catch (err) {
console.log(
`some error importing sqlite - ${message.sqliteFilePath}`,
err
);
abortImport();
toast.error(
`Unexpected error while importing sqlite \nPlease check console for more info`
);
}
}
);
ipcRenderer.on(
SAVE_REPLY_CHANNEL.SQLITE,
(_, message: SQLITE_SAVE_REPLY_FORMAT) => {
if (!value.db || !value.initialized) {
return toast.error(
"No JSON/Sqlite imported to save as new SQLite file",
{
type: "error",
style: { width: 400 },
}
);
}
console.log(
"Save sqlite with info",
message,
value,
value.db?.sequelize
);
const newFileName = message?.sqliteFilePath;
const oldFileName = (
value.db?.sequelize as unknown as { options: { storage: string } }
).options.storage;
if (newFileName && oldFileName) {
const reader = electronFs.createReadStream(oldFileName);
const writer = electronFs.createWriteStream(newFileName);
reader.pipe(writer);
reader.on("end", () => {
console.log("Saved", newFileName);
toast.success("Saved sqlite file, loading from new file");
sqliteParser(newFileName, true);
});
}
}
);
ipcRenderer.on(UTIL_CHANNEL.CLOSE_FILE, closeFile);
// Remove all listeners on window unmount
return () => {
removeIpcListeners();
};
}, [value]);
return (
<WorkbenchContext.Provider
value={{
...value,
currentPath,
currentPathType,
loadingStatus,
processingQuery,
updateLoadingStatus,
jsonParser,
sqliteParser,
importJsonFile,
startImport,
abortImport,
closeFile,
startProcessing: () => setProcessingQuery(true),
endProcessing: () => setProcessingQuery(false),
updateCurrentPath,
goToFileInTableView,
goToLicenseDetection,
goToPackage,
}}
>
{props.children}
</WorkbenchContext.Provider>
);
};
export const useWorkbenchDB = () => useContext(WorkbenchContext);