@zaisma/cli 0.28.0 → 0.30.0

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.
Files changed (3) hide show
  1. package/README.md +3 -1
  2. package/package.json +1 -1
  3. package/zaisma.mjs +58 -8
package/README.md CHANGED
@@ -6,7 +6,9 @@ Run it with `npx @zaisma/cli <command>`, or install it once with `npm install -g
6
6
 
7
7
  Before anything else, `zaisma check ./build` lists anything in the game Zaisma would refuse or that won't work on Zaisma (threaded engine builds, `alert()`, forms, servers the game hasn't listed…), each with how to fix it. It needs no key and sends nothing.
8
8
 
9
- 1. Make an API key at https://zaisma.com/account, under Game developers, and set it: `export ZAISMA_KEY=zsk_…`
9
+ To play a multiplayer game on your own computer before uploading it, run `zaisma play-locally` in its folder: the game and its game server run here, and the test bench's Add a player opens another tab as a new player, each on Zaisma's own game page. It needs no key and no internet.
10
+
11
+ 1. Make an API key at https://zaisma.com/dashboard/settings, under Keys, and set it: `export ZAISMA_KEY=zsk_…`
10
12
  2. Make a game: `zaisma new "Sky Hopper"`
11
13
  3. Upload your build folder (the one with `index.html`): `zaisma upload ./build --game sky-hopper --patch-notes "First version"`. Only new or changed files are sent. It runs the check first, and in what it sends (never in your folder) it downloads scripts, styles and fonts the game loads from other sites and unpacks pre-compressed builds (`.gz`, `.br`, `.unityweb`), then says what it changed. Once the scan passes you get a private preview link.
12
14
  4. Publish it: `zaisma publish sky-hopper <version_id>`, then show it to everyone: `zaisma visibility sky-hopper listed`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zaisma/cli",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "description": "Upload and publish browser games on Zaisma from the command line.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://zaisma.com",
package/zaisma.mjs CHANGED
@@ -20,7 +20,8 @@ const KEY = process.env.ZAISMA_KEY || "";
20
20
  const CLI_VERSION = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")).version;
21
21
  /** Files bigger than this go in parts; the server says how big each part is. */
22
22
  const DIRECT_BYTES = 95 * 1024 * 1024;
23
- const JUNK = new Set([".DS_Store", "Thumbs.db", "desktop.ini", "__MACOSX", ".git", "node_modules"]);
23
+ // .zaisma-local holds what play-locally keeps (its keys, saves and database): never sent.
24
+ const JUNK = new Set([".DS_Store", "Thumbs.db", "desktop.ini", "__MACOSX", ".git", "node_modules", ".zaisma-local"]);
24
25
  /** How long upload waits for the scan (200 checks, 3 seconds apart) before it says it's still scanning. ZAISMA_SCAN_CHECK_INTERVAL_MS is for tests. */
25
26
  const SCAN_CHECK_INTERVAL_MS = Number(process.env.ZAISMA_SCAN_CHECK_INTERVAL_MS) || 3000;
26
27
  const SCAN_CHECK_COUNT = 200;
@@ -104,6 +105,7 @@ const COMMAND_ERROR_CODES = {
104
105
  shader_texture_not_approved: "shader, check: a texture sent for the shader was rejected, or still waits for a person, so the shader wasn't sent.",
105
106
  no_chrome: "check: there's no Chrome on this computer to draw the shader in, and no --game with ZAISMA_KEY to draw it on Zaisma.",
106
107
  local_chrome_failed: "check: the Chrome on this computer couldn't draw the shader.",
108
+ game_server_not_run: "play-locally: npx couldn't run @zaisma/game-server, which runs the game on this computer.",
107
109
  server_unreachable: "Zaisma couldn't be reached: no connection, or a wrong ZAISMA_API.",
108
110
  unexpected_answer: "Zaisma answered without JSON, like a page from a network in between.",
109
111
  };
@@ -132,6 +134,18 @@ const COMMANDS = {
132
134
  "draw-on-zaisma": { hasValue: false, about: "A shader's folder only: draw it on Zaisma's own renderer, with --game and ZAISMA_KEY, even when there's a Chrome here" },
133
135
  },
134
136
  },
137
+ "play-locally": {
138
+ about:
139
+ "Play your game on this computer, with its game server and a test bench: Add a player opens another tab as someone else, so you play together, with made-up accounts, friends, blocks and items between them, and Zaisma's buy sheet with a test card that's never charged; " +
140
+ "needs no key and no internet. A file change reloads the game; a change to the game server starts it again with its rooms kept. It runs @zaisma/game-server's play-locally",
141
+ arguments: [{ name: "folder", isOptional: true }],
142
+ flags: {
143
+ port: { hasValue: true, placeholder: "<port>", about: "The first of four ports, 4800 unless given: the bench, the game's site, the stand-in rooms and your game server take it and the next three" },
144
+ game: { hasValue: true, placeholder: "<slug>", about: "The game's slug; with ZAISMA_KEY set, each start brings in the game's items from Zaisma" },
145
+ "on-network": { hasValue: false, about: "Let a phone on the same Wi-Fi open the bench too; it prints the address and a QR code" },
146
+ "no-open": { hasValue: false, about: "Don't open the bench in the browser" },
147
+ },
148
+ },
135
149
  upload: {
136
150
  about: "Upload a version; only changed files are sent. It runs the check first, and downloads files the game loads from outside sites and unpacks pre-compressed builds in what it sends, never in the folder",
137
151
  arguments: [{ name: "folder" }],
@@ -458,7 +472,7 @@ function helpText() {
458
472
  "Exit codes:",
459
473
  ...Object.values(EXIT_CODES).map((exit) => ` ${exit.exitCode} ${exit.meaning}`),
460
474
  "",
461
- "Set ZAISMA_KEY to an API key from https://zaisma.com/account (under Game developers).",
475
+ "Set ZAISMA_KEY to an API key from https://zaisma.com/dashboard/settings (under Keys).",
462
476
  "Without installing: npx @zaisma/cli <command>. Installed (npm i -g @zaisma/cli): zaisma <command>.",
463
477
  );
464
478
  return lines.join("\n");
@@ -489,7 +503,7 @@ function helpAnswer() {
489
503
  }
490
504
 
491
505
  async function api(method, path, body) {
492
- if (!KEY) refuse("no_key", { message: "ZAISMA_KEY isn't set.", field: "ZAISMA_KEY", fix: "Make an API key at https://zaisma.com/account (under Game developers) and set it: export ZAISMA_KEY=zsk_…" }, EXIT_CODES.failed);
506
+ if (!KEY) refuse("no_key", { message: "ZAISMA_KEY isn't set.", field: "ZAISMA_KEY", fix: "Make an API key at https://zaisma.com/dashboard/settings (under Keys) and set it: export ZAISMA_KEY=zsk_…" }, EXIT_CODES.failed);
493
507
  let response;
494
508
  try {
495
509
  response = await fetch(`${API}/api/dev${path}`, {
@@ -1479,7 +1493,7 @@ async function exportGame({ slug }, flags) {
1479
1493
  const folder = flags.folder ?? `${slug}-export`;
1480
1494
  const there = await readdir(folder).catch(() => null);
1481
1495
  if (there?.length) refuse("folder_not_empty", { message: `${folder} isn't empty.`, field: "folder", fix: "Give a new or empty folder with --folder, or move what's in it." });
