dino-core
Version:
A dependency injection framework for NodeJS applications
74 lines • 3.23 kB
JavaScript
;
// Copyright 2018 Quirino Brizi [quirino.brizi@gmail.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvironmentConfigurationBuilder = void 0;
const tslib_1 = require("tslib");
const app_root_path_1 = tslib_1.__importDefault(require("app-root-path"));
const nconf_1 = tslib_1.__importDefault(require("nconf"));
const helper_1 = require("../helper/helper");
const Logger_1 = require("../Logger");
const environment_configuration_1 = require("../model/environment.configuration");
/**
* Load the configuration and add all variables to the application context
* @typedef {EnvironmentConfigurationBuilder}
* @public
* @since 0.2.2
*/
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class EnvironmentConfigurationBuilder {
/**
* Load the configuration defined for the service. Configuration can be provide as:
* - command line arguments
* - environment variables
* - JSON file, path for the JSON file should be provided
* @param {String} path the patch for the configuration file relative to the root of the project
*
* @public
* @static
*/
static load(appConfigPath) {
// configure nconf to load from command line, environment variable and file.
const conf = nconf_1.default.argv().env();
let path = appConfigPath ?? conf.get('dino.config.path') ?? conf.get('DINO_CONFIG_PATH');
if (path !== undefined && path.charAt(0) !== '/') {
path = `${app_root_path_1.default}/${path}`;
}
Logger_1.Logger.debug(`** loading configuration from ${path}`);
if (path !== undefined) {
conf.file(`${Date.now()}`, { file: path });
}
EnvironmentConfigurationBuilder.loadAdditionalConfigurationLoaders(conf);
return new environment_configuration_1.EnvironmentConfiguration(conf);
}
/**
* Load additional configuration if required
*
* @param {Any} conf the provided configuration
*
* @static
* @private
*/
static loadAdditionalConfigurationLoaders(conf) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const additionalConfigurations = helper_1.Helper.getVariable(conf, 'dino:configuration', {});
for (const key in additionalConfigurations) {
const element = additionalConfigurations[key];
require(element.module);
nconf_1.default.use(key, (element.options ?? {}));
}
}
}
exports.EnvironmentConfigurationBuilder = EnvironmentConfigurationBuilder;
//# sourceMappingURL=EnvironmentConfigurationBuilder.js.map