@opengis/fastify-table
Version:
core-plugins
61 lines (60 loc) • 2.26 kB
JavaScript
/* eslint-disable no-console */
import path from "node:path";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import config from "../../../../config.js";
import loadTemplatePath from "./loadTemplatePath.js";
import userTemplateDir from "./userTemplateDir.js";
import customTokens from "./customTokens.js";
import yml2json from "../../yml/funcs/yml2json.js";
const cwd = process.cwd();
export default function getTemplatePath(type) {
if (!type)
return [];
// form cache
if (loadTemplatePath[type])
return loadTemplatePath[type];
loadTemplatePath[type] = [];
const moduleList = [];
const moduleDir = path.join(cwd, "module");
if (existsSync(moduleDir) && !moduleList.length) {
readdirSync(moduleDir).forEach((el) => {
if (existsSync(path.join(cwd, "module", el, "tokens.yml"))) {
const json = yml2json(readFileSync(path.join(cwd, "module", el, "tokens.yml")));
Object.assign(customTokens, json);
console.log("load user tokens: root", `${cwd}/${el}`);
}
moduleList.push(path.join(moduleDir, el, type));
});
}
// add user template dir
userTemplateDir.forEach((dr) => {
moduleList.push(path.join(dr, type));
});
if (config.templateDir) {
moduleList.push(path.join(cwd, config.templateDir, type));
}
moduleList.push(path.join(cwd, "server/templates", type));
moduleList.forEach((el) => {
const templateDir = el;
if (!existsSync(templateDir))
return;
const list = readdirSync(templateDir, { withFileTypes: true });
list.forEach((file) => {
const isDir = file.isDirectory();
const name = isDir
? file.name
: file.name.substring(0, file.name.lastIndexOf("."));
const ext = isDir
? null
: file.name.substring(file.name.lastIndexOf(".") + 1);
loadTemplatePath[type].push([
name,
path.join(templateDir, file.name),
ext,
isDir,
]);
});
});
loadTemplatePath.module = moduleList;
return loadTemplatePath[type];
}