prague-blockchain-week/utils/engine.js

142 řádky
3.9 KiB
JavaScript
Surový Normální zobrazení Historie

2023-01-23 00:42:21 +01:00
import { emptyDir, exists } from "https://deno.land/std@0.119.0/fs/mod.ts";
2023-01-21 22:07:27 +01:00
import { parse as tomlParse } from "https://deno.land/std@0.173.0/encoding/toml.ts";
2023-01-23 02:27:35 +01:00
import { load as yamlLoad } from "https://deno.land/x/js_yaml_port@3.14.0/js-yaml.js";
2023-01-21 22:07:27 +01:00
2023-01-23 02:27:35 +01:00
let _silentMode = false;
2023-01-23 00:42:21 +01:00
2023-01-21 22:07:27 +01:00
export class DeConfEngine {
constructor(options = {}) {
this.options = options;
this.srcDir = this.options.srcDir || "./data";
2023-01-23 00:42:21 +01:00
this.outputDir = this.options.outputDir || "./dist";
2023-01-23 00:59:31 +01:00
this.publicUrl = this.options.publicUrl || "https://data.prgblockweek.com";
2023-01-23 01:25:49 +01:00
this.githubUrl = this.options.githubUrl ||
"https://github.com/utxo-foundation/prague-blockchain-week/tree/main/data";
2023-01-23 00:42:21 +01:00
2023-01-23 02:27:35 +01:00
if (options.silentMode) {
_silentMode = true;
}
}
async init() {
2023-01-23 00:59:31 +01:00
this.entries = [];
2023-01-21 22:07:27 +01:00
for await (const f of Deno.readDir(this.srcDir)) {
2023-01-23 00:42:21 +01:00
if (!f.name.match(/^\d+$/)) continue;
2023-01-23 01:25:49 +01:00
const pkg = new DeConf_Package(f.name, this);
2023-01-23 00:42:21 +01:00
await pkg.load([this.srcDir, f.name]);
2023-01-23 02:27:35 +01:00
this.entries.push(pkg);
}
}
async build() {
await emptyDir(this.outputDir);
for (const pkg of this.entries) {
2023-01-23 00:42:21 +01:00
console.table(pkg.data.events.map((e) => e.data.index), ["name"]);
await pkg.write(this.outputDir);
2023-01-21 22:07:27 +01:00
}
2023-01-23 01:03:52 +01:00
await _jsonWrite(
[this.outputDir, "index.json"],
this.entries.map((p) => ({
id: p.id,
name: p.data.index.name,
2023-01-23 01:25:49 +01:00
dataUrl: p.data.index.dataUrl,
2023-01-23 01:03:52 +01:00
})),
);
2023-01-21 22:07:27 +01:00
}
2023-01-23 02:27:35 +01:00
async schemas(version = "1") {
const schemaDir = `./utils/schema/${version}`;
const arr = [];
for await (const f of Deno.readDir(schemaDir)) {
const m = f.name.match(/^(.+)\.yaml$/);
if (!m) {
continue;
}
arr.push({
name: m[1],
schema: Object.assign(
{ $id: this.schemaUrl(version, m[1]) },
await _yamlLoad([schemaDir, f.name].join("/")),
),
});
}
return arr.sort((x, y) => x.name > y.name ? 1 : -1);
}
schemaUrl(version = "1", type = "index") {
return `${this.publicUrl}/schema/${version}/${type}.json`;
}
entriesList() {
return this.entries.map((e) => e.id);
}
2023-01-23 00:42:21 +01:00
}
class DeConf_Package {
2023-01-23 01:25:49 +01:00
constructor(id, engine) {
2023-01-23 00:42:21 +01:00
this.id = id;
this.data = null;
2023-01-23 01:25:49 +01:00
this.engine = engine;
2023-01-23 00:42:21 +01:00
}
async load(specDir) {
const pkg = {};
// load year index
pkg.index = await _tomlLoad([...specDir, "index.toml"].join("/"));
2023-01-23 01:25:49 +01:00
pkg.index.dataUrl = [this.engine.publicUrl, this.id].join("/");
pkg.index.dataGithubUrl = [this.engine.githubUrl, this.id].join("/");
2023-01-23 02:27:35 +01:00
//console.log(`\n##\n## [${pkg.index.name}] \n##`);
2023-01-23 00:42:21 +01:00
// load sub-events
pkg.events = [];
for await (const ef of Deno.readDir([...specDir, "events"].join("/"))) {
if (!ef.name.match(/^[\w\d\-]+$/)) continue;
const ev = new DeConf_Event(ef.name);
await ev.load([...specDir, "events", ef.name]);
pkg.events.push(ev);
}
this.data = pkg;
}
async write(dir) {
const outputDir = [dir, this.id].join("/");
await emptyDir(outputDir);
2023-01-23 00:59:31 +01:00
await _jsonWrite([outputDir, "index.json"], this.toJSON());
2023-01-23 00:42:21 +01:00
}
toJSON() {
return Object.assign({ id: this.id }, this.data.index, {
events: this.data.events,
time: new Date(),
});
}
}
class DeConf_Event {
constructor(id) {
this.id = id;
this.data = null;
}
async load(dir) {
const efIndex = await _tomlLoad([...dir, "index.toml"].join("/"));
const event = {
2023-01-23 02:27:35 +01:00
index: { id: this.id, ...efIndex },
2023-01-23 00:42:21 +01:00
};
this.data = event;
}
toJSON() {
return Object.assign({ id: this.id }, this.data.index);
}
}
async function _tomlLoad(fn) {
return tomlParse(await Deno.readTextFile(fn));
}
2023-01-23 02:27:35 +01:00
async function _yamlLoad(fn) {
return yamlLoad(await Deno.readTextFile(fn));
}
2023-01-23 00:42:21 +01:00
async function _jsonWrite(fn, data) {
if (Array.isArray(fn)) {
fn = fn.join("/");
}
await Deno.writeTextFile(fn, JSON.stringify(data, null, 2));
if (!_silentMode) {
console.log(`${fn} writed`);
2023-01-21 22:07:27 +01:00
}
2023-01-23 00:42:21 +01:00
return true;
2023-01-21 22:07:27 +01:00
}