Skip to content

Commit

Permalink
Project file watching & event dispatcher for development environments
Browse files Browse the repository at this point in the history
  • Loading branch information
XbNz committed Jan 2, 2025
1 parent 9c9d369 commit c787b4c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions resources/js/electron-plugin/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
});
}
Expand Down Expand Up @@ -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();
22 changes: 22 additions & 0 deletions resources/js/electron-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -107,6 +109,10 @@ class NativePHP {
await this.startPhpApp();
this.startScheduler();

if (process.env.NODE_ENV === "development") {
this.watchPhpChanges();
}

await notifyLaravel("booted");
}

Expand Down Expand Up @@ -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();

0 comments on commit c787b4c

Please sign in to comment.