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