69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
import fs from 'fs';
|
|
import { BasePaths } from "./base-paths.js";
|
|
import { FileHelper } from './file-helper.js';
|
|
import { Fragment } from '../struct/fragment.js';
|
|
|
|
export const SPECIAL_CONTENT_SYMBOL = `content`;
|
|
|
|
const FragmentSuperManager = {
|
|
fragments: null,
|
|
initialized: false,
|
|
|
|
initialize() {
|
|
const fragmentDir = BasePaths.fragments();
|
|
const entries = fs.readdirSync(fragmentDir, { encoding: 'utf-8', withFileTypes: true });
|
|
const fragmentEntries = entries.filter(
|
|
(e) => FileHelper.isFragment(e)
|
|
);
|
|
|
|
let fragments = [];
|
|
|
|
for(let i = 0; i < fragmentEntries.length; i++) {
|
|
const type = FileHelper.getFragmentType(fragmentEntries[i]);
|
|
const path = fragmentEntries[i].parentPath + '/' + fragmentEntries[i].name;
|
|
const contents = fs.readFileSync(path, { encoding: 'utf-8' });
|
|
|
|
const fragment = new Fragment(type, contents);
|
|
|
|
const name = FileHelper.getBaseName(fragmentEntries[i]);
|
|
|
|
fragments.push({
|
|
name: name,
|
|
fragment: fragment
|
|
});
|
|
}
|
|
|
|
this.fragments = fragments;
|
|
this.initialized = true;
|
|
},
|
|
|
|
get(key) {
|
|
const match = this.fragments?.find(
|
|
(e) => e.name === key
|
|
);
|
|
|
|
return match?.fragment;
|
|
},
|
|
};
|
|
|
|
export class FragmentManager {
|
|
contentFragment;
|
|
_fragmentSuperManager;
|
|
|
|
constructor(contentFragment) {
|
|
this.contentFragment = contentFragment;
|
|
this._fragmentSuperManager = FragmentSuperManager;
|
|
|
|
if(!FragmentSuperManager.initialized) {
|
|
FragmentSuperManager.initialize();
|
|
}
|
|
}
|
|
|
|
get(key) {
|
|
if(key === SPECIAL_CONTENT_SYMBOL) {
|
|
return this.contentFragment;
|
|
}
|
|
|
|
return this._fragmentSuperManager.get(key);
|
|
}
|
|
} |