1482
- if (!KEY) refuse("no_key", { message: "ZAISMA_KEY isn't set.", field: "ZAISMA_KEY", fix: "Make an API key at https://zaisma.com/account (under Game developers) and set it: export ZAISMA_KEY=zsk_…" }, EXIT_CODES.failed);
1496
+ if (!KEY) refuse("no_key", { message: "ZAISMA_KEY isn't set.", field: "ZAISMA_KEY", fix: "Make an API key at https://zaisma.com/dashboard/settings (under Keys) and set it: export ZAISMA_KEY=zsk_…" }, EXIT_CODES.failed);
1483
1497
  let response;
1484
1498
  try {
1485
1499
  response = await fetch(`${API}/api/dev/games/${encodeURIComponent(slug)}/export/download`, { headers: { authorization: `Bearer ${KEY}`, "x-zaisma-client": "cli", "user-agent": `zaisma-cli/${CLI_VERSION}` } });
@@ -1832,7 +1846,7 @@ async function secrets(command, values) {
1832
1846
  }
1833
1847
 
1834
1848
  /** The fields of an item the file gives and set_item takes. */
1835
- const ITEM_FILE_FIELDS = ["item_name", "title", "description", "item_kind", "item_category", "price_minor_units", "currency_code", "local_prices"];
1849
+ const ITEM_FILE_FIELDS = ["item_name", "title", "description", "item_kind", "item_category", "price_minor_units", "currency_code", "local_prices", "is_on_game_page"];
1836
1850
 
1837
1851
  /** A price as people read it, from minor units, for the list. */
1838
1852
  function itemPriceText(gameItem) {
@@ -1861,7 +1875,7 @@ async function items(command, values) {
1861
1875
  } catch {
1862
1876
  wanted = null;
1863
1877
  }
1864
- const fileFix = 'Give a JSON list of whole items, like [{ "item_name": "revive", "title": "Revive", "description": "", "item_kind": "consumable", "item_category": "gameplay", "price_minor_units": 199, "currency_code": "usd", "local_prices": [] }].';
1878
+ const fileFix = 'Give a JSON list of whole items, like [{ "item_name": "revive", "title": "Revive", "description": "", "item_kind": "consumable", "item_category": "gameplay", "price_minor_units": 199, "currency_code": "usd", "local_prices": [], "is_on_game_page": false }].';
1865
1879
  if (!Array.isArray(wanted) || wanted.some((gameItem) => !gameItem || typeof gameItem !== "object" || typeof gameItem.item_name !== "string")) refuse("bad_item", { message: `${values.file ?? "What was piped in"} isn't a JSON list of items.`, field: "file", fix: fileFix });
1866
1880
  for (const gameItem of wanted) {
1867
1881
  const unknown = Object.keys(gameItem).find((key) => !ITEM_FILE_FIELDS.includes(key) && !["card_fee_note", "created_at", "updated_at"].includes(key));
@@ -1871,13 +1885,15 @@ async function items(command, values) {
1871
1885
  const repeated = names.find((name, i) => names.indexOf(name) !== i);
1872
1886
  if (repeated) refuse("bad_item", { message: `${repeated} is in the file twice.`, field: "file", fix: "Give each item once." });
1873
1887
  const current = (await api("GET", path)).items;
1874
- const same = (gameItem, existing) => ITEM_FILE_FIELDS.every((field) => JSON.stringify(gameItem[field] ?? null) === JSON.stringify(existing[field] ?? null));
1888
+ // An item file that leaves is_on_game_page out means false, as items set sends it.
1889
+ const valueOf = (gameItem, field) => gameItem[field] ?? (field === "is_on_game_page" ? false : null);
1890
+ const same = (gameItem, existing) => ITEM_FILE_FIELDS.every((field) => JSON.stringify(valueOf(gameItem, field)) === JSON.stringify(valueOf(existing, field)));
1875
1891
  const changed = wanted.filter((gameItem) => { const existing = current.find((other) => other.item_name === gameItem.item_name); return !existing || !same(gameItem, existing); });
1876
1892
  const gone = current.filter((existing) => !names.includes(existing.item_name));
1877
1893
  const setNames = [];
1878
1894
  for (const gameItem of changed) {
1879
1895
  const { item_name: itemName, ...fields } = gameItem;
1880
- for (const field of ITEM_FILE_FIELDS.slice(1)) if (!(field in fields)) fields[field] = field === "local_prices" ? [] : field === "description" ? "" : null;
1896
+ for (const field of ITEM_FILE_FIELDS.slice(1)) if (!(field in fields)) fields[field] = field === "local_prices" ? [] : field === "description" ? "" : field === "is_on_game_page" ? false : null;
1881
1897
  for (const key of ["card_fee_note", "created_at", "updated_at"]) delete fields[key];
1882
1898
  await api("PUT", `${path}/${encodeURIComponent(itemName)}`, fields);
1883
1899
  setNames.push(itemName);
@@ -2100,6 +2116,7 @@ async function run({ command, values, flags, globalFlags, rest }) {
2100
2116
  return EXIT_CODES.done;
2101
2117
  }
2102
2118
  if (command === "check") return checkFolder(values, flags);
2119
+ if (command === "play-locally") return playLocally(values, flags);
2103
2120
  if (command === "export") return exportGame(values, flags);
2104
2121
  if (command === "upload") return upload(values, flags);
2105
2122
  if (command === "shader") return sendShader(values, flags);
@@ -2153,6 +2170,39 @@ async function run({ command, values, flags, globalFlags, rest }) {
2153
2170
  return EXIT_CODES.done;
2154
2171
  }
2155
2172
 
2173
+ /**
2174
+ * play-locally: the game server package's command, run through npx so this command stays free of dependencies
2175
+ * (ZAISMA_GAME_SERVER_BIN, a path to its bin/node.js, is for tests and the repository). Its output and exit code
2176
+ * come through as they are; with --json it prints { bench_url, game_site_url, rooms_url, network_url } once listening.
2177
+ */
2178
+ async function playLocally(values, flags) {
2179
+ const { spawn } = await import("node:child_process");
2180
+ const given = [values.folder ?? ".", ...(flags.port !== undefined ? ["--port", flags.port] : []), ...(flags.game !== undefined ? ["--game", flags.game] : []), ...(flags["on-network"] ? ["--on-network"] : []), ...(flags["no-open"] ? ["--no-open"] : []), ...(isJson ? ["--json"] : [])];
2181
+ const { existsSync: isThere } = await import("node:fs");
2182
+ const { dirname: folderOf, join: joinPath } = await import("node:path");
2183
+ const local = process.env.ZAISMA_GAME_SERVER_BIN;
2184
+ // npm's npx run through this Node, where npm keeps it beside Node (Windows) or under lib/ (macOS, Linux): a .cmd can't be spawned without a shell on Windows.
2185
+ const npxScript = [joinPath(folderOf(process.execPath), "node_modules/npm/bin/npx-cli.js"), joinPath(folderOf(process.execPath), "../lib/node_modules/npm/bin/npx-cli.js")].find((path) => isThere(path));
2186
+ const npxArguments = ["--yes", "@zaisma/game-server@1", "play-locally", ...given];
2187
+ const [file, list] = local ? [process.execPath, [local, "play-locally", ...given]] : npxScript ? [process.execPath, [npxScript, ...npxArguments]] : ["npx", npxArguments];
2188
+ let child;
2189
+ try {
2190
+ child = spawn(file, list, { stdio: "inherit" });
2191
+ } catch {
2192
+ refuse("game_server_not_run", { message: "npx couldn't run @zaisma/game-server.", fix: "Check that Node 22.18 or newer and npm are installed, and that npm can reach its registry once to download the package." }, EXIT_CODES.failed);
2193
+ }
2194
+ // Ctrl-C reaches the child from the terminal by itself; a SIGTERM sent to this command alone is passed on.
2195
+ process.on("SIGINT", () => {});
2196
+ process.on("SIGTERM", () => child.kill("SIGTERM"));
2197
+ const exitCode = await new Promise((done) => {
2198
+ child.on("error", () => done(null));
2199
+ // Stopped by a signal (Ctrl-C) is a stop, not a failure.
2200
+ child.on("exit", (code, signal) => done(code ?? (signal ? 0 : 1)));
2201
+ });
2202
+ if (exitCode === null) refuse("game_server_not_run", { message: "npx couldn't run @zaisma/game-server.", fix: "Check that Node 22.18 or newer and npm are installed, and that npm can reach its registry once to download the package." }, EXIT_CODES.failed);
2203
+ return exitCode === 0 ? EXIT_CODES.done : { exitCode, meaning: "play-locally stopped" };
2204
+ }
2205
+
2156
2206
  /** A failure, in the API's shape: with --json the { error } on stdout, otherwise one line on stderr. */
2157
2207
  function printError(error) {
2158
2208
  if (isJson) printJson({ error });