events/index.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-06-16 02:23:05 +02:00
import Ajv from "npm:ajv@8.8.2";
import addFormats from "npm:ajv-formats@2.1.1";
import { load as yamlLoad } from "https://deno.land/x/js_yaml_port@3.14.0/js-yaml.js";
import { emptyDir } from "https://deno.land/std@0.173.0/fs/mod.ts";
2023-10-11 22:01:00 +02:00
import { Eta } from "https://deno.land/x/eta@v3.0.3/src/index.ts"
2023-06-16 02:23:05 +02:00
async function _loadYaml(fn) {
return yamlLoad(await Deno.readTextFile(fn));
}
2023-10-11 22:01:00 +02:00
function getFlagEmoji(countryCode) {
2023-11-05 22:05:34 +01:00
if (countryCode === 'xx') return "🏴‍☠️";
2023-10-11 22:01:00 +02:00
const codePoints = countryCode
.toUpperCase()
.split('')
.map(char => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
2023-06-16 02:23:05 +02:00
async function test() {
const ajv = new Ajv({ strict: false });
addFormats(ajv);
const schema = await _loadYaml("./schema.yaml");
const validator = ajv.compile(schema);
2023-06-16 02:31:54 +02:00
2023-11-05 21:36:11 +01:00
const list = await _loadYaml(`./events/events.yaml`);
2023-11-05 22:43:36 +01:00
const ids = [];
2023-11-05 21:36:11 +01:00
for (const item of list) {
Deno.test(`${item.id}`, async () => {
if (!validator(item)) {
2023-06-16 02:31:54 +02:00
throw validator.errors;
}
2023-11-05 22:43:36 +01:00
if (ids.includes(item.id)) {
throw `ID exists: ${item.id}`
}
ids.push(item.id)
2023-06-16 02:31:54 +02:00
});
2023-06-16 02:23:05 +02:00
}
}
async function build() {
2023-11-05 21:36:11 +01:00
const list = await _loadYaml(`./events/events.yaml`);
2023-06-16 02:31:54 +02:00
await emptyDir("./dist");
const fn = "./dist/index.json";
2023-11-05 21:36:11 +01:00
await Deno.writeTextFile(fn, JSON.stringify(list, null, 2));
2023-10-11 22:01:00 +02:00
console.log(`File saved: `, fn);
const readmeFn = "./README.md"
const eta = new Eta({ views: "./" })
2023-10-11 22:01:44 +02:00
//console.log(output)
2023-10-15 08:43:37 +02:00
const warning = `<!--
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DO NOT EDIT THIS FILE DIRECLY
EDIT "./README.tpl.eta" INSTEAD
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-->`
2023-11-05 21:36:11 +01:00
await Deno.writeTextFile(readmeFn, warning + "\n\n" + eta.render("./README.tpl.eta", { events: list, getFlagEmoji }))
2023-10-11 22:01:00 +02:00
console.log(`File saved: `, readmeFn);
2023-06-16 02:23:05 +02:00
}
switch (Deno.args[0] || "test") {
case "test":
await test();
break;
case "build":
await build();
break;
}