Skip to content

Commit

Permalink
Merge branch 'main' into index-html-redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
OLILHR authored Oct 29, 2024
2 parents 2ea12a0 + 1fe5a81 commit c269bcc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
38 changes: 28 additions & 10 deletions src/server/ebd-loader.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { readdirSync } from "fs";
import { join } from "path";

function tryReadDir(path: string) {
try {
return readdirSync(path);
} catch {
return null;
}
}

// fetch submodule data from either /static/ebd (sveltekit "dev server") or /build/ebd (sveltekit "preview")
export function getEbds(): Record<string, string[]> {
const ebdPath = join(process.cwd(), "static", "ebd");
const staticPath = join(process.cwd(), "static", "ebd");
const buildPath = join(process.cwd(), "build", "ebd");
const ebds: Record<string, string[]> = {};

try {
const formatVersions = readdirSync(ebdPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const basePath = tryReadDir(staticPath) ? staticPath : buildPath;
const formatVersions = tryReadDir(basePath);

if (!formatVersions) {
console.error("submodule data not found.");
return {};
}

try {
for (const formatVersion of formatVersions) {
const versionPath = join(ebdPath, formatVersion);
ebds[formatVersion] = readdirSync(versionPath)
.filter((file) => file.endsWith(".svg"))
.map((file) => file.replace(".svg", ""))
.sort((a, b) => a.localeCompare(b));
const versionPath = join(basePath, formatVersion);
const files = tryReadDir(versionPath);

if (files) {
ebds[formatVersion] = files
.filter((file) => file.endsWith(".svg"))
.map((file) => file.replace(".svg", ""))
.sort((a, b) => a.localeCompare(b));
}
}

return ebds;
Expand Down
29 changes: 21 additions & 8 deletions src/server/format-version-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ function formatVersion(version: string): FormatVersion {
};
}

export function getFormatVersions(): FormatVersion[] {
const ebdPath = join(process.cwd(), "static", "ebd");
function tryReadDir(path: string) {
try {
return readdirSync(ebdPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => formatVersion(dirent.name))
.sort((a, b) => b.code.localeCompare(a.code)); // descending order
} catch (error) {
console.error("No format version directories available.", error);
return readdirSync(path, { withFileTypes: true });
} catch {
return null;
}
}

// fetch submodule data from either /static/ebd (sveltekit "dev server") or /build/ebd (sveltekit "preview")
export function getFormatVersions(): FormatVersion[] {
const staticPath = join(process.cwd(), "static", "ebd");
const buildPath = join(process.cwd(), "build", "ebd");

const dirents = tryReadDir(staticPath) || tryReadDir(buildPath);

if (!dirents) {
console.error("submodule data not found.");
return [];
}

return dirents
.filter((dirent) => dirent.isDirectory())
.map((dirent) => formatVersion(dirent.name))
.sort((a, b) => b.code.localeCompare(a.code));
}

0 comments on commit c269bcc

Please sign in to comment.