UNPKG

aztec

Version:

Node Js Framework for creating API Services

135 lines (113 loc) 4.25 kB
import fs = require('fs'); import { Base } from './base.class'; import { ControllersManager } from './controllers-manager.class'; import { ModelsManager } from './models-manager.class'; import { RouterController } from './router/router.controller'; import { Router } from './router/router.class'; import { Handler } from './handler.class'; import { dropIfNotDefault } from "./lib"; export class ImportSystem extends Base { constructor() { super(); this.controllersManager = new ControllersManager(); this.routerController = new RouterController(this); this.modelsManager = new ModelsManager(); this.router = new 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'] = {}; } protected use(path: string): any { 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 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 dropIfNotDefault(application.baseDir); } } else if (aztec) { if (aztec[partialPath[1]]) return aztec[partialPath[1]]; else Handler.error(`Failed to found "${partialPath[1]}" in "${partialPath[0]}"`); } else { try { return require(path); } catch(e) { Handler.error(e); } } } protected requireAndCreate(app, component: string, asObject: boolean = 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.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.error(`the "${component}" does not exist or it has no "export"`); } }); } } else { Handler.error(`Cannot find "${component}" in "${app.name}" application directory.`); } } protected require(path: string, isDefault: boolean = 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 dropIfNotDefault(`${path}/${file}`); }); } else { return files.map(file => { return require(`${path}/${file}`); }); } } } protected controllersManager: ControllersManager; protected routerController: RouterController; protected modelsManager: ModelsManager; protected router: Router; protected controllers: any; protected models: any; protected ['api-prefix']: string = 'v'; }