aztec
Version: 
Node Js Framework for creating API Services
135 lines (134 loc) • 5.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const base_class_1 = require("./base.class");
const controllers_manager_class_1 = require("./controllers-manager.class");
const models_manager_class_1 = require("./models-manager.class");
const router_controller_1 = require("./router/router.controller");
const router_class_1 = require("./router/router.class");
const handler_class_1 = require("./handler.class");
const lib_1 = require("./lib");
class ImportSystem extends base_class_1.Base {
    constructor() {
        super();
        this['api-prefix'] = 'v';
        this.controllersManager = new controllers_manager_class_1.ControllersManager();
        this.routerController = new router_controller_1.RouterController(this);
        this.modelsManager = new models_manager_class_1.ModelsManager();
        this.router = new router_class_1.Router(this);
        if (!global['use'])
            global['use'] = this.use.bind(this);
        if (!global['AZTEC'])
            global['AZTEC'] = {};
        if (!global['AZTEC']['SERVICES'])
            global['AZTEC']['SERVICES'] = {};
        if (!global['AZTEC']['APPS'])
            global['AZTEC']['APPS'] = {};
    }
    use(path) {
        let partialPath = path.split('/');
        if (partialPath.length === 0)
            partialPath = [path];
        let application, service, aztec;
        switch (partialPath[0]) {
            case 'Service':
                service = global['AZTEC']['SERVICES'][partialPath[1]];
                break;
            case 'App':
                application = global['AZTEC']['APPS'][partialPath[1]];
                break;
            case 'Aztec':
                aztec = require('../index');
                break;
        }
        if (service && partialPath[1] === service.name) {
            return lib_1.dropIfNotDefault(service.baseDir);
        }
        else if (application) {
            switch (partialPath[2]) {
                case 'Controller':
                    return (this.controllers[`${partialPath[3] || partialPath[1]}Controller`] ||
                        global['AZTEC']['APPS'][application.name]['controllers'][`${partialPath[3] || partialPath[1]}Controller`]);
                case 'Model':
                    return (this.models[partialPath[3] || partialPath[1]] ||
                        global['AZTEC']['APPS'][application.name]['models'][partialPath[3] || partialPath[1]]);
                case 'Routes': return this.require(application.router.get('path'));
            }
            if (partialPath[1] === application.name) {
                return lib_1.dropIfNotDefault(application.baseDir);
            }
        }
        else if (aztec) {
            if (aztec[partialPath[1]])
                return aztec[partialPath[1]];
            else
                handler_class_1.Handler.error(`Failed to found "${partialPath[1]}" in "${partialPath[0]}"`);
        }
        else {
            try {
                return require(path);
            }
            catch (e) {
                handler_class_1.Handler.error(e);
            }
        }
    }
    requireAndCreate(app, component, asObject = false) {
        let components = this.require(component, true);
        if (components.length) {
            if (asObject) {
                let componentsAsObject = {};
                components.forEach(component => {
                    try {
                        componentsAsObject[component.name] = new component;
                    }
                    catch (e) {
                        handler_class_1.Handler.error(`the "${component}" does not exist or it has no "export"`);
                    }
                });
                return componentsAsObject;
            }
            else {
                return components.map(component => {
                    try {
                        return new component;
                    }
                    catch (e) {
                        handler_class_1.Handler.error(`the "${component}" does not exist or it has no "export"`);
                    }
                });
            }
        }
        else {
            handler_class_1.Handler.error(`Cannot find "${component}" in "${app.name}" application directory.`);
        }
    }
    require(path, isDefault = false) {
        switch (path) {
            case 'routes':
                path = this.router.get('path');
                break;
            case 'models':
                path = this.modelsManager.get('path');
                break;
            case 'controllers':
                path = this.controllersManager.get('path');
                break;
        }
        const exists = fs.existsSync(path);
        if (exists) {
            let files = fs.readdirSync(path);
            if (isDefault) {
                return files.map(file => {
                    return lib_1.dropIfNotDefault(`${path}/${file}`);
                });
            }
            else {
                return files.map(file => {
                    return require(`${path}/${file}`);
                });
            }
        }
    }
}
exports.ImportSystem = ImportSystem;