Move checkExistsAndCreateDir out of preload

This commit is contained in:
Manav Rathi 2024-03-23 20:05:37 +05:30
parent f21dc84840
commit 81ba5379c9
No known key found for this signature in database
4 changed files with 23 additions and 8 deletions

View file

@ -2,5 +2,11 @@
* @file file system related functions exposed over the context bridge.
*/
import { existsSync } from "node:fs";
import * as fs from "node:fs/promises";
export const fsExists = (path: string) => existsSync(path);
/* TODO: Audit below this */
export const checkExistsAndCreateDir = (dirPath: string) =>
fs.mkdir(dirPath, { recursive: true });

View file

@ -8,11 +8,9 @@
import { ipcMain } from "electron/main";
import { appVersion } from "../services/appUpdater";
import { checkExistsAndCreateDir, fsExists } from "./fs";
import { openDirectory, openLogDirectory } from "./general";
import { logToDisk } from "./log";
import { fsExists } from "./fs";
// - General
export const attachIPCHandlers = () => {
// Notes:
@ -40,7 +38,11 @@ export const attachIPCHandlers = () => {
ipcMain.handle("openLogDirectory", (_) => openLogDirectory());
// See: [Note: Catching exception during .send/.on]
ipcMain.on("logToDisk", (_, msg) => logToDisk(msg));
ipcMain.on("logToDisk", (_, message) => logToDisk(message));
ipcMain.handle("fsExists", (_, path) => fsExists(path));
ipcMain.handle("checkExistsAndCreateDir", (_, dirPath) =>
checkExistsAndCreateDir(dirPath),
);
};

View file

@ -95,6 +95,11 @@ const logToDisk = (message: string): void =>
const fsExists = (path: string): Promise<boolean> =>
ipcRenderer.invoke("fsExists", path);
// - AUDIT below this
const checkExistsAndCreateDir = (dirPath: string): Promise<void> =>
ipcRenderer.invoke("checkExistsAndCreateDir", dirPath);
// - FIXME below this
/* preload: duplicated logError */
@ -168,9 +173,6 @@ const writeNodeStream = async (
// - Export
const checkExistsAndCreateDir = (dirPath: string) =>
fs.mkdir(dirPath, { recursive: true });
const saveStreamToDisk = writeStream;
const saveFileToDisk = (path: string, contents: string) =>
@ -439,8 +441,11 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
exists: fsExists,
},
// - Export
// - FS legacy
// TODO: Move these into fs + document + rename if needed
checkExistsAndCreateDir,
// - Export
saveStreamToDisk,
saveFileToDisk,

View file

@ -80,6 +80,8 @@ export interface ElectronAPIsType {
/** TODO: AUDIT below this */
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
/** TODO: FIXME or migrate below this */
saveStreamToDisk: (
path: string,
fileStream: ReadableStream<any>,