Files

64 lines
2.0 KiB
JavaScript

import * as esbuild from "esbuild";
import { execSync } from "child_process";
import { writeFileSync, mkdirSync, readFileSync } from "fs";
// read .env file is present
try {
for (const line of readFileSync(".env", "utf8").split("\n")) {
const [key, ...rest] = line.split("=");
if (key && rest.length) {
process.env[key.trim()] = rest.join("=").trim();
}
}
} catch {}
const apiKey = process.env.WOV_API_KEY;
if (!process.env.WOV_API_KEY) {
throw new Error("WOV_API_KEY is not set");
}
let revision;
try { revision = execSync("git rev-parse HEAD").toString().trim(); }
catch { throw new Error("failed to get git revision"); }
// Fetch roles
console.log("fetching roles... ");
const res = await fetch("https://api.wolvesville.com/roles", { headers: { "Authorization": `Bot ${apiKey}` } });
const data = await res.json();
if (!res.ok) {
throw new Error(`roles API error ${res.status}: ${JSON.stringify(data)}`);
}
if (!Array.isArray(data.roles) || data.roles.length === 0) {
throw new Error(`unexpected roles response: ${JSON.stringify(data)}`);
}
if (data.roles.some(r => typeof r.name !== "string")) {
throw new Error("some roles are missing a name field");
}
const code = `// Auto generated by scripts/build.mjs\nexport const ROLES: string[] = ${JSON.stringify(data.roles.map(r => r.name), null, 4)};\n`;
mkdirSync("src/generated", { recursive: true });
writeFileSync("src/generated/roles.ts", code);
console.log("done.");
// Bundle
const options = {
entryPoints: ["src/content.ts", "src/popup.ts", "src/background.ts"],
bundle: true,
outdir: "dist",
platform: "browser",
target: "chrome120",
define: {
__GIT_REVISION__: JSON.stringify(revision),
__GITEA_API__: JSON.stringify("https://git.pihkaal.me/api/v1/repos/pihkaal/wov-assassins-convention-tweaks"),
},
};
if (process.argv.includes("--watch")) {
const ctx = await esbuild.context(options);
await ctx.watch();
console.log("watching...");
} else {
await esbuild.build(options);
}