@vnejs/desktop 0.0.1

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 ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@vnejs/desktop",
3
+ "version": "0.0.1",
4
+ "description": "Tools to run desktop app of @vnejs",
5
+ "source": "src/index.js",
6
+ "main": "dist/index.js",
7
+ "bin": {
8
+ "@vnejs/desktop": "./src/index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1",
12
+ "publish:major": "npm version major && npm publish --access public",
13
+ "publish:minor": "npm version minor && npm publish --access public",
14
+ "publish:patch": "npm version patch && npm publish --access public"
15
+ },
16
+ "author": "",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "commander": "13.1.0",
20
+ "express": "4.21.2",
21
+ "get-port": "7.1.0",
22
+ "open": "10.1.0"
23
+ }
24
+ }
@@ -0,0 +1,24 @@
1
+ const { app, BrowserWindow } = require("electron");
2
+
3
+ const { runServer } = require("./server");
4
+ let mainWindow;
5
+
6
+ const isProd = process.env.NODE_ENV === "production";
7
+
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();
16
+ };
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 }));
package/src/index.js ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { Command } = require("commander");
4
+
5
+ const electronAction = require("./electron");
6
+ const webAction = require("./web");
7
+
8
+ const packageJson = require("../package.json");
9
+
10
+ const program = new Command();
11
+
12
+ program.name(packageJson.name).description(packageJson.description).version(packageJson.version);
13
+
14
+ program.command("electron").action(electronAction);
15
+ program.command("web").action(webAction);
16
+
17
+ program.parse();
package/src/server.js ADDED
@@ -0,0 +1,70 @@
1
+ const path = require("path");
2
+ const express = require("express");
3
+ const getPort = require("get-port");
4
+ const bodyParser = require("body-parser");
5
+ const { writeFile, appendFile, readdirSync, existsSync, readFile } = require("fs");
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
+ });
21
+
22
+ const onLog = (req, res) => {
23
+ res.end("ok");
24
+
25
+ appendFile(path.join(rootDir, "log.txt"), `${req.body.log}\n`);
26
+ };
27
+
28
+ const runServer = async ({ oldApp } = {}) => {
29
+ const port = await getPort();
30
+
31
+ console.log("server will try to start on localhost: ", port);
32
+
33
+ const app =
34
+ oldApp ||
35
+ express()
36
+ .use(bodyParser.json({ limit: "50mb" }))
37
+ .use(express.static(path.join(rootDir, "dist")))
38
+ .use("/state/save", onSaveState)
39
+ .use("/state/get", onGetState)
40
+ .use("/log", onLog);
41
+
42
+ readdirSync(gamePath).forEach((modDir) => {
43
+ const mediaDir = path.join(gamePath, modDir, "media");
44
+ const publicDir = path.join(gamePath, modDir, "public");
45
+
46
+ existsSync(mediaDir) && app.use("/media", express.static(mediaDir));
47
+ existsSync(publicDir) && app.use(express.static(publicDir));
48
+ });
49
+
50
+ return new Promise(async (resolve) => {
51
+ const server = app.listen(port);
52
+
53
+ server.on("listening", () => {
54
+ console.log("server started on port: ", port);
55
+
56
+ resolve(port);
57
+ });
58
+ server.on("error", async (error) => {
59
+ if (error.code === "EADDRINUSE") {
60
+ console.log("server not started on port: ", port);
61
+
62
+ await runServer({ oldApp: app });
63
+
64
+ resolve(port);
65
+ }
66
+ });
67
+ });
68
+ };
69
+
70
+ module.exports = { runServer };
package/src/web.js ADDED
@@ -0,0 +1,4 @@
1
+ const open = require("open");
2
+ const { runServer } = require("./server");
3
+
4
+ runServer().then((port) => open(`http://localhost:${port}`));