Skip to content

Integrate Plugin #109

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

Merged
merged 5 commits into from
Oct 18, 2024
Merged
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
23 changes: 23 additions & 0 deletions .github/workflows/run-plugin-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: plugin-tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 18.x
- name: Install dependencies
working-directory: ./resources/js/
run: npm install
- name: Run tests
working-directory: ./resources/js/
run: npm run plugin:test
18 changes: 18 additions & 0 deletions resources/js/electron-plugin/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"env": {
"browser": true,
"node": true
},
"rules": {
"prettier/prettier": "error"
}
}
25 changes: 25 additions & 0 deletions resources/js/electron-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# production
build
dist-ssr
*.local

# logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
coverage
18 changes: 18 additions & 0 deletions resources/js/electron-plugin/.stylelintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": [
"stylelint-config-recommended",
"stylelint-config-sass-guidelines"
],
"overrides": [
{
"files": ["**/*.scss"],
"customSyntax": "postcss-scss"
}
],
"rules": {
"function-parentheses-space-inside": null,
"no-descending-specificity": null,
"max-nesting-depth": 2,
"selector-max-id": 1
}
}
5 changes: 5 additions & 0 deletions resources/js/electron-plugin/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@babel/preset-typescript',
],
};
185 changes: 185 additions & 0 deletions resources/js/electron-plugin/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { app } from "electron";
import { autoUpdater } from "electron-updater";
import state from "./server/state";
import { electronApp, optimizer } from "@electron-toolkit/utils";
import { retrieveNativePHPConfig, retrievePhpIniSettings, runScheduler, startAPI, startPhpApp, startQueue, startWebsockets, } from "./server";
import { notifyLaravel } from "./server/utils";
import { resolve } from "path";
import ps from "ps-node";
class NativePHP {
constructor() {
this.processes = [];
this.schedulerInterval = undefined;
}
bootstrap(app, icon, phpBinary, cert) {
require("@electron/remote/main").initialize();
state.icon = icon;
state.php = phpBinary;
state.caCert = cert;
this.bootstrapApp(app);
this.addEventListeners(app);
}
addEventListeners(app) {
app.on("open-url", (event, url) => {
notifyLaravel("events", {
event: "\\Native\\Laravel\\Events\\App\\OpenedFromURL",
payload: [url],
});
});
app.on("open-file", (event, path) => {
notifyLaravel("events", {
event: "\\Native\\Laravel\\Events\\App\\OpenFile",
payload: [path],
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("before-quit", () => {
if (this.schedulerInterval) {
clearInterval(this.schedulerInterval);
}
this.killChildProcesses();
});
app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window);
});
app.on("activate", function (event, hasVisibleWindows) {
if (!hasVisibleWindows) {
notifyLaravel("booted");
}
event.preventDefault();
});
}
bootstrapApp(app) {
return __awaiter(this, void 0, void 0, function* () {
yield app.whenReady();
const config = yield this.loadConfig();
this.setDockIcon();
this.setAppUserModelId(config);
this.setDeepLinkHandler(config);
this.startAutoUpdater(config);
yield this.startElectronApi();
state.phpIni = yield this.loadPhpIni();
yield this.startPhpApp();
yield this.startQueueWorker();
yield this.startWebsockets();
this.startScheduler();
yield notifyLaravel("booted");
});
}
loadConfig() {
return __awaiter(this, void 0, void 0, function* () {
let config = {};
try {
const result = yield retrieveNativePHPConfig();
config = JSON.parse(result.stdout);
}
catch (error) {
console.error(error);
}
return config;
});
}
setDockIcon() {
if (process.platform === "darwin" &&
process.env.NODE_ENV === "development") {
app.dock.setIcon(state.icon);
}
}
setAppUserModelId(config) {
electronApp.setAppUserModelId(config === null || config === void 0 ? void 0 : config.app_id);
}
setDeepLinkHandler(config) {
const deepLinkProtocol = config === null || config === void 0 ? void 0 : config.deeplink_scheme;
if (deepLinkProtocol) {
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(deepLinkProtocol, process.execPath, [
resolve(process.argv[1]),
]);
}
}
else {
app.setAsDefaultProtocolClient(deepLinkProtocol);
}
}
}
startAutoUpdater(config) {
var _a;
if (((_a = config === null || config === void 0 ? void 0 : config.updater) === null || _a === void 0 ? void 0 : _a.enabled) === true) {
autoUpdater.checkForUpdatesAndNotify();
}
}
startElectronApi() {
return __awaiter(this, void 0, void 0, function* () {
const electronApi = yield startAPI();
state.electronApiPort = electronApi.port;
console.log("Electron API server started on port", electronApi.port);
});
}
loadPhpIni() {
return __awaiter(this, void 0, void 0, function* () {
let config = {};
try {
const result = yield retrievePhpIniSettings();
config = JSON.parse(result.stdout);
}
catch (error) {
console.error(error);
}
return config;
});
}
startPhpApp() {
return __awaiter(this, void 0, void 0, function* () {
this.processes.push(yield startPhpApp());
});
}
startQueueWorker() {
return __awaiter(this, void 0, void 0, function* () {
this.processes.push(yield startQueue());
});
}
startWebsockets() {
return __awaiter(this, void 0, void 0, function* () {
this.processes.push(yield startWebsockets());
});
}
startScheduler() {
const now = new Date();
const delay = (60 - now.getSeconds()) * 1000 + (1000 - now.getMilliseconds());
setTimeout(() => {
console.log("Running scheduler...");
runScheduler();
this.schedulerInterval = setInterval(() => {
console.log("Running scheduler...");
runScheduler();
}, 60 * 1000);
}, delay);
}
killChildProcesses() {
this.processes
.filter((p) => p !== undefined)
.forEach((process) => {
try {
ps.kill(process.pid);
}
catch (err) {
console.error(err);
}
});
}
}
export default new NativePHP();
40 changes: 40 additions & 0 deletions resources/js/electron-plugin/dist/preload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ipcRenderer } from 'electron';
import * as remote from '@electron/remote';
import Native from './native';
window.Native = Native;
window.remote = remote;
ipcRenderer.on('log', (event, { level, message, context }) => {
if (level === 'error') {
console.error(`[${level}] ${message}`, context);
}
else if (level === 'warn') {
console.warn(`[${level}] ${message}`, context);
}
else {
console.log(`[${level}] ${message}`, context);
}
});
ipcRenderer.on('native-event', (event, data) => {
data.event = data.event.replace(/^(\\)+/, '');
if (window.Livewire) {
window.Livewire.dispatch('native:' + data.event, data.payload);
}
if (window.livewire) {
window.livewire.components.components().forEach(component => {
if (Array.isArray(component.listeners)) {
component.listeners.forEach(event => {
if (event.startsWith('native')) {
let event_parts = event.split(/(native:|native-)|:|,/);
if (event_parts[1] == 'native:') {
event_parts.splice(2, 0, 'private', undefined, 'nativephp', undefined);
}
let [s1, signature, channel_type, s2, channel, s3, event_name,] = event_parts;
if (data.event === event_name) {
window.livewire.emit(event, data.payload);
}
}
});
}
});
}
});
12 changes: 12 additions & 0 deletions resources/js/electron-plugin/dist/preload/native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ipcRenderer } from 'electron';
export default {
on: (event, callback) => {
ipcRenderer.on('native-event', (_, data) => {
event = event.replace(/^(\\)+/, '');
data.event = data.event.replace(/^(\\)+/, '');
if (event === data.event) {
return callback(data.payload, event);
}
});
}
};
1 change: 1 addition & 0 deletions resources/js/electron-plugin/dist/server/ProcessResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Loading
Loading