mkmrare/cmd/cube-dev.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-11-21 16:59:49 +00:00
import { existsSync } from "fs";
import { createServer } from "http";
2019-11-23 15:20:58 +00:00
import { Article, asyncLoadJSON, MCMDB } from "../lib";
import { cubeHTML, genCube } from "../lib/cube";
2019-11-22 10:47:27 +00:00
2019-11-21 16:59:49 +00:00
async function run() {
if (process.argv.length < 3) {
2019-11-23 15:20:58 +00:00
console.error("Usage: yarn cube-dev <uid>");
2019-11-21 16:59:49 +00:00
process.exit(1);
return;
}
const uid = process.argv[2];
const uidCards = `${uid}-cards.json`;
if (!existsSync("mcmCards.json")) {
console.error("Card db is missing! Run 'yarn convert-db' first.");
process.exit(1);
}
if (!existsSync(uidCards)) {
console.error(`Could not find ${uidCards}! Run 'yarn fetch ${uid}' first.`);
process.exit(1);
}
let db = await asyncLoadJSON<MCMDB>("mcmCards.json");
let articles = await asyncLoadJSON<Article[]>(`${uid}-cards.json`);
2019-11-23 15:20:58 +00:00
const cubecards = await genCube(db, articles);
2019-11-21 16:59:49 +00:00
const server = createServer(async (req, res) => {
2019-11-23 15:20:58 +00:00
const template = await cubeHTML(uid, cubecards);
2019-11-21 16:59:49 +00:00
res.end(template);
});
2019-11-22 10:47:27 +00:00
2019-11-21 16:59:49 +00:00
server.on("clientError", (err, socket) => {
socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
});
server.listen(8000);
}
run();