UNPKG

ern-api-gen

Version:

Electrode Native API generator

53 lines 1.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const os_1 = __importDefault(require("os")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const LoggerFactory_1 = __importDefault(require("./LoggerFactory")); const File_1 = __importDefault(require("./File")); const StringUtils_1 = require("./StringUtils"); const Log = LoggerFactory_1.default.getLogger('ServiceLoader'); const tryNewRequire = mod => { try { const Clz = require(mod).default; return new Clz(); } catch (e) { Log.warn(`could not require ${mod}`, e); } }; exports.SEARCH_PATH = [path_1.default.join(__dirname, '..', '..', 'resources')]; exports.default = { load(className) { const ret = []; const lines = []; for (const searchPath of exports.SEARCH_PATH) { const meta = new File_1.default(searchPath, 'META-INF', 'services', className); if (!meta.exists()) { continue; } try { lines.push(...fs_1.default.readFileSync(meta.getPath(), 'utf8').split(os_1.default.EOL)); } catch (e) { Log.warn(`Error loading ${className}`, e); return ret; } } for (const line of lines) { const [mod, comment] = line.split('#', 2); if (StringUtils_1.isEmpty(mod)) { continue; } const conf = tryNewRequire(path_1.default.join(__dirname, '..', ...mod.split('.'))); if (conf) { ret.push(conf); } } return ret; }, }; //# sourceMappingURL=ServiceLoader.js.map