Skip to content

Commit

Permalink
Separate ebd loader functions for fetching SVGs and JSONs metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
OLILHR committed Nov 5, 2024
1 parent 8d09aea commit 479ea18
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getEbds } from "$server/ebd-loader";
import { getEbdsWithMetadata } from "$server/ebd-loader";
import { getFormatVersions } from "$server/format-version-loader";

export const load = async () => {
const formatVersions = getFormatVersions();
const ebds = getEbds();
const ebds = getEbdsWithMetadata();

return {
formatVersions,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/ebd/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getEbds } from "$server/ebd-loader";
import { getEbdsWithMetadata } from "$server/ebd-loader";
import { getFormatVersions } from "$server/format-version-loader";

import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async () => {
const formatVersions = getFormatVersions();
const ebds = getEbds();
const ebds = getEbdsWithMetadata();

return {
formatVersions,
Expand Down
76 changes: 49 additions & 27 deletions src/server/ebd-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { join } from "path";

import type { EbdNameExtended, MetaData } from "$lib/types/metadata";

let ebdFiles: Record<string, string[]> | null = null;
let ebdsWithMetadata: Record<string, EbdNameExtended[]> | null = null;

// mapping of FVs where the BDEW skipped EBD related updates
const skippedFormatVersionToInsteadFormatVersionMap: Record<string, string> = {
// key = skipped format version; value = previous (still valid) format version
FV2410: "FV2404",
};

export function getEbds(): Record<string, EbdNameExtended[]> {
// fetches EBD files and associated metadata
function getEbds(): Record<string, string[]> {
if (ebdFiles) return ebdFiles;

const staticPath = join(process.cwd(), "static", "ebd");
const ebds: Record<string, EbdNameExtended[]> = {};
const ebds: Record<string, string[]> = {};

try {
const formatVersions = readdirSync(staticPath, { withFileTypes: true })
Expand All @@ -28,36 +34,52 @@ export function getEbds(): Record<string, EbdNameExtended[]> {

ebds[formatVersion] = files
.filter((file) => file.endsWith(".svg"))
.map((file) => {
const ebdCode = file.replace(".svg", "");
let ebd_name = ebdCode; // by default, EBD <input> only shows ebd_code "E_XXXX"

try {
const jsonPath = join(versionPath, `${ebdCode}.json`);
const jsonContent = readFileSync(jsonPath, "utf-8");
const parseMetaData = JSON.parse(jsonContent) as MetaData;
// only update extended ebd_name if corresponding metadata <value> of ebd_name <key> exists
if (
parseMetaData.metadata.chapter &&
parseMetaData.metadata.chapter.trim()
) {
ebd_name = `${ebdCode}_${parseMetaData.metadata.chapter}`;
}
} catch (error) {
console.warn(`no metadata available for ${ebdCode}: ${error}`);
}

return {
ebd_code: ebdCode,
ebd_name,
};
})
.sort((a, b) => a.ebd_code.localeCompare(b.ebd_code));
.map((file) => file.replace(".svg", ""))
.sort((a, b) => a.localeCompare(b));
}

ebdFiles = ebds;
return ebds;
} catch (error) {
console.error("error reading EBDs from submodule:", error);
return {};
}
}

// Export function for SSG
export function getEbdFiles(): Record<string, string[]> {
return getEbds();
}

// export function for UI, including EBDs and associated metadata
export function getEbdsWithMetadata(): Record<string, EbdNameExtended[]> {
if (ebdsWithMetadata) return ebdsWithMetadata;

const ebds = getEbds();
const result: Record<string, EbdNameExtended[]> = {};

for (const [formatVersion, ebdCodes] of Object.entries(ebds)) {
const versionPath = join(process.cwd(), "static", "ebd", formatVersion);

result[formatVersion] = ebdCodes.map((ebdCode) => {
let ebd_name = ebdCode;

try {
const jsonPath = join(versionPath, `${ebdCode}.json`);
const parseMetaData = JSON.parse(
readFileSync(jsonPath, "utf-8"),
) as MetaData;
if (parseMetaData.metadata.chapter?.trim()) {
ebd_name = `${ebdCode}_${parseMetaData.metadata.chapter}`;
}
} catch (error) {
console.warn(`no metadata available for ${ebdCode}: ${error}`);
}

return { ebd_code: ebdCode, ebd_name };
});
}

ebdsWithMetadata = result;
return result;
}
4 changes: 2 additions & 2 deletions src/server/prerender-entries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { join } from "path";

import { getEbds } from "$server/ebd-loader";
import { getEbdFiles } from "$server/ebd-loader";
import { getFormatVersions } from "$server/format-version-loader";

export function prerenderEntries() {
const staticPath = join(process.cwd(), "static", "ebd");

const formatVersions = getFormatVersions();
const ebds = getEbds();
const ebds = getEbdFiles();

const entries = [];
const formatVersionCounts: Record<string, number> = {};
Expand Down

0 comments on commit 479ea18

Please sign in to comment.