From bff58795af5844e602503c37e998ecaa08465633 Mon Sep 17 00:00:00 2001
From: ali asaria <aliasaria@users.noreply.github.com>
Date: Wed, 8 Jan 2025 13:00:29 -0500
Subject: [PATCH] revert switching to chokidar

---
 src/main/main.ts | 48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/src/main/main.ts b/src/main/main.ts
index 4394a038..8767c56b 100644
--- a/src/main/main.ts
+++ b/src/main/main.ts
@@ -35,14 +35,14 @@ import {
   checkDependencies,
   checkIfCondaBinExists,
   getLogFilePath,
-  isPlatformWindows,
 } from './util';
 
 import installExtension, {
   REACT_DEVELOPER_TOOLS,
 } from 'electron-devtools-installer';
 
-import FileWatcher from './file-watcher';
+const fs = require('fs');
+const Tail = require('tail').Tail;
 
 // ////////////
 // STORAGE
@@ -138,20 +138,18 @@ console.log('setting up listening to log file');
 const startListeningToServerLog = async () => {
   // Now listen to the log file and send updates to the renderer
   const logFile = await getLogFilePath();
-
-  // console.log(`🤡 Asking Chokidar to start`);
-
-  let options = {};
-
-  // if this is windows, we start FileWatcher with option.usePolling = true, otherwise we use the default
-  // and don't provide options.
-  // This is because in Windows, the engine runs in WSL2 but the app that watches the logs is running in Windows
-  // and they can't communicate using iNotify directly. (So we poll only in this situation)
-  if (isPlatformWindows()) {
-    options = { usePolling: true };
+  //create the file if it doesn't exist:
+  if (!fs.existsSync(logFile)) {
+    // first make the directory:
+    const logDir = path.dirname(logFile);
+    if (!fs.existsSync(logDir)) {
+      fs.mkdirSync(logDir, { recursive: true });
+    }
+    fs.writeFileSync(logFile, '');
   }
+  let tail = new Tail(logFile);
 
-  const watcher = new FileWatcher(logFile, options);
+  let currentlySubscribed = false;
 
   ipcMain.on('serverLog:startListening', async (event) => {
     console.log('main.js: start listening to log');
@@ -159,11 +157,26 @@ const startListeningToServerLog = async () => {
       'serverLog:update',
       '**Connecting to Terminal Output from Transformer Engine**'
     );
-    watcher.start();
+    if (!tail.isWatching) {
+      tail.watch();
+    }
+    console.log('logFile', logFile);
+    if (currentlySubscribed) {
+      console.log('already watching');
+      return;
+    }
+
+    currentlySubscribed = true;
+    tail = new Tail(logFile);
 
-    watcher.on('update', (data) => {
+    tail.on('line', function (data) {
+      // console.log('main.js: line', data);
       event.reply('serverLog:update', data);
     });
+
+    tail.on('error', function (error) {
+      console.log('ERROR: ', error);
+    });
   });
 
   ipcMain.on('serverLog:stopListening', async (event) => {
@@ -172,7 +185,8 @@ const startListeningToServerLog = async () => {
       'serverLog:update',
       '**Disconnecting Terminal Output from Transformer Engine**'
     );
-    watcher.stop();
+    tail.unwatch();
+    currentlySubscribed = false;
   });
 };
 
-- 
GitLab