explorer-data/utils/w3pdata.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-10-07 20:48:35 +02:00
import yaml from "npm:js-yaml";
2023-10-07 20:01:09 +02:00
export class W3PData {
2023-10-07 20:48:35 +02:00
constructor() {
}
2023-10-07 20:01:09 +02:00
2023-10-07 20:48:35 +02:00
async init() {
this.data = await this.load("./src");
}
2023-10-07 20:01:09 +02:00
2023-10-07 20:48:35 +02:00
async load(dataDir) {
const out = {};
for await (const f of Deno.readDir(dataDir)) {
if (f.isFile && f.name.match(/\.yaml$/)) {
const name = f.name.split(".")[0];
out[name] = await this.loadYaml(`${dataDir}/${f.name}`);
}
if (f.isDirectory && f.name === "projects") {
out.projects = [];
const projectsDir = `${dataDir}/${f.name}`;
for await (const pcd of Deno.readDir(projectsDir)) {
const catName = pcd.name;
const catDir = `${projectsDir}/${pcd.name}`;
for await (const pd of Deno.readDir(catDir)) {
if (!pd.isDirectory) {
continue;
2023-10-07 20:01:09 +02:00
}
2023-10-07 20:48:35 +02:00
const pDir = `${catDir}/${pd.name}`;
const index = yaml.load(
await Deno.readTextFile(`${pDir}/index.yaml`),
);
out.projects.push(Object.assign({ id: pd.name }, index));
}
2023-10-07 20:01:09 +02:00
}
2023-10-07 20:48:35 +02:00
}
2023-10-07 20:01:09 +02:00
}
2023-10-07 20:48:35 +02:00
return out;
}
async loadYaml(f) {
return yaml.load(await Deno.readTextFile(f));
}
}