From c787b4c44f1afe6f834468f9a5aaccb4e1a00708 Mon Sep 17 00:00:00 2001
From: A G <fdwnct@gmail.com>
Date: Wed, 1 Jan 2025 20:16:49 -0500
Subject: [PATCH] Project file watching & event dispatcher for development
 environments

---
 resources/js/electron-plugin/dist/index.js | 18 ++++++++++++++++++
 resources/js/electron-plugin/src/index.ts  | 22 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/resources/js/electron-plugin/dist/index.js b/resources/js/electron-plugin/dist/index.js
index a34180bf..f4212f48 100644
--- a/resources/js/electron-plugin/dist/index.js
+++ b/resources/js/electron-plugin/dist/index.js
@@ -16,7 +16,9 @@ import { notifyLaravel } from "./server/utils.js";
 import { resolve } from "path";
 import { stopAllProcesses } from "./server/api/childProcess.js";
 import ps from "ps-node";
+import { watch } from "fs";
 import electronUpdater from 'electron-updater';
+import { getAppPath } from "./server/php.js";
 const { autoUpdater } = electronUpdater;
 class NativePHP {
     constructor() {
@@ -78,6 +80,9 @@ class NativePHP {
             state.phpIni = yield this.loadPhpIni();
             yield this.startPhpApp();
             this.startScheduler();
+            if (process.env.NODE_ENV === "development") {
+                this.watchPhpChanges();
+            }
             yield notifyLaravel("booted");
         });
     }
@@ -173,5 +178,18 @@ class NativePHP {
             }
         });
     }
+    watchPhpChanges() {
+        const appPath = getAppPath();
+        watch(appPath, { recursive: true }, (eventType, filename) => {
+            if (filename && filename.endsWith('.php')) {
+                console.log(`PHP file changed: ${filename} (${eventType})`);
+                notifyLaravel('events', {
+                    event: '\\Native\\Laravel\\Events\\App\\ProjectFileChanged',
+                    payload: [filename],
+                });
+            }
+        });
+        console.log(`Watching for PHP file changes in: ${appPath}`);
+    }
 }
 export default new NativePHP();
diff --git a/resources/js/electron-plugin/src/index.ts b/resources/js/electron-plugin/src/index.ts
index 191c5d9c..69ce3964 100644
--- a/resources/js/electron-plugin/src/index.ts
+++ b/resources/js/electron-plugin/src/index.ts
@@ -14,9 +14,11 @@ import { notifyLaravel } from "./server/utils.js";
 import { resolve } from "path";
 import { stopAllProcesses } from "./server/api/childProcess.js";
 import ps from "ps-node";
+import {watch} from "fs";
 
 // Workaround for CommonJS module
 import electronUpdater from 'electron-updater';
+import {getAppPath} from "./server/php.js";
 const { autoUpdater } = electronUpdater;
 
 class NativePHP {
@@ -107,6 +109,10 @@ class NativePHP {
     await this.startPhpApp();
     this.startScheduler();
 
+    if (process.env.NODE_ENV === "development") {
+        this.watchPhpChanges();
+    }
+
     await notifyLaravel("booted");
   }
 
@@ -216,6 +222,22 @@ class NativePHP {
         }
       });
   }
+
+  private watchPhpChanges() {
+      const appPath = getAppPath();
+
+      watch(appPath, { recursive: true }, (eventType, filename) => {
+          if (filename && filename.endsWith('.php')) {
+              console.log(`PHP file changed: ${filename} (${eventType})`);
+              notifyLaravel('events', {
+                  event: '\\Native\\Laravel\\Events\\App\\ProjectFileChanged',
+                  payload: [filename],
+              });
+          }
+      });
+
+      console.log(`Watching for PHP file changes in: ${appPath}`);
+  }
 }
 
 export default new NativePHP();