@opengis/fastify-table
Version:
core-plugins
94 lines (93 loc) • 3.76 kB
JavaScript
import path from "node:path";
import yaml from "js-yaml";
import { readdir, readFile } from "node:fs/promises";
import getTemplatePath from "./getTemplatePath.js";
import loadTemplate from "./loadTemplate.js";
import mdToHTML from "../../md/funcs/mdToHTML.js";
async function readFileData(file) {
const data = await readFile(file, "utf-8");
const ext = file.substring(file.lastIndexOf(".") + 1);
if (ext === "yml") {
return yaml.load(data);
}
if (ext === "json" && data) {
return JSON.parse(data);
}
if (ext === "md") {
const match = data.match(/^---\r?\n([\s\S]*?)---\r?\n([\s\S]*)$/);
const parseLinesRecursively = (lines, index = 0, result = {}) => index >= lines.length
? result
: (([key, ...rest]) => key && rest.length
? parseLinesRecursively(lines, index + 1, {
...result,
[key.trim()]: rest.join(":").trim(),
})
: parseLinesRecursively(lines, index + 1, result))((lines[index] || "").split(":"));
const header = match?.length
? parseLinesRecursively(match[1]?.split?.(/\r?\n/) || [])
: {};
const body = match?.[2]?.replace?.(/\r\n/g, "")?.startsWith?.("```html")
? match[2]?.match?.(/```html\r?\n([\s\S]*?)```/)?.[1]
: match?.[2];
return { ...header, html: mdToHTML(body || data) };
}
return data;
}
const isProduction = process.env.NODE_ENV === "production";
async function getTemplateData(template) {
// dir template: dashboard, card
if (template[0][3]) {
const files = await readdir(template[0][1]);
const data = await Promise.all(files.map(async (el) => readFileData(path.join(template[0][1], el))));
return files.map((el, i) => [el, data[i]]);
}
// one file template: table, form
if (template.length === 1) {
const data = await readFileData(template[0][1]);
if (template[0][2] === "json" &&
path.basename(path.dirname(template[0][1])) === "table" &&
data?.parent) {
const parentTemplate = getTemplatePath("table").find((el) => el[0] === data.parent);
const parentData = await readFileData(parentTemplate[1]);
return { ...(parentData || {}), ...data };
}
return data;
}
// multi file template: select, etc
if (template.length > 1) {
const data = await Promise.all(template.map(async (el) => readFileData(el[1])));
if (Array.isArray(data[0])) {
return data[0];
}
const result = {};
template.forEach((el, i) => {
Object.assign(result, typeof data[i] === "object" ? data[i] : { [el[2]]: data[i] });
});
return result;
}
return null;
}
export default async function getTemplate(type, name) {
if (!type)
return null;
if (!name)
return null;
const key = `${type}:${name}`;
if (name === "cache" && !isProduction)
return loadTemplate; // all cache debug
// if (loadTemplate[key] && isProduction && false) return loadTemplate[key]; // from cache
// type one or multi
const templateList = Array.isArray(type)
? type
.map((el) => getTemplatePath(el))
.filter((list) => list?.filter((el) => el[0] === name).length)[0] || []
: getTemplatePath(type);
// find template
const template = templateList?.filter((el) => el[0] === name);
if (name === "list" && !isProduction)
return templateList; // all template debug
if (!template.length)
return null; // not found
loadTemplate[key] = await getTemplateData(template);
return loadTemplate[key];
}