Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1.  
  2.  
  3. /**
  4. * Export the current Environment.
  5. **/
  6. exports.env = null;
  7.  
  8.  
  9. /**
  10. * Export the last error that occured, if any.
  11. **/
  12. exports._lastError = null;
  13.  
  14.  
  15. /**
  16. * Storage for current settings object.
  17. **/
  18. var SETTINGS;
  19.  
  20.  
  21. /**
  22. * These are the various environment based settings.
  23. *
  24. * @return {Object} The loaded settings object.
  25. * @throws {Error} If the settings file cannot be found.
  26. **/
  27. var environments = {
  28. development: function() {
  29. return require('./settings/development').settings;
  30. },
  31.  
  32. production: function() {
  33. return require('./settings/production').settings;
  34. },
  35.  
  36. local: function() {
  37. return require('./settings/local').settings;
  38. }
  39. };
  40.  
  41.  
  42. /**
  43. * Cache for cache()
  44. **/
  45. var _cache = {};
  46.  
  47.  
  48. /**
  49. * Takes a given key and stores it in the cache
  50. * for faster lookup times.
  51. *
  52. * @param {String} key The key name.
  53. * @param {Mixed} value The value for the key.
  54. * @return {Mixed} If value: the value; Else key exists boolean.
  55. **/
  56. function cache(key, value) {
  57. if (!value) {
  58. return _cache[key];
  59. } else {
  60. return _cache[key] = value;
  61. }
  62. }
  63.  
  64.  
  65. /**
  66. * Parses a string to figure out its value within an object
  67. *
  68. * @param {Object} settings The settings object to search within.
  69. * @param {String} key The key to search for.
  70. * @param {Mixed} value If acting as a Setter, the value to set.
  71. * @return {Mixed} The current value of object at key.
  72. **/
  73. function resolve(settings, key, value) {
  74. if (_cache[key] === undefined || arguments.length === 3) {
  75. var current = settings;
  76. for (var node, parts = key.split('.');
  77. parts.length && (node = parts.shift()); ) {
  78. if (parts.length !== 0) {
  79. current = current[node] = (current[node] || {});
  80. } else {
  81. current = (value ? (current[node] = value) : current[node]);
  82. }
  83. }
  84. return cache(key, current);
  85. } else {
  86. return cache(key);
  87. }
  88. }
  89.  
  90.  
  91. /**
  92. * Sets the current environment and loads up the corresponding settings.
  93. *
  94. * @param {String} env The environment to switch to.
  95. * @return {Boolean} The environment was successfully switched.
  96. **/
  97. exports.setEnv = function(env) {
  98. if (typeof environments[env] === 'function') {
  99. try {
  100. SETTINGS = environments[env]();
  101. exports.env = env;
  102. return true;
  103. } catch (e) {
  104. exports._lastError = e;
  105. return false;
  106. }
  107. } else {
  108. return false;
  109. }
  110. };
  111.  
  112.  
  113. /**
  114. * Returns the currently loaded environment.
  115. *
  116. * @return {String} The currently loaded environment.
  117. * @this The `settings` object.
  118. **/
  119. exports.getEnv = function() {
  120. return exports.env;
  121. };
  122.  
  123.  
  124. /**
  125. * Set the current environment to that defined in process.env.SERVER_ENV;
  126. *
  127. * Defaults to 'local' environment.
  128. *
  129. * @return {Boolean} The environment was successfully switched.
  130. **/
  131. exports.setAutoEnv = function() {
  132. if (process.env.SERVER_ENV) {
  133. return exports.setEnv(process.env.SERVER_ENV);
  134. } else {
  135. return exports.setEnv('local');
  136. }
  137. };
  138.  
  139.  
  140. /**
  141. * Returns the value of the setting at `key`.
  142. *
  143. * If only we had proxies, of well, here's to ES Harmony
  144. *
  145. * @param {String} key An object tree, levels separated by a period.
  146. * @return {Mixed} The value for the key.
  147. **/
  148. exports.get = function(key) {
  149. if (!SETTINGS) {
  150. throw new Error('setEnv must be called before settings can be accessed');
  151. }
  152.  
  153. return resolve(SETTINGS, key);
  154. };
  155.  
  156.  
  157. /**
  158. * Sets the value of the setting at key with `value`.
  159. *
  160. * @param {String} key An object tree, levels separated by a period.
  161. * @param {Mixed} value The value to set the key to.
  162. **/
  163. exports.set = function(key, value) {
  164. if (!SETTINGS) {
  165. throw new Error('setEnv must be called before settings can be changed');
  166. }
  167.  
  168. resolve(SETTINGS, key, value);
  169. };
  170.  
  171.  
  172. /**
  173. * The toJSON method for JSON.stringify
  174. *
  175. * @return {String} The JSON encoded `SETTINGS` object.
  176. **/
  177. exports.toJSON = function() {
  178. return SETTINGS;
  179. };
Add Comment
Please, Sign In to add comment