Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

environments.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @fileoverview Environments manager
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const envs = require("../../conf/environments");
  10. //------------------------------------------------------------------------------
  11. // Public Interface
  12. //------------------------------------------------------------------------------
  13. class Environments {
  14. /**
  15. * create env context
  16. */
  17. constructor() {
  18. this._environments = new Map();
  19. this.load();
  20. }
  21. /**
  22. * Loads the default environments.
  23. * @returns {void}
  24. * @private
  25. */
  26. load() {
  27. Object.keys(envs).forEach(envName => {
  28. this._environments.set(envName, envs[envName]);
  29. });
  30. }
  31. /**
  32. * Gets the environment with the given name.
  33. * @param {string} name The name of the environment to retrieve.
  34. * @returns {Object?} The environment object or null if not found.
  35. */
  36. get(name) {
  37. return this._environments.get(name) || null;
  38. }
  39. /**
  40. * Gets all the environment present
  41. * @returns {Object} The environment object for each env name
  42. */
  43. getAll() {
  44. return Array.from(this._environments).reduce((coll, env) => {
  45. coll[env[0]] = env[1];
  46. return coll;
  47. }, {});
  48. }
  49. /**
  50. * Defines an environment.
  51. * @param {string} name The name of the environment.
  52. * @param {Object} env The environment settings.
  53. * @returns {void}
  54. */
  55. define(name, env) {
  56. this._environments.set(name, env);
  57. }
  58. /**
  59. * Imports all environments from a plugin.
  60. * @param {Object} plugin The plugin object.
  61. * @param {string} pluginName The name of the plugin.
  62. * @returns {void}
  63. */
  64. importPlugin(plugin, pluginName) {
  65. if (plugin.environments) {
  66. Object.keys(plugin.environments).forEach(envName => {
  67. this.define(`${pluginName}/${envName}`, plugin.environments[envName]);
  68. });
  69. }
  70. }
  71. }
  72. module.exports = Environments;