@vnejs/desktop 0.0.37 → 0.0.38

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.37",
3
+ "version": "0.0.38",
4
4
  "description": "Tools to run desktop app of @vnejs",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -10,6 +10,9 @@ const program = new Command();
10
10
 
11
11
  program.name(packageJson.name).description(packageJson.description).version(packageJson.version);
12
12
 
13
- program.command("web").action(webAction);
13
+ program
14
+ .command("web")
15
+ .option("--port-preferred <port>", "preferred port number (overrides package.json vnejs.portPreferred)")
16
+ .action(webAction);
14
17
 
15
18
  program.parse();
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { runServer } = require("./server");
2
2
  const { runElectron } = require("./electron");
3
+ const runWeb = require("./web");
3
4
 
4
- module.exports = { runElectron, runServer };
5
+ module.exports = { runElectron, runServer, runWeb };
package/src/server.js CHANGED
@@ -1,10 +1,10 @@
1
1
  const path = require("path");
2
2
  const os = require("os");
3
3
  const fastify = require("fastify");
4
- const getPort = require("get-port").default;
5
4
  const fastifyCors = require("@fastify/cors");
6
5
  const fastifyStatic = require("@fastify/static");
7
6
  const { readFile, readdir, writeFile, appendFile, mkdir, unlink, access } = require("fs/promises");
7
+ const { default: getPort } = require("get-port");
8
8
 
9
9
  const getPrefix = (key) => {
10
10
  const dot = key.indexOf(".");
@@ -62,8 +62,15 @@ const pathExists = async (target) => {
62
62
  }
63
63
  };
64
64
 
65
- const runServer = async ({ rootDir, stateDir, oldApp, achievementLock, achievementUnlock } = {}) => {
66
- const port = await getPort();
65
+ const readPortPreferred = async (rootDir) => {
66
+ const pkg = JSON.parse(await readFile(path.join(rootDir, "package.json"), "utf8"));
67
+ const port = Number(pkg.vnejs?.portPreferred);
68
+
69
+ return Number.isInteger(port) && port > 0 ? port : undefined;
70
+ };
71
+
72
+ const runServer = async ({ rootDir, stateDir, oldApp, onAchievementLock, onAchievementUnlock } = {}) => {
73
+ const port = await getPort({ port: await readPortPreferred(rootDir) });
67
74
 
68
75
  const resolvedStateDir = stateDir || path.join(rootDir, "state");
69
76
  const gamePath = path.join(rootDir, "game");
@@ -113,14 +120,14 @@ const runServer = async ({ rootDir, stateDir, oldApp, achievementLock, achieveme
113
120
  }
114
121
  };
115
122
 
116
- const onAchievementLock = (req, reply) => {
117
- typeof achievementLock === "function" && achievementLock(req.body?.id, req.body?.data);
123
+ const onAchievementLockMiddleware = (req, reply) => {
124
+ typeof onAchievementLock === "function" && onAchievementLock(req.body?.id, req.body?.data);
118
125
 
119
126
  return reply.send({ status: "ok" });
120
127
  };
121
128
 
122
- const onAchievementUnlock = (req, reply) => {
123
- typeof achievementUnlock === "function" && achievementUnlock(req.body?.id, req.body?.data);
129
+ const onAchievementUnlockMiddleware = (req, reply) => {
130
+ typeof onAchievementUnlock === "function" && onAchievementUnlock(req.body?.id, req.body?.data);
124
131
 
125
132
  return reply.send({ status: "ok" });
126
133
  };
@@ -142,8 +149,8 @@ const runServer = async ({ rootDir, stateDir, oldApp, achievementLock, achieveme
142
149
 
143
150
  app.post("/state/save", onSaveState);
144
151
  app.get("/state/get", onGetState);
145
- app.post("/achievement/lock", onAchievementLock);
146
- app.post("/achievement/unlock", onAchievementUnlock);
152
+ app.post("/achievement/lock", onAchievementLockMiddleware);
153
+ app.post("/achievement/unlock", onAchievementUnlockMiddleware);
147
154
  app.post("/log", onLog);
148
155
 
149
156
  const publicDirs = [path.join(rootDir, "dist")];
@@ -165,13 +172,7 @@ const runServer = async ({ rootDir, stateDir, oldApp, achievementLock, achieveme
165
172
 
166
173
  await app.register(fastifyStatic, { root: publicDirs });
167
174
 
168
- if (mediaDirs.length) {
169
- await app.register(fastifyStatic, {
170
- root: mediaDirs,
171
- prefix: "/media/",
172
- decorateReply: false,
173
- });
174
- }
175
+ if (mediaDirs.length) await app.register(fastifyStatic, { root: mediaDirs, prefix: "/media/", decorateReply: false });
175
176
 
176
177
  return app;
177
178
  };
@@ -188,7 +189,7 @@ const runServer = async ({ rootDir, stateDir, oldApp, achievementLock, achieveme
188
189
  if (error.code === "EADDRINUSE") {
189
190
  console.log("server not started on port: ", port);
190
191
 
191
- return runServer({ rootDir, stateDir, oldApp: app, achievementLock, achievementUnlock });
192
+ return runServer({ rootDir, stateDir, oldApp: app, onAchievementLock, onAchievementUnlock });
192
193
  }
193
194
 
194
195
  throw error;
package/src/web.js CHANGED
@@ -3,8 +3,10 @@ const { runServer } = require("./server");
3
3
 
4
4
  const getRootDir = () => process.env.PWD || process.env.INIT_CWD;
5
5
 
6
- module.exports = async () => {
7
- const port = await runServer({ rootDir: getRootDir() });
6
+ const runWeb = async ({ rootDir = getRootDir() } = {}) => {
7
+ const port = await runServer({ rootDir });
8
8
 
9
- return open(`http://localhost:${port}`);
9
+ return open(`http://localhost:${port}/?port=${port}`);
10
10
  };
11
+
12
+ module.exports = runWeb;