From 2051ccee46a1ff45e6426412992b6af10f8bd906 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 17 Apr 2024 14:48:03 +0530 Subject: [PATCH] List files alternate --- desktop/src/main/fs.ts | 5 +++++ desktop/src/main/ipc.ts | 3 +++ desktop/src/preload.ts | 4 ++++ web/packages/next/types/ipc.ts | 12 ++++++++++++ 4 files changed, 24 insertions(+) diff --git a/desktop/src/main/fs.ts b/desktop/src/main/fs.ts index 2428d3a80..ebf53487c 100644 --- a/desktop/src/main/fs.ts +++ b/desktop/src/main/fs.ts @@ -27,3 +27,8 @@ export const fsIsDir = async (dirPath: string) => { const stat = await fs.stat(dirPath); return stat.isDirectory(); }; + +export const fsLsFiles = async (dirPath: string) => + (await fs.readdir(dirPath, { withFileTypes: true })) + .filter((e) => e.isFile()) + .map((e) => e.name); diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts index e307ad167..2e01b4a10 100644 --- a/desktop/src/main/ipc.ts +++ b/desktop/src/main/ipc.ts @@ -20,6 +20,7 @@ import { import { fsExists, fsIsDir, + fsLsFiles, fsMkdirIfNeeded, fsReadTextFile, fsRename, @@ -134,6 +135,8 @@ export const attachIPCHandlers = () => { ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath)); + ipcMain.handle("fsLsFiles", (_, dirPath: string) => fsLsFiles(dirPath)); + // - Conversion ipcMain.handle("convertToJPEG", (_, fileData, filename) => diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index 9341f02f2..2c2677cf1 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -121,6 +121,9 @@ const fsWriteFile = (path: string, contents: string): Promise => const fsIsDir = (dirPath: string): Promise => ipcRenderer.invoke("fsIsDir", dirPath); +const fsLsFiles = (dirPath: string): Promise => + ipcRenderer.invoke("fsLsFiles", dirPath); + // - AUDIT below this // - Conversion @@ -322,6 +325,7 @@ contextBridge.exposeInMainWorld("electron", { readTextFile: fsReadTextFile, writeFile: fsWriteFile, isDir: fsIsDir, + lsFiles: fsLsFiles, }, // - Conversion diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts index 348f815bb..55e7fca3f 100644 --- a/web/packages/next/types/ipc.ts +++ b/web/packages/next/types/ipc.ts @@ -205,6 +205,18 @@ export interface Electron { * directory. */ isDir: (dirPath: string) => Promise; + + /** + * Return a list of the names of the files in the given directory. + * + * Note: + * + * - This is not recursive, it will only return the names of direct + * children. + * + * - It will return only the names of files, not directories. + */ + lsFiles: (dirPath: string) => Promise; }; /*