primordial-soup/scripts/publish-private.mjs

121 lines
3.5 KiB
JavaScript

import { readFile, mkdir, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { chapterMetadata } from "../viewer/tutorials.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));
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 js = await readFile("dist/viewer.js"),
css = await readFile("dist/viewer.css");
const bundle = createHash("sha256")
.update(js)
.update(css)
.digest("hex")
.slice(0, 16);
const viewer = await api(
`/1/viewer/file?filename=viewer.js&bundle=${bundle}`,
"POST",
js,
true,
);
const style = await api(
`/1/viewer/file?filename=viewer.css&bundle=${bundle}`,
"POST",
css,
true,
);
const current = await api("/1");
await api("/1", "PUT", {
...current,
public: false,
engine: { ...uploaded.engine, entryPoint: "dist/engine.js" },
viewer: {
url: viewer.url,
topLevelVariable: "primordialSoup",
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("/beta-users", "POST", { usernameOrEmail: "coyotte508" });
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");
await writeFile(
".local/release/published.json",
JSON.stringify(published, null, 2),
);
console.log(
JSON.stringify(
{
private: !published.public,
unlisted: privacy.unlisted,
chapters: published.tutorial.chapters.length,
access: access.map((x) => x.username),
engine: published.engine.package,
viewer: published.viewer,
},
null,
2,
),
);