@nasriya/hypercloud
Version:
Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.
93 lines (92 loc) • 3.95 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const helpers_1 = __importDefault(require("../../../utils/helpers"));
const Component_1 = __importDefault(require("../assets/Component"));
class ComponentsManager {
#_storage = {};
#_registers = [];
#_helpers = {
create: (component) => {
this.#_storage[component.name] = component;
},
register: async (directory) => {
try {
const dirents = await fs_1.default.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_1.default.loadFileModule(path_1.default.join(file.parentPath, file.name));
if (!(content instanceof Component_1.default)) {
continue;
}
const compName = content.name;
if (compName in this.#_storage) {
throw new Error(`${compName} is already defined. Only unique Component names are allowed`);
}
this.#_helpers.create(content);
}
for (const folder of folders) {
if (folder.name !== 'locals') {
await this.#_helpers.register(path_1.default.join(directory, folder.name));
}
}
}
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_1.default.checkPathAccessibility(viewsPath);
if (validity.valid) {
this.#_registers.push(this.#_helpers.register(viewsPath));
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.#_registers.length > 0) {
helpers_1.default.printConsole('Scanning for components...');
await Promise.allSettled(this.#_registers);
this.#_registers.length = 0;
}
}
/**Get all the components in an array */
get all() { return Object.values(this.#_storage); }
/**Access the storage object */
get storage() { return this.#_storage; }
}
exports.default = new ComponentsManager();
;