Manav Rathi 1 year ago
parent
commit
f19b90d0ae
3 changed files with 54 additions and 50 deletions
  1. 1 1
      desktop/src/main.ts
  2. 0 45
      desktop/src/main/services/chokidar.ts
  3. 53 4
      desktop/src/main/services/watch.ts

+ 1 - 1
desktop/src/main.ts

@@ -25,7 +25,7 @@ import log, { initLogging } from "./main/log";
 import { createApplicationMenu, createTrayContextMenu } from "./main/menu";
 import { setupAutoUpdater } from "./main/services/app-update";
 import autoLauncher from "./main/services/auto-launcher";
-import { initWatcher } from "./main/services/chokidar";
+import { initWatcher } from "./main/services/watch";
 import { userPreferences } from "./main/stores/user-preferences";
 import { registerStreamProtocol } from "./main/stream";
 import { isDev } from "./main/util";

+ 0 - 45
desktop/src/main/services/chokidar.ts

@@ -1,45 +0,0 @@
-import chokidar from "chokidar";
-import { BrowserWindow } from "electron/main";
-import path from "path";
-import log from "../log";
-import { getElectronFile } from "./fs";
-import { getWatchMappings } from "./watch";
-
-/**
- * Convert a file system {@link filePath} that uses the local system specific
- * path separators into a path that uses POSIX file separators.
- */
-const normalizeToPOSIX = (filePath: string) =>
-    filePath.split(path.sep).join(path.posix.sep);
-
-export function initWatcher(mainWindow: BrowserWindow) {
-    const mappings = getWatchMappings();
-    const folderPaths = mappings.map((mapping) => {
-        return mapping.folderPath;
-    });
-
-    const watcher = chokidar.watch(folderPaths, {
-        awaitWriteFinish: true,
-    });
-    watcher
-        .on("add", async (path) => {
-            mainWindow.webContents.send(
-                "watch-add",
-                await getElectronFile(normalizeToPOSIX(path)),
-            );
-        })
-        .on("unlink", (path) => {
-            mainWindow.webContents.send("watch-unlink", normalizeToPOSIX(path));
-        })
-        .on("unlinkDir", (path) => {
-            mainWindow.webContents.send(
-                "watch-unlink-dir",
-                normalizeToPOSIX(path),
-            );
-        })
-        .on("error", (error) => {
-            log.error("Error while watching files", error);
-        });
-
-    return watcher;
-}

+ 53 - 4
desktop/src/main/services/watch.ts

@@ -1,9 +1,58 @@
-import type { FSWatcher } from "chokidar";
-import ElectronLog from "electron-log";
+import chokidar, { type FSWatcher } from "chokidar";
+import { BrowserWindow } from "electron/main";
 import fs from "node:fs/promises";
 import path from "node:path";
-import { FolderWatch, WatchStoreType } from "../../types/ipc";
+import { FolderWatch } from "../../types/ipc";
+import log from "../log";
 import { watchStore } from "../stores/watch";
+import { getElectronFile } from "./fs";
+
+/**
+ * Create and return a new file system watcher.
+ *
+ * Internally this uses the watcher from the chokidar package.
+ *
+ * @param mainWindow The window handle is used to notify the renderer process of
+ * pertinent file system events.
+ */
+export const createWatcher = (mainWindow: BrowserWindow) => {
+    const mappings = getWatchMappings();
+    const folderPaths = mappings.map((mapping) => {
+        return mapping.folderPath;
+    });
+
+    const watcher = chokidar.watch(folderPaths, {
+        awaitWriteFinish: true,
+    });
+    watcher
+        .on("add", async (path) => {
+            mainWindow.webContents.send(
+                "watch-add",
+                await getElectronFile(normalizeToPOSIX(path)),
+            );
+        })
+        .on("unlink", (path) => {
+            mainWindow.webContents.send("watch-unlink", normalizeToPOSIX(path));
+        })
+        .on("unlinkDir", (path) => {
+            mainWindow.webContents.send(
+                "watch-unlink-dir",
+                normalizeToPOSIX(path),
+            );
+        })
+        .on("error", (error) => {
+            log.error("Error while watching files", error);
+        });
+
+    return watcher;
+};
+
+/**
+ * Convert a file system {@link filePath} that uses the local system specific
+ * path separators into a path that uses POSIX file separators.
+ */
+const normalizeToPOSIX = (filePath: string) =>
+    filePath.split(path.sep).join(path.posix.sep);
 
 export const findFiles = async (dirPath: string) => {
     const items = await fs.readdir(dirPath, { withFileTypes: true });
@@ -25,7 +74,7 @@ export const addWatchMapping = async (
     folderPath: string,
     uploadStrategy: number,
 ) => {
-    ElectronLog.log(`Adding watch mapping: ${folderPath}`);
+    log.info(`Adding watch mapping: ${folderPath}`);
     const watchMappings = getWatchMappings();
     if (isMappingPresent(watchMappings, folderPath)) {
         throw new Error(`Watch mapping already exists`);