@vnejs/desktop 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/desktop",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Tools to run desktop app of @vnejs",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -19,6 +19,7 @@
19
19
  "commander": "13.1.0",
20
20
  "express": "4.21.2",
21
21
  "get-port": "5.1.1",
22
+ "electron": "30.3.1",
22
23
  "open": "10.1.0"
23
24
  }
24
25
  }
@@ -0,0 +1 @@
1
+ require("./electron")();
package/src/electron.js CHANGED
@@ -1,24 +1,11 @@
1
- const { app, BrowserWindow } = require("electron");
1
+ const electron = require("electron");
2
+ const proc = require("node:child_process");
2
3
 
3
4
  const { runServer } = require("./server");
4
- let mainWindow;
5
+ const { runElectron } = require("./electron.utils");
5
6
 
6
- const isProd = process.env.NODE_ENV === "production";
7
+ module.exports = () => {
8
+ proc.spawn(electron);
7
9
 
8
- const createWindow = (options) => {
9
- mainWindow = new BrowserWindow({ autoHideMenuBar: isProd, fullscreen: true, frame: true });
10
-
11
- if (options.port) mainWindow.loadURL(`http://localhost:${options.port}/`);
12
- if (options.file) mainWindow.loadFile(options.file);
13
-
14
- mainWindow.on("closed", () => (mainWindow = null));
15
- isProd ? mainWindow.removeMenu() : mainWindow.webContents.openDevTools();
10
+ return runServer({ rootDir: process.env.PWD }).then((port) => runElectron({ port }));
16
11
  };
17
-
18
- const runElectron = (options) => {
19
- app.whenReady().then(() => createWindow(options));
20
- app.on("window-all-closed", () => process.platform !== "darwin" && app.quit());
21
- app.on("activate", () => mainWindow === null && createWindow(options));
22
- };
23
-
24
- runServer().then((port) => runElectron({ port }));
@@ -0,0 +1,23 @@
1
+ const { app, BrowserWindow } = require("electron");
2
+
3
+ let mainWindow;
4
+
5
+ const isProd = process.env.NODE_ENV === "production";
6
+
7
+ const createWindow = (options) => {
8
+ mainWindow = new BrowserWindow({ autoHideMenuBar: isProd, fullscreen: true, frame: true });
9
+
10
+ if (options.port) mainWindow.loadURL(`http://localhost:${options.port}/`);
11
+ if (options.file) mainWindow.loadFile(options.file);
12
+
13
+ mainWindow.on("closed", () => (mainWindow = null));
14
+ isProd ? mainWindow.removeMenu() : mainWindow.webContents.openDevTools();
15
+ };
16
+
17
+ const runElectron = (options) => {
18
+ app.whenReady().then(() => createWindow(options));
19
+ app.on("window-all-closed", () => process.platform !== "darwin" && app.quit());
20
+ app.on("activate", () => mainWindow === null && createWindow(options));
21
+ };
22
+
23
+ module.exports = { runElectron };
package/src/server.js CHANGED
@@ -4,29 +4,29 @@ const getPort = require("get-port");
4
4
  const bodyParser = require("body-parser");
5
5
  const { writeFile, appendFile, readdirSync, existsSync, readFile } = require("fs");
6
6
 
7
- const rootDir = process.env.PWD;
8
- const stateJson = path.join(rootDir, "state.json");
9
- const gamePath = path.join(rootDir, "game");
10
-
11
- const onSaveState = (req, res) =>
12
- writeFile(stateJson, JSON.stringify(req.body), (err) =>
13
- err ? res.json({ status: "error" }) : res.json({ status: "ok" })
14
- );
15
-
16
- const onGetState = (req, res) =>
17
- readFile(stateJson, (err, data) => {
18
- if (err) return writeFile(stateJson, "{}", () => res.json({ status: "ok", state: {} }));
19
- return res.json({ status: "ok", state: JSON.parse(data.toString()) });
20
- });
7
+ const runServer = async ({ rootDir, oldApp } = {}) => {
8
+ const port = await getPort();
21
9
 
22
- const onLog = (req, res) => {
23
- res.end("ok");
10
+ const stateJson = path.join(rootDir, "state.json");
11
+ const gamePath = path.join(rootDir, "game");
24
12
 
25
- appendFile(path.join(rootDir, "log.txt"), `${req.body.log}\n`);
26
- };
13
+ const onSaveState = (req, res) =>
14
+ writeFile(stateJson, JSON.stringify(req.body), (err) =>
15
+ err ? res.json({ status: "error" }) : res.json({ status: "ok" })
16
+ );
27
17
 
28
- const runServer = async ({ oldApp } = {}) => {
29
- const port = await getPort();
18
+ const onGetState = (req, res) =>
19
+ readFile(stateJson, (err, data) =>
20
+ err
21
+ ? writeFile(stateJson, "{}", () => res.json({ status: "ok", state: {} }))
22
+ : res.json({ status: "ok", state: JSON.parse(data.toString()) })
23
+ );
24
+
25
+ const onLog = (req, res) => {
26
+ res.end("ok");
27
+
28
+ appendFile(path.join(rootDir, "log.txt"), `${req.body.log}\n`);
29
+ };
30
30
 
31
31
  console.log("server will try to start on localhost: ", port);
32
32
 
@@ -59,7 +59,7 @@ const runServer = async ({ oldApp } = {}) => {
59
59
  if (error.code === "EADDRINUSE") {
60
60
  console.log("server not started on port: ", port);
61
61
 
62
- await runServer({ oldApp: app });
62
+ await runServer({ rootDir, oldApp: app });
63
63
 
64
64
  resolve(port);
65
65
  }
package/src/web.js CHANGED
@@ -1,4 +1,4 @@
1
1
  const open = require("open").default;
2
2
  const { runServer } = require("./server");
3
3
 
4
- runServer().then((port) => open(`http://localhost:${port}`));
4
+ module.exports = () => runServer({ rootDir: process.env.PWD }).then((port) => open(`http://localhost:${port}`));