Skip to content

Commit dc1dba8

Browse files
committed
wip
1 parent fb234e7 commit dc1dba8

File tree

6 files changed

+197
-1
lines changed

6 files changed

+197
-1
lines changed

resources/js/electron-plugin/dist/server/api.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import settingsRoutes from "./api/settings";
3030
import shellRoutes from "./api/shell";
3131
import progressBarRoutes from "./api/progressBar";
3232
import powerMonitorRoutes from "./api/powerMonitor";
33+
import childProcessRoutes from "./api/childProcess";
3334
function startAPIServer(randomSecret) {
3435
return __awaiter(this, void 0, void 0, function* () {
3536
const port = yield getPort({
@@ -56,6 +57,7 @@ function startAPIServer(randomSecret) {
5657
httpServer.use("/api/menu-bar", menuBarRoutes);
5758
httpServer.use("/api/progress-bar", progressBarRoutes);
5859
httpServer.use("/api/power-monitor", powerMonitorRoutes);
60+
httpServer.use("/api/child-process", childProcessRoutes);
5961
httpServer.use("/api/broadcast", broadcastingRoutes);
6062
if (process.env.NODE_ENV === "development") {
6163
httpServer.use("/api/debug", debugRoutes);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import express from 'express';
2+
import { utilityProcess } from 'electron';
3+
import state from '../state';
4+
import { notifyLaravel } from "../utils";
5+
const router = express.Router();
6+
router.post('/start', (req, res) => {
7+
const { alias, cmd, args, cwd, env } = req.body;
8+
console.log(req.body);
9+
if (state.processes[alias] !== null) {
10+
res.sendStatus(409);
11+
return;
12+
}
13+
const proc = utilityProcess.fork(cmd, args || null, {
14+
env: env || null,
15+
cwd: cwd || null,
16+
serviceName: alias,
17+
});
18+
console.log(proc);
19+
proc.stdout.on('data', (data) => {
20+
console.log('Message received from process [' + alias + ']:', data);
21+
notifyLaravel('events', {
22+
event: 'Native\\Laravel\\Events\\ChildProcess\\MessageReceived',
23+
payload: {
24+
alias,
25+
data,
26+
}
27+
});
28+
});
29+
proc.stderr.on('data', (data) => {
30+
console.log('Error received from process [' + alias + ']:', data);
31+
notifyLaravel('events', {
32+
event: 'Native\\Laravel\\Events\\ChildProcess\\ErrorReceived',
33+
payload: {
34+
alias,
35+
data,
36+
}
37+
});
38+
});
39+
proc.on('spawn', () => {
40+
console.log('Process [' + alias + '] spawned!');
41+
notifyLaravel('events', {
42+
event: 'Native\\Laravel\\Events\\ChildProcess\\ProcessSpawned',
43+
payload: [alias]
44+
});
45+
});
46+
proc.on('exit', (code) => {
47+
console.log('Process [' + alias + '] exited!');
48+
notifyLaravel('events', {
49+
event: 'Native\\Laravel\\Events\\ChildProcess\\ProcessExited',
50+
payload: {
51+
alias,
52+
code,
53+
}
54+
});
55+
});
56+
state.processes[alias] = proc;
57+
res.json(proc);
58+
});
59+
router.post('/stop', (req, res) => {
60+
const { alias } = req.body;
61+
const proc = state.processes[alias];
62+
if (proc === null) {
63+
res.sendStatus(200);
64+
return;
65+
}
66+
if (proc.kill()) {
67+
delete state.processes[alias];
68+
}
69+
res.sendStatus(200);
70+
});
71+
router.post('/message', (req, res) => {
72+
const { alias, message } = req.body;
73+
const proc = state.processes[alias];
74+
if (proc === null) {
75+
res.sendStatus(200);
76+
return;
77+
}
78+
proc.postMessage(message);
79+
res.sendStatus(200);
80+
});
81+
export default router;

resources/js/electron-plugin/dist/server/state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default {
3232
icon: null,
3333
store: settingsStore,
3434
randomSecret: generateRandomString(32),
35+
processes: {},
3536
windows: {},
3637
findWindow(id) {
3738
return this.windows[id] || null;

resources/js/electron-plugin/src/server/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import settingsRoutes from "./api/settings";
2222
import shellRoutes from "./api/shell";
2323
import progressBarRoutes from "./api/progressBar";
2424
import powerMonitorRoutes from "./api/powerMonitor";
25+
import childProcessRoutes from "./api/childProcess";
2526
import { Server } from "net";
2627

2728
export interface APIProcess {
@@ -55,6 +56,7 @@ async function startAPIServer(randomSecret: string): Promise<APIProcess> {
5556
httpServer.use("/api/menu-bar", menuBarRoutes);
5657
httpServer.use("/api/progress-bar", progressBarRoutes);
5758
httpServer.use("/api/power-monitor", powerMonitorRoutes);
59+
httpServer.use("/api/child-process", childProcessRoutes);
5860
httpServer.use("/api/broadcast", broadcastingRoutes);
5961

6062
if (process.env.NODE_ENV === "development") {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import express from 'express';
2+
import { utilityProcess } from 'electron';
3+
import state from '../state';
4+
import { notifyLaravel } from "../utils";
5+
6+
const router = express.Router();
7+
8+
router.post('/start', (req, res) => {
9+
const {alias, cmd, args, cwd, env} = req.body
10+
11+
console.log(req.body)
12+
13+
if (state.processes[alias] !== null) {
14+
res.sendStatus(409)
15+
return
16+
}
17+
18+
const proc = utilityProcess.fork(
19+
cmd,
20+
args || null,
21+
{
22+
env: env || null,
23+
cwd: cwd || null,
24+
serviceName: alias,
25+
}
26+
)
27+
28+
console.log(proc)
29+
30+
proc.stdout.on('data', (data) => {
31+
console.log('Message received from process [' + alias + ']:', data)
32+
notifyLaravel('events', {
33+
event: 'Native\\Laravel\\Events\\ChildProcess\\MessageReceived',
34+
payload: {
35+
alias,
36+
data,
37+
}
38+
})
39+
})
40+
41+
proc.stderr.on('data', (data) => {
42+
console.log('Error received from process [' + alias + ']:', data)
43+
notifyLaravel('events', {
44+
event: 'Native\\Laravel\\Events\\ChildProcess\\ErrorReceived',
45+
payload: {
46+
alias,
47+
data,
48+
}
49+
})
50+
})
51+
52+
proc.on('spawn', () => {
53+
console.log('Process [' + alias + '] spawned!')
54+
notifyLaravel('events', {
55+
event: 'Native\\Laravel\\Events\\ChildProcess\\ProcessSpawned',
56+
payload: [alias]
57+
})
58+
})
59+
60+
proc.on('exit', (code) => {
61+
console.log('Process [' + alias + '] exited!')
62+
notifyLaravel('events', {
63+
event: 'Native\\Laravel\\Events\\ChildProcess\\ProcessExited',
64+
payload: {
65+
alias,
66+
code,
67+
}
68+
})
69+
})
70+
71+
state.processes[alias] = proc
72+
73+
res.json(proc)
74+
});
75+
76+
router.post('/stop', (req, res) => {
77+
const {alias} = req.body
78+
79+
const proc = state.processes[alias]
80+
81+
if (proc === null) {
82+
res.sendStatus(200)
83+
return
84+
}
85+
86+
if (proc.kill()) {
87+
delete state.processes[alias]
88+
}
89+
90+
res.sendStatus(200)
91+
});
92+
93+
router.post('/message', (req, res) => {
94+
const {alias, message} = req.body
95+
96+
const proc = state.processes[alias]
97+
98+
if (proc === null) {
99+
res.sendStatus(200)
100+
return
101+
}
102+
103+
proc.postMessage(message)
104+
105+
res.sendStatus(200)
106+
});
107+
108+
export default router;

resources/js/electron-plugin/src/server/state.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BrowserWindow } from "electron";
1+
import { BrowserWindow, UtilityProcess } from "electron";
22
import Store from "electron-store";
33
import { notifyLaravel } from "./utils";
44

@@ -28,6 +28,7 @@ interface State {
2828
phpIni: any;
2929
caCert: string | null;
3030
icon: string | null;
31+
processes: Record<string, UtilityProcess>;
3132
windows: Record<string, BrowserWindow>;
3233
randomSecret: string;
3334
store: Store;
@@ -57,6 +58,7 @@ export default {
5758
icon: null,
5859
store: settingsStore,
5960
randomSecret: generateRandomString(32),
61+
processes: {},
6062
windows: {},
6163
findWindow(id: string) {
6264
return this.windows[id] || null;

0 commit comments

Comments
 (0)