jsm-core
Version: 
Core library for JSM project
142 lines (141 loc) • 6.23 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Module = Module;
exports.Path = Path;
exports.Logger = Logger;
const express_1 = require("express");
const jsm_logger_1 = __importStar(require("jsm-logger"));
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const module_registry_1 = require("./module.registry");
const context_1 = require("../../context");
const typedi_1 = require("typedi");
function extractDirectoryPath(stackLine) {
    // Regular expression to match the file path and extract the directory
    const regex = /\s+at\s.*\((.*\/)[^/]+:\d+:\d+\)/;
    const match = stackLine.match(regex);
    if (match && match[1]) {
        return match[1]; // Directory path is captured in the first group
    }
    return null; // Return null if the pattern does not match
}
function Module() {
    return (target) => {
        const routePath = target.routePath || "/";
        const logger = target.logger || (0, jsm_logger_1.default)(jsm_logger_1.LoggerContext.LOADER, "Module");
        let modulePath = '';
        let moduleBasePath;
        const stack = new Error().stack;
        if (stack) {
            const stackLines = stack.split("\n");
            const callerLine = stackLines[4];
            modulePath = extractDirectoryPath(callerLine) || '';
        }
        if (!modulePath) {
            modulePath = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'src/modules');
        }
        moduleBasePath = modulePath.replace((0, context_1.getRegistry)().cwd, '');
        logger.event(`@Module applied on`, modulePath === null || modulePath === void 0 ? void 0 : modulePath.replace((0, context_1.getRegistry)().cwd, ''));
        const subModulesPath = node_path_1.default.join(modulePath, "modules");
        // Define a mount function
        const mount = (app, parentModules = []) => {
            try {
                /**
                 * Create an instance of the module class
                 */
                const instance = new target();
                if (instance.models && Object.keys(instance.models).length > 0) {
                    for (const modelName in instance.models) {
                        const model = instance.models[modelName];
                        if (model &&
                            typeof model === 'function' &&
                            typeof model.schema === 'object' &&
                            typeof model.modelName === 'string' &&
                            typeof model.db === 'object') {
                            typedi_1.Container.set(modelName, model);
                        }
                    }
                }
                /**
                 * The route path is constructed by joining the route paths of all parent modules
                 */
                const parentModulePaths = parentModules.map((module) => module.routePath);
                const moduleRoutePath = node_path_1.default.join(...parentModulePaths, routePath);
                /**
                 * Create a new router for the module
                 */
                const route = (0, express_1.Router)();
                app.use(moduleRoutePath, route);
                /**
                 * Load controllers from the module directory
                 */
                node_fs_1.default.readdirSync(modulePath).forEach((file) => {
                    const filePath = node_path_1.default.join(modulePath, file);
                    if ((filePath.endsWith(".controller.ts") || filePath.endsWith(".controller.js")) && node_fs_1.default.statSync(filePath).isFile()) {
                        Promise.resolve(`${filePath}`).then(s => __importStar(require(s))).then((controllerModule) => {
                            if (typeof controllerModule.default === "function") {
                                controllerModule.default(route);
                            }
                            else {
                                logger.warn(`Module ${file} does not have a default function`);
                            }
                        });
                    }
                });
                logger.success(`Module loaded successfully at ${moduleRoutePath}`);
            }
            catch (error) {
                logger.error("Error loading module", error);
            }
            return routePath;
        };
        // Register the module
        (0, module_registry_1.registerModule)(routePath, moduleBasePath, mount);
    };
}
function Path(path) {
    return (target) => {
        target.routePath = path;
    };
}
function Logger(logger) {
    return (target) => {
        target.logger = logger;
    };
}