UNPKG

@egodigital/nef

Version:

Managed Extensibility Framework like library written for Node.js

95 lines 3.43 kB
"use strict"; /** * This file is part of the @egodigital/nef distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/nef is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/nef is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectoryCatalog = void 0; const _ = require("lodash"); const fastGlob = require("fast-glob"); const path = require("path"); const CatalogBase_1 = require("./CatalogBase"); const util_1 = require("../util"); /** * A catalog based on one or more JavaScript modules in a directory. */ class DirectoryCatalog extends CatalogBase_1.CatalogBase { /** * Initializes a new instance of that class. * * @param {string} directory The path to the directory. * @param {FileCatalogOptions} [options] Custom options. */ constructor(directory, options) { super(); this.directory = directory; this.options = options; this.directory = util_1.toStringSafe(directory); if (_.isNil(this.options)) { this.options = {}; } let cwd = util_1.toStringSafe(this.options.cwd); if ('' === cwd.trim()) { cwd = process.cwd(); } else { if (!path.isAbsolute(cwd)) { cwd = path.join(process.cwd(), cwd); } } if (!path.isAbsolute(this.directory)) { this.directory = path.join(cwd, this.directory); } this.directory = path.resolve(this.directory); } /** @inheritdoc */ async getClasses() { return this.loadClasses(await fastGlob(this.getPatterns(), this.getGlobOptions())); } /** @inheritdoc */ getClassesSync() { return this.loadClasses(fastGlob.sync(this.getPatterns(), this.getGlobOptions())); } getGlobOptions() { return { absolute: true, cwd: this.directory, ignore: util_1.asArray(this.options.exclude) .map(x => util_1.toStringSafe(x)) .filter(x => '' !== x.trim()), onlyFiles: true, unique: true, }; } getPatterns() { const PATTERNS = util_1.asArray(this.options.patterns) .map(x => util_1.toStringSafe(x)) .filter(x => '' !== x.trim()); if (!PATTERNS.length) { PATTERNS.push('*' + path.extname(process.mainModule.filename)); } return PATTERNS; } loadClasses(files) { const CLASS_LIST = []; files.forEach(f => { const MODULE = util_1.loadModule(path.join(path.dirname(f), path.basename(f, path.extname(f))), true); CLASS_LIST.push(...util_1.getClassesFromObject(MODULE)); }); return CLASS_LIST; } } exports.DirectoryCatalog = DirectoryCatalog; //# sourceMappingURL=DirectoryCatalog.js.map