From 81ba5379c9c3afb64961207bffb6f815242eee54 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 23 Mar 2024 20:05:37 +0530 Subject: [PATCH] Move checkExistsAndCreateDir out of preload --- desktop/src/main/fs.ts | 6 ++++++ desktop/src/main/ipc.ts | 10 ++++++---- desktop/src/preload.ts | 13 +++++++++---- web/packages/shared/electron/types.ts | 2 ++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/desktop/src/main/fs.ts b/desktop/src/main/fs.ts index 50a9ddc61..12527d551 100644 --- a/desktop/src/main/fs.ts +++ b/desktop/src/main/fs.ts @@ -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 }); diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts index 83726fc8d..aa3841d2d 100644 --- a/desktop/src/main/ipc.ts +++ b/desktop/src/main/ipc.ts @@ -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), + ); }; diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index b81a7a35e..dad8b95c7 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -95,6 +95,11 @@ const logToDisk = (message: string): void => const fsExists = (path: string): Promise => ipcRenderer.invoke("fsExists", path); +// - AUDIT below this + +const checkExistsAndCreateDir = (dirPath: string): Promise => + 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, diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.ts index 3cb519d7a..81a16d593 100644 --- a/web/packages/shared/electron/types.ts +++ b/web/packages/shared/electron/types.ts @@ -80,6 +80,8 @@ export interface ElectronAPIsType { /** TODO: AUDIT below this */ checkExistsAndCreateDir: (dirPath: string) => Promise; + + /** TODO: FIXME or migrate below this */ saveStreamToDisk: ( path: string, fileStream: ReadableStream,