Browse Source

List files alternate

Manav Rathi 1 year ago
parent
commit
2051ccee46
4 changed files with 24 additions and 0 deletions
  1. 5 0
      desktop/src/main/fs.ts
  2. 3 0
      desktop/src/main/ipc.ts
  3. 4 0
      desktop/src/preload.ts
  4. 12 0
      web/packages/next/types/ipc.ts

+ 5 - 0
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);

+ 3 - 0
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) =>

+ 4 - 0
desktop/src/preload.ts

@@ -121,6 +121,9 @@ const fsWriteFile = (path: string, contents: string): Promise<void> =>
 const fsIsDir = (dirPath: string): Promise<boolean> =>
     ipcRenderer.invoke("fsIsDir", dirPath);
 
+const fsLsFiles = (dirPath: string): Promise<boolean> =>
+    ipcRenderer.invoke("fsLsFiles", dirPath);
+
 // - AUDIT below this
 
 // - Conversion
@@ -322,6 +325,7 @@ contextBridge.exposeInMainWorld("electron", {
         readTextFile: fsReadTextFile,
         writeFile: fsWriteFile,
         isDir: fsIsDir,
+        lsFiles: fsLsFiles,
     },
 
     // - Conversion

+ 12 - 0
web/packages/next/types/ipc.ts

@@ -205,6 +205,18 @@ export interface Electron {
          * directory.
          */
         isDir: (dirPath: string) => Promise<boolean>;
+
+        /**
+         * 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<string>;
     };
 
     /*