@nasriya/hypercloud
Version:
Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.
88 lines (87 loc) • 3.57 kB
JavaScript
import fs from 'fs';
import path from 'path';
import helpers from "../../../utils/helpers.js";
import Component from "../assets/Component.js";
class ComponentsManager {
create: (component) => {
this.
},
register: async (directory) => {
try {
const dirents = await fs.promises.readdir(directory, { encoding: 'utf-8', withFileTypes: true });
const files = dirents.filter(i => i.isFile() && (i.name.toLowerCase().endsWith('.comp.js') || i.name.toLowerCase().endsWith('.component.js')));
const folders = dirents.filter(i => !i.isFile());
for (const file of files) {
const content = await helpers.loadFileModule(path.join(file.parentPath, file.name));
if (!(content instanceof Component)) {
continue;
}
const compName = content.name;
if (compName in this.
throw new Error(`${compName} is already defined. Only unique Component names are allowed`);
}
this.
}
for (const folder of folders) {
if (folder.name !== 'locals') {
await this.
}
}
}
catch (error) {
if (error instanceof Error) {
error.message = `Unable to register components from directory (${directory}): ${error.message}`;
}
throw error;
}
}
};
/**
* Register your defined components so you can use them in your code
* @param paths A `PathLike` or an array of `PathLike` directories containing your components.
*/
register(paths) {
if (!Array.isArray(paths)) {
paths = [paths];
}
const errRes = { message: 'Invalid components\' paths detected. Read the error list', errors: [], paths };
// Validating input
for (const viewsPath of paths) {
const validity = helpers.checkPathAccessibility(viewsPath);
if (validity.valid) {
this.
continue;
}
const error = { path: viewsPath, type: 'invalid_path', error: '' };
if (validity.errors.notString) {
error.error = 'Not a string';
}
if (validity.errors.doesntExist) {
error.error = 'Path doesn\'t exist';
}
if (validity.errors.notAccessible) {
error.error = 'access denied: no read permissions';
}
errRes.errors.push(error);
}
if (errRes.errors.length > 0) {
throw errRes;
}
}
/**Run all the stored registers */
async scan() {
if (this.
helpers.printConsole('Scanning for components...');
await Promise.allSettled(this.
this.
}
}
/**Get all the components in an array */
get all() { return Object.values(this.
/**Access the storage object */
get storage() { return this.
}
export default new ComponentsManager();