virtual-star/lib/render/template-manager.js
2025-04-21 21:46:30 -04:00

64 lines
1.7 KiB
JavaScript

import fs from 'fs';
import { Fragment, fragmentFormats } from "../struct/fragment.js";
import { BasePaths } from "./base-paths.js";
import { FileHelper } from './file-helper.js';
const TemplateSuperManager = {
templates: null,
initalized: false,
initialize() {
const templateDir = BasePaths.templates();
const entries = fs.readdirSync(templateDir, { encoding: 'utf-8', withFileTypes: true });
const templateEntries = entries.filter(
(e) => FileHelper.isFragment(e)
);
let templates = [];
for(let i = 0; i < templateEntries.length; i++) {
const type = FileHelper.getFragmentType(templateEntries[i]);
const path = templateEntries[i].parentPath + '/' + templateEntries[i].name;
const contents = fs.readFileSync(path, { encoding: 'utf-8' });
const template = new Fragment(type, contents);
const name = FileHelper.getBaseName(templateEntries[i]);
templates.push({
name: name,
template: template
});
}
this.templates = templates;
this.initialized = true;
},
get(key) {
if(!this.initalized) {
this.initialize();
}
const match = this.templates.find(
(t) => t.name === key
);
return match?.template;
}
}
export class TemplateManager {
_templateSuperManager;
constructor() {
this._templateSuperManager = TemplateSuperManager;
if(!TemplateSuperManager.initalized) {
TemplateSuperManager.initialize();
}
}
get(key) {
return this._templateSuperManager.get(key);
}
}