@egodigital/nef
Version:
Managed Extensibility Framework like library written for Node.js
87 lines • 3.08 kB
JavaScript
;
/**
* 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.ApplicationCatalog = 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 of the current application (e.g.).
*/
class ApplicationCatalog extends CatalogBase_1.CatalogBase {
/**
* Initializes a new instance of that class.
*
* @param {ApplicationCatalogOptions} [options] The custom options.
*/
constructor(options) {
super();
this.options = options;
if (_.isNil(options)) {
options = {};
}
this.options = options;
}
/**
* Gets the underlying application (process),
*/
get application() {
return this.options.application ||
process;
}
/** @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: path.dirname(this.application.mainModule.filename),
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.ApplicationCatalog = ApplicationCatalog;
//# sourceMappingURL=ApplicationCatalog.js.map