@zaisma/cli 0.70.0 → 0.72.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.
package/zaisma.mjs CHANGED
@@ -12,6 +12,7 @@ import { fileURLToPath } from "node:url";
12
12
  import { createBrotliDecompress, createGunzip } from "node:zlib";
13
13
  import { CHECKED_TEXT_EXTENSIONS, GAME_FILE_EXTENSIONS, PRECOMPRESSED_EXTENSIONS, checkGameFiles, extensionOf, findOutsideReferences } from "./check.mjs";
14
14
  import { assetHeadSizes } from "./performance.mjs";
15
+ import { PiecesError, addPiece, checkPiece, listPieces, newerPieces, removePiece, updatePiece } from "./pieces.mjs";
15
16
  import { findChrome, openChrome } from "./chrome.mjs";
16
17
  import { SHADER_CHECK_PAGE, SHADER_LIMITS, isShaderCheckPassing, shaderCheckPageExpressions, shaderDraftAnswer, shaderRenderFromPage, shaderRenderInputOf } from "./shader-check.mjs";
17
18
 
@@ -126,7 +127,7 @@ const EXIT_CODES = {
126
127
  held: { exitCode: 3, meaning: "upload: the version is held for a person at Zaisma to look at, usually within a day." },
127
128
  rejected: { exitCode: 4, meaning: "upload: the version was rejected. Its review_reason and findings say why and how to fix it." },
128
129
  still_scanning: { exitCode: 5, meaning: "upload: the version was still being scanned after 10 minutes. Check it later with zaisma versions <slug>." },
129
- problems_found: { exitCode: 6, meaning: "check: something in the game won't work on Zaisma as it is, and the upload command doesn't fix it; or the shader doesn't compile, is too slow or flashes. The problems say what and how to fix it." },
130
+ problems_found: { exitCode: 6, meaning: "check: something in the game won't work on Zaisma as it is, and the upload command doesn't fix it; or the shader doesn't compile, is too slow or flashes; or pieces check: the piece wouldn't add or would break in a game. The problems say what and how to fix it." },
130
131
  };
131
132
 
132
133
  /** The errors only the command itself answers with; every other code is the API's (ERROR_CODES in packages/core/src/errors.ts). */
@@ -159,8 +160,27 @@ const COMMAND_ERROR_CODES = {
159
160
  unexpected_answer: "Zaisma answered without JSON, like a page from a network in between.",
160
161
  skill_not_ours: "skill: a zaisma skill this command didn't install is already in that folder, so nothing was written; field names the folder.",
161
162
  install_not_confirmed: "skill: nothing was installed, since nobody answered the question: give --yes to install without it.",
163
+ bad_piece_statement: "pieces: a piece's piece.json isn't one: field names what's wrong in it.",
164
+ piece_statement_newer: "pieces: a piece's piece.json is written for a newer command than this one: run the newest.",
165
+ bad_piece_source: "pieces: what was given isn't a piece's folder, its .tgz or a name on npm, or its archive can't be trusted.",
166
+ piece_not_found: "pieces: there's no such piece or version where it was looked for.",
167
+ piece_source_unreachable: "pieces: npm couldn't be reached, so nothing was changed.",
168
+ piece_already_added: "pieces add: the game already has this piece at another version; a piece moves to another version only with pieces update.",
169
+ piece_not_added: "pieces update, pieces remove: the game has no such piece.",
170
+ piece_version_clash: "pieces: two pieces need versions of the same thing that don't overlap, so nothing was changed; the error says both versions and the ways out.",
171
+ piece_need_unmet: "pieces: something a piece needs wasn't found, so nothing was changed.",
172
+ piece_edited: "pieces update, pieces remove: the piece's files in the game were changed by hand; --discard-edits loses the edits.",
173
+ piece_source_needed: "pieces update: the piece came from a folder or a .tgz, so --from must say where the new version is.",
174
+ piece_not_updatable: "pieces update: a template is copied once and is the game's own.",
175
+ piece_not_removable: "pieces remove: a template's files are the game's own.",
176
+ piece_still_needed: "pieces remove: another of the game's pieces needs this one, so nothing was changed.",
177
+ template_file_exists: "pieces add: a template would write over a file the game already has, so nothing was changed.",
178
+ bad_pieces_record: "pieces: the game's pieces/pieces.json can't be read.",
179
+ pieces_record_newer: "pieces: the game's pieces/pieces.json was written by a newer command than this one: run the newest.",
162
180
  };
163
181
 
182
+ const GAME_FOLDER = { hasValue: true, placeholder: "<folder>", about: "The game's folder, the one with its index.html (or its source, for a game that's built); this folder if left out" };
183
+ const DRY_RUN = { hasValue: false, about: "Say what it would do, and change nothing" };
164
184
  const BY_ME = { hasValue: false, about: "Mark it as written by you; otherwise it says your AI wrote it" };
165
185
  const TEXT = { hasValue: true, placeholder: '"…"', about: "The text, in markdown; or pipe it in" };
166
186
  const KEYS = { hasValue: true, placeholder: "'[…]'", about: 'Only these localStorage keys, as a JSON list, like \'["coins", "level"]\'' };
@@ -387,10 +407,46 @@ const COMMANDS = {
387
407
  yes: { hasValue: false, about: "Install without asking first" },
388
408
  },
389
409
  },
410
+ pieces: {
411
+ about: "A game's pieces, the shared building blocks it's made from (docs/PIECES.md): each one's name, version, kind, standard, licence, what it needs, its settings and hooks and where to import it from, and whether its files were changed by hand",
412
+ arguments: [],
413
+ flags: { folder: GAME_FOLDER },
414
+ },
415
+ "pieces add": {
416
+ about: "Add a piece to a game, with what it needs: a piece the game already has is left exactly as it is, and a version that doesn't fit stops with both versions named and nothing added. The piece is a folder, the .tgz that npm pack makes, or a name on npm with an optional range. Adding never runs a piece's code",
417
+ arguments: [{ name: "source" }],
418
+ flags: { folder: GAME_FOLDER, "dry-run": DRY_RUN },
419
+ },
420
+ "pieces update": {
421
+ about: "Move one of a game's pieces to another version: only the one you name, and only when everything else in the game accepts it",
422
+ arguments: [{ name: "name" }],
423
+ flags: {
424
+ folder: GAME_FOLDER,
425
+ version: { hasValue: true, placeholder: "<range>", about: 'The version to move to, or a range to take the newest of, like "^1.2"; the newest if left out' },
426
+ from: { hasValue: true, placeholder: "<source>", about: "Where the new version is, for a piece that came from a folder or a .tgz" },
427
+ "discard-edits": { hasValue: false, about: "Replace the piece's files even though they were changed by hand" },
428
+ "dry-run": DRY_RUN,
429
+ },
430
+ },
431
+ "pieces remove": {
432
+ about: "Take one piece out of a game, unless another of its pieces needs it. Nothing else is removed: what it needed and nothing needs now is named",
433
+ arguments: [{ name: "name" }],
434
+ flags: { folder: GAME_FOLDER, "discard-edits": { hasValue: false, about: "Remove the piece's files even though they were changed by hand" }, "dry-run": DRY_RUN },
435
+ },
436
+ "pieces newer": {
437
+ about: "Which of a game's pieces have a newer version on npm, the newest the rest of the game accepts, and what holds it back. It changes nothing: a version moves only with pieces update",
438
+ arguments: [],
439
+ flags: { folder: GAME_FOLDER },
440
+ },
441
+ "pieces check": {
442
+ about: "Check a piece you're making before you publish it: its piece.json, the files it lists, that every import is a listed file or a stated need, and its package.json beside it",
443
+ arguments: [{ name: "folder" }],
444
+ flags: { fix: { hasValue: false, about: "Write piece.json.js again from piece.json first, when the piece has one" } },
445
+ },
390
446
  help: { about: "This help; with --json, every command, flag, exit code and error code", arguments: [], flags: {} },
391
447
  };
392
448
  /** Commands with a second word, like zaisma forum post. */
393
- const SUBCOMMANDS = { clip: ["remove"], links: ["set", "remove"], forum: ["post", "message", "reply"], database: ["query", "batch", "export", "import", "restore", "undo-restore"], saves: ["get", "change", "replace", "reset", "copies", "copy", "restore"], secrets: ["set", "delete"], items: ["set", "remove"], "kept-rooms": ["delete", "bring-back", "erase-now", "restore", "undo-restore", "restores", "restore-all", "restore-all-status", "undo-restore-all"] };
449
+ const SUBCOMMANDS = { pieces: ["add", "update", "remove", "newer", "check"], clip: ["remove"], links: ["set", "remove"], forum: ["post", "message", "reply"], database: ["query", "batch", "export", "import", "restore", "undo-restore"], saves: ["get", "change", "replace", "reset", "copies", "copy", "restore"], secrets: ["set", "delete"], items: ["set", "remove"], "kept-rooms": ["delete", "bring-back", "erase-now", "restore", "undo-restore", "restores", "restore-all", "restore-all-status", "undo-restore-all"] };
394
450
  /** Flags every command takes. */
395
451
  const GLOBAL_FLAGS = {
396
452
  json: { hasValue: false, about: "Print the API's answer as JSON, as-is (on failure its { error }), and nothing else" },
@@ -611,7 +667,8 @@ async function walk(root) {
611
667
  const hiddenPaths = [];
612
668
  async function visit(dir) {
613
669
  for (const entry of await readdir(dir, { withFileTypes: true })) {
614
- if (JUNK.has(entry.name)) continue;
670
+ // A .d.ts is for editors and AIs (a piece's index.d.ts, the SDK's types): it never runs, so it's never checked or sent.
671
+ if (JUNK.has(entry.name) || entry.name.endsWith(".d.ts")) continue;
615
672
  const full = join(dir, entry.name);
616
673
  if (entry.name.startsWith(".")) {
617
674
  hiddenPaths.push(relative(root, full).split(sep).join("/"));
@@ -2304,6 +2361,7 @@ async function run({ command, values, flags, globalFlags, rest }) {
2304
2361
  return EXIT_CODES.done;
2305
2362
  }
2306
2363
  if (command === "skill") return installSkill(flags);
2364
+ if (command === "pieces" || command.startsWith("pieces ")) return pieces(command, values, flags);
2307
2365
  await updateSkillCopies();
2308
2366
  if (command === "games") {
2309
2367
  const answer = await api("GET", "/games");
@@ -2464,6 +2522,93 @@ async function updateSkillCopies() {
2464
2522
  if (skillUpdates.length && !isJson) console.error(`Updated the Zaisma skill in ${skillUpdates.length} ${skillUpdates.length === 1 ? "AI tool" : "AI tools"} to ${CLI_VERSION}.`);
2465
2523
  }
2466
2524
 
2525
+ /** zaisma pieces: a game's shared building blocks (pieces.mjs). Nothing reaches Zaisma: it reads and writes the game's folder. */
2526
+ async function pieces(command, values, flags) {
2527
+ const folder = resolve(command === "pieces check" ? values.folder : (flags.folder ?? "."));
2528
+ if (!(await stat(folder).catch(() => null))?.isDirectory()) refuse("not_a_folder", { message: `${folder} isn't a folder.`, field: "folder", fix: command === "pieces check" ? "Give the folder of the piece you're making." : "Give the game's folder with --folder, or run it inside the folder." });
2529
+ const isDryRun = Boolean(flags["dry-run"]);
2530
+ const isDiscardingEdits = Boolean(flags["discard-edits"]);
2531
+ let answer;
2532
+ try {
2533
+ if (command === "pieces") answer = await listPieces(folder);
2534
+ else if (command === "pieces add") answer = await addPiece(folder, values.source, { isDryRun });
2535
+ else if (command === "pieces update") answer = await updatePiece(folder, values.name, { versionRange: flags.version ?? "*", source: flags.from ?? null, isDiscardingEdits, isDryRun });
2536
+ else if (command === "pieces remove") answer = await removePiece(folder, values.name, { isDiscardingEdits, isDryRun });
2537
+ else if (command === "pieces newer") answer = await newerPieces(folder);
2538
+ else answer = await checkPiece(folder, { isFixing: Boolean(flags.fix) });
2539
+ } catch (error) {
2540
+ if (error instanceof PiecesError) throw new CommandError(error.error, EXIT_CODES.failed.exitCode);
2541
+ throw error;
2542
+ }
2543
+ const exit = command === "pieces check" && !answer.is_ready ? EXIT_CODES.problems_found : EXIT_CODES.done;
2544
+ if (isJson) {
2545
+ printAnswer(answer);
2546
+ return exit;
2547
+ }
2548
+ const needLine = (need) => `${need.name} ${need.version_range}${need.kind === "piece" ? "" : ` (${need.kind})`}`;
2549
+ const sayPiece = (shown, firstWord) => {
2550
+ console.log(`${firstWord}${shown.name} ${shown.piece_version} ${shown.kind}, standard ${shown.standard}, ${shown.license}${shown.is_edited ? " [its files were changed by hand]" : ""}`);
2551
+ console.log(` ${shown.summary}`);
2552
+ if (shown.needs.length) console.log(` needs: ${shown.needs.map(needLine).join(", ")}`);
2553
+ if (shown.import_path) console.log(` in the game: import ... from "${shown.import_path}"`);
2554
+ if (shown.server_import_path) console.log(` in zaisma-server/: import ... from "${shown.server_import_path}"`);
2555
+ if (shown.settings.length) console.log(` settings: ${shown.settings.map((setting) => setting.name).join(", ")}`);
2556
+ if (shown.hooks.length) console.log(` hooks: ${shown.hooks.map((hook) => hook.name).join(", ")}`);
2557
+ };
2558
+ const sayLibrary = (shown, firstWord) => console.log(`${firstWord}${shown.name} ${shown.library_version} library${shown.license ? `, ${shown.license}` : ""}${shown.is_edited ? " [its files were changed by hand]" : ""}`);
2559
+ const sayImportMap = (importMap) => {
2560
+ if (!Object.keys(importMap.imports).length) return;
2561
+ console.log("\nA page with no bundler needs this before its scripts, so the pieces find their libraries:");
2562
+ console.log(` <script type="importmap">${JSON.stringify(importMap)}</script>`);
2563
+ };
2564
+ const sayBrought = (brought, verb) => {
2565
+ for (const shown of brought.added_pieces) sayPiece(shown, `${verb} `);
2566
+ for (const shown of brought.added_libraries) sayLibrary(shown, `${verb} `);
2567
+ if (brought.kept_names.length) console.log(`Already in the game, left exactly as they are: ${brought.kept_names.join(", ")}.`);
2568
+ for (const need of brought.engine_needs) console.log(`${need.needed_by_name} needs the engine ${need.name} ${need.version_range}: the command can't bring or check an engine, so the game must already be on it.`);
2569
+ // A game's first pieces bring its first standards, which is no news; a standard beside the ones it had is.
2570
+ if (brought.new_standards.length && brought.standards.length > brought.new_standards.length) console.log(`${isDryRun ? "It would bring" : "The game now holds"} pieces built on ${brought.new_standards.join(" and ")}${brought.new_standards.length === 1 ? ", a standard" : ", standards"} it didn't have before: pieces of different standards aren't made to fit each other.`);
2571
+ };
2572
+ const dryRunLine = "Nothing was changed: this was a dry run.";
2573
+ if (command === "pieces") {
2574
+ if (!answer.pieces.length && !answer.libraries.length) console.log("This game has no pieces yet. Add one with: zaisma pieces add <a piece's folder, .tgz or npm name>");
2575
+ for (const shown of answer.pieces) sayPiece(shown, "");
2576
+ for (const shown of answer.libraries) sayLibrary(shown, "");
2577
+ sayImportMap(answer.import_map);
2578
+ } else if (command === "pieces add") {
2579
+ if (!answer.added_pieces.length && !answer.added_libraries.length) console.log("The game already has it: nothing to do.");
2580
+ sayBrought(answer, isDryRun ? "Would add" : "Added");
2581
+ sayImportMap(answer.import_map);
2582
+ console.log(isDryRun ? `\n${dryRunLine}` : "\nEach piece's index.d.ts and README say how to use it. Change a piece through its settings and hooks, never its files.");
2583
+ } else if (command === "pieces update") {
2584
+ const [updated, earlierVersion] = answer.updated_piece ? [answer.updated_piece.piece_version, answer.earlier_piece_version] : [answer.updated_library?.library_version, answer.earlier_library_version];
2585
+ if (!updated) console.log(`${answer.name} is already at ${answer.earlier_piece_version ?? answer.earlier_library_version}: nothing to do.`);
2586
+ else {
2587
+ console.log(`${isDryRun ? "Would move" : "Moved"} ${answer.name} from ${earlierVersion} to ${updated}.`);
2588
+ sayBrought(answer, isDryRun ? "Would add" : "Added");
2589
+ sayImportMap(answer.import_map);
2590
+ if (isDryRun) console.log(dryRunLine);
2591
+ }
2592
+ } else if (command === "pieces remove") {
2593
+ console.log(`${isDryRun ? "Would remove" : "Removed"} ${answer.removed_name} (${answer.removed_folders.join(", ")}).`);
2594
+ if (answer.no_longer_needed_names.length) console.log(`No piece needs these any more: ${answer.no_longer_needed_names.join(", ")}. They stay until you remove them: the game's own code may use them.`);
2595
+ if (isDryRun) console.log(dryRunLine);
2596
+ } else if (command === "pieces newer") {
2597
+ const newerLine = (name, current, newest, newestAccepted, heldBackByNames) => {
2598
+ if (newestAccepted === newest) return `${name} ${current} → ${newest}: zaisma pieces update ${name}`;
2599
+ return `${name} ${current} → ${newest}, held back by ${heldBackByNames.join(" and ")}${newestAccepted ? `; the newest they accept is ${newestAccepted}: zaisma pieces update ${name} --version ${newestAccepted}` : ""}`;
2600
+ };
2601
+ for (const shown of answer.newer_pieces) console.log(newerLine(shown.name, shown.piece_version, shown.newest_piece_version, shown.newest_accepted_piece_version, shown.held_back_by_names));
2602
+ for (const shown of answer.newer_libraries) console.log(newerLine(shown.name, shown.library_version, shown.newest_library_version, shown.newest_accepted_library_version, shown.held_back_by_names));
2603
+ if (!answer.newer_pieces.length && !answer.newer_libraries.length) console.log("Nothing newer on npm.");
2604
+ if (answer.unchecked_names.length) console.log(`Not on npm, so not looked up: ${answer.unchecked_names.join(", ")}.`);
2605
+ } else {
2606
+ for (const problem of answer.problems) console.log(`${problem.problem_kind === "refused" ? "✗" : "·"} ${problem.path ? `${problem.path}: ` : ""}${problem.message} ${problem.fix} (${problem.code})`);
2607
+ console.log(answer.is_ready ? `${answer.name} ${answer.piece_version} is ready: add it to a game with zaisma pieces add ${values.folder}, or publish it with npm publish.` : "Fix what's marked ✗, then check again.");
2608
+ }
2609
+ return exit;
2610
+ }
2611
+
2467
2612
  /** zaisma skill: install the skill into each AI tool found, after one question (or --yes). */
2468
2613
  async function installSkill(flags) {
2469
2614
  if (flags.folder !== undefined && flags.project) refuse("conflicting_flags", { message: "--folder and --project don't go together.", field: "project", fix: "Give --folder to install into one skills folder, or --project to install into this project's tool folders." });