UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

90 lines 3.32 kB
"use strict"; 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Loader = void 0; const glob = __importStar(require("glob")); const path = __importStar(require("path")); const container_1 = require("../container"); class Loader { constructor() { this.app = container_1.Container.get('app'); this.loadedComponents = new Map(); } async autoScanApp() { await this.scan(this.app.appPath); } async scan(absoluteFilePath) { var _a; const filePaths = glob.sync(path.join(absoluteFilePath, '**/*.@(ts|js)'), { nodir: true, matchBase: true }); for (const file of filePaths) { if (file.slice(-5) !== '.d.ts') { const Target = (await (_a = `${file}`, Promise.resolve().then(() => __importStar(require(_a))))); Object.keys(Target) .filter((k) => typeof Target[k] === 'function') .forEach((k) => { this.load(Target[k], file); }); } } return this; } getComponentsByType(type) { var _a; return (_a = this.loadedComponents.get(type)) === null || _a === void 0 ? void 0 : _a.map(item => item.target); } getComponentsWithFileByType(type) { return this.loadedComponents.get(type); } load(Target, file) { var _a, _b; if (!Target || !Target.prototype) return; const isIgnore = Reflect.getMetadata('ignore', Target); const env = Reflect.getMetadata('env', Target); if (isIgnore === true) return; if (env && env !== this.app.getEnv()) return; const type = (_a = Reflect.getMetadata('type', Target)) !== null && _a !== void 0 ? _a : 'component'; if (this.loadedComponents.has(type)) { (_b = this.loadedComponents.get(type)) === null || _b === void 0 ? void 0 : _b.push({ target: Target, file }); } else { this.loadedComponents.set(type, [{ target: Target, file }]); } return this; } } exports.Loader = Loader; //# sourceMappingURL=loader.js.map