Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project file watching & event dispatcher for development environments #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading