primordial-soup/scripts/publish-private.mjs

150 lines
4.5 KiB
JavaScript

import { uploadViewerFiles } from "./viewer-files.mjs";
import { readFile, mkdir, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { execFileSync } from "node:child_process";
import assert from "node:assert/strict";
import { NEW_GENES_OPTION } from "../engine/new-genes.js";
import { chapterMetadata } from "../viewer/tutorials.js";
import { COLOR_BLIND_PREFERENCE } from "../viewer/preferences.js";
const token = (await readFile(`${homedir()}/.bgs`, "utf8")).trim();
const base =
"https://admin.boardgamers.space/api/admin/gameinfo/primordial-soup";
async function api(path, method = "GET", body, raw = false) {
const response = await fetch(base + path, {
method,
headers: {
Authorization: `Bearer ${token}`,
...(body
? {
"Content-Type": raw
? "application/octet-stream"
: "application/json",
}
: {}),
},
body: body ? (raw ? body : JSON.stringify(body)) : undefined,
});
if (!response.ok)
throw new Error(
`${method} ${path}: ${response.status} ${await response.text()}`,
);
return response.json();
}
const before = await api("/1");
const meta = await api("/meta");
if (before.public || !meta.unlisted)
throw new Error(
"Publication requires an existing private, unlisted version.",
);
await mkdir(".local/release", { recursive: true });
await writeFile(".local/release/before.json", JSON.stringify(before, null, 2));
await writeFile(
".local/release/meta-before.json",
JSON.stringify(meta, null, 2),
);
execFileSync("pnpm", ["pack", "--pack-destination", ".local/release"], {
stdio: "inherit",
});
const pkg = JSON.parse(await readFile("package.json", "utf8"));
const tarball = await readFile(
`.local/release/boardgamers-primordial-soup-${pkg.version}.tgz`,
);
const uploaded = await api("/1/engine", "POST", tarball, true);
const files = await uploadViewerFiles(
"dist",
"viewer.js",
["viewer.css"],
(query, bytes) => api("/1/viewer/file?" + query, "POST", bytes, true),
);
const viewer = { url: files.url },
style = { url: files.stylesheets[0] };
const current = await api("/1");
await api("/1", "PUT", {
...current,
public: false,
preferences: [
...(current.preferences ?? []).filter(
(preference) => preference.name !== COLOR_BLIND_PREFERENCE.name,
),
COLOR_BLIND_PREFERENCE,
],
options: [
...(current.options ?? []).filter((o) => o.name !== NEW_GENES_OPTION.name),
NEW_GENES_OPTION,
],
engine: { ...uploaded.engine, entryPoint: "dist/engine.js" },
viewer: {
...current.viewer,
url: viewer.url,
scriptBytes: files.scriptBytes,
topLevelVariable: "primordialSoup",
replayable: true,
fullScreen: true,
fullScreenMobile: true,
dependencies: { scripts: [], stylesheets: [style.url] },
},
tutorial: {
sections: [
{
id: "basics",
title: "Life in the soup",
description:
"Learn a complete round: movement, feeding, genes, division and evolution.",
},
{
id: "adaptations",
title: "A more capable colony",
description:
"Explore advanced genes and interactions between colonies.",
},
],
chapters: chapterMetadata.map((c, i) => ({
...c,
section: i < 6 ? "basics" : "adaptations",
})),
},
});
await api("/meta", "PUT", { ...meta, players: [3, 4, 5, 6] });
const published = await api("/1"),
privacy = await api("/meta"),
access = await api("/beta-users");
if (published.public || !privacy.unlisted)
throw new Error("Privacy check failed");
assert.deepEqual(privacy.players, [3, 4, 5, 6]);
assert.deepEqual(
published.options.find((option) => option.name === NEW_GENES_OPTION.name),
NEW_GENES_OPTION,
);
assert.equal(published.engine.package.version, pkg.version);
assert.deepEqual(
published.preferences.find(
(preference) => preference.name === COLOR_BLIND_PREFERENCE.name,
),
COLOR_BLIND_PREFERENCE,
);
assert.equal(
published.viewer.chat,
current.viewer?.chat,
"Publication changed the viewer chat setting",
);
await writeFile(
".local/release/published.json",
JSON.stringify(published, null, 2),
);
console.log(
JSON.stringify(
{
private: !published.public,
unlisted: privacy.unlisted,
players: privacy.players,
options: published.options,
preferences: published.preferences,
chapters: published.tutorial.chapters.length,
access: access.map((x) => x.username),
engine: published.engine.package,
viewer: published.viewer,
},
null,
2,
),
);