UNPKG

magnet-core

Version:

Magnet's core, a simple module loader.

63 lines 2.46 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); class Module { constructor(app, options = {}) { this.app = app; this.log = app.log; this.options = options; this.name = this.constructor.name; this.init(); this.originalModuleName = this.moduleName; this.namespace = options.namespace; // Until es7 have a way to initialize property if (this.moduleName) { this.moduleName = this.moduleName.replace(/@|\/|\-/g, '_'); this.config = this.prepareConfig(this.moduleName, (typeof this.defaultConfig === 'string') ? require(`${this.defaultConfig}/config/${this.moduleName}`).default : this.defaultConfig); } } init() { } prepareConfig(ns = '', defaultConfig = {}) { if (lodash_1.isFunction(defaultConfig)) { defaultConfig = defaultConfig(this.app); } if (ns) { return Object.assign(defaultConfig, this.app.config[ns], this.options); } else { return Object.assign(defaultConfig, this.options); } } insert(currentModule, ns = '') { if (this.namespace) { ns += this.namespace; } if (!ns) { ns += this.moduleName; } if (this.app[ns]) { this.log.warn(`${this.name}: Module ${ns} already exist, overridden anyway`); } this.app[ns] = currentModule; } setup() { return __awaiter(this, void 0, void 0, function* () { this.log.warn(`Module ${this.name} missing setup function`); }); } teardown() { return __awaiter(this, void 0, void 0, function* () { }); } } exports.Module = Module; //# sourceMappingURL=module.js.map