UNPKG

magnet-core

Version:

Magnet's core, a simple module loader.

63 lines 2.68 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()); }); }; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); const bunyan = require("bunyan"); const magnet_1 = require("./magnet"); const process_1 = require("./process"); const utils_1 = require("./utils"); const copy_config_1 = require("./copy-config"); const magnet_2 = require("./config/magnet"); __export(require("./loader")); function MagnetFn(modules) { return __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(modules)) { throw new TypeError('Modules should pass in as array'); } else if (!modules.length) { throw new Error('No modules provided'); } let app = { log: bunyan.createLogger({ name: 'magnet-core' }) }; app.magnet = new magnet_1.default(app); process.once('uncaughtException', utils_1.errorHandler.bind(null, app)); process.once('SIGUSR2', utils_1.errorHandler.bind(null, app)); process.once('SIGINT', utils_1.errorHandler.bind(null, app)); try { const tasks = yield process_1.performTasks(app, modules); app.magnet.teardowns = tasks.teardowns; // Check failed setup if (tasks.fails.length) { for (const fail of tasks.fails) { app.log.error(fail); } throw new Error('Some modules cannot setup'); } let autoCopyConfig = magnet_2.default.autoCopyConfig; autoCopyConfig = (app.config && app.config.magnet && app.config.magnet.autoCopyConfig) ? app.config.magnet.autoCopyConfig : autoCopyConfig; if (autoCopyConfig) { yield copy_config_1.default(); } app.log.info('Ready'); return app; } catch (err) { app.log.error(err.stack); throw err; } }); } exports.default = MagnetFn; //# sourceMappingURL=index.js.map