@johnmmackey/ms-utils 4.0.1 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Version 4.1.0
2
+ Added functionality:
3
+ * uses ```dotenv``` to load a local .env file
4
+ * pushes configuration into environment variables
5
+
1
6
  ## Version 4.0.0
2
7
  BREAKING CHANGES
3
8
  * @johnmmackey/amqp-utils removed as a dependency
package/README.md CHANGED
@@ -28,7 +28,6 @@ let x = Config.get('key'); //typeof string
28
28
  let y = Config.getObj('key'); // typeof Object
29
29
 
30
30
  sendmail(
31
- [
32
31
  {"email": "user1@gmail.com","name": "John Smith"},
33
32
  {"email": "user2@yahoo.com","name": "Jane Doe"},
34
33
  {"email": "user3@aol.com","name": "Karen from Texas"}
@@ -42,6 +41,14 @@ sendmail(
42
41
  .then( () => console.log('Success'))
43
42
  .catch( err => console.log(err))
44
43
  ```
44
+
45
+ ##
46
+ ```Config.load``` now has 2 side effects:
47
+ * uses ```dotenv``` to load a .env file in the current working directory into the environment
48
+ * pushes all items loaded from etcd into the environment.
49
+
50
+ The order of precedence: .env > etcd > external set environment
51
+
45
52
  ## Future
46
53
  * Add local queueing for log events when AMQP is not operative.
47
54
  * Configurable logging setup (drivers)
package/lib/config.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const { Etcd3 } = require('etcd3');
4
+ const dotenv = require('dotenv');
4
5
  const client = new Etcd3({ hosts: process.env.ETCD_URI || 'http://localhost:2379' });
5
6
 
6
7
 
@@ -24,7 +25,14 @@ async function tryLoad(serviceName) {
24
25
  }
25
26
 
26
27
  async function load(serviceName) {
27
- return Promise.retry(10, tryLoad.bind(this, serviceName), 5000)
28
+ await Promise.retry(10, tryLoad.bind(this, serviceName), 5000);
29
+
30
+ // as a side-effect, load these into process.env
31
+ dotenv.populate(process.env, config, { override: true });
32
+
33
+ // load any .env file that may be present into the environment
34
+ dotenv.config({ override: true });
35
+ return config;
28
36
  }
29
37
 
30
38
  function get(k) {
@@ -34,10 +42,10 @@ function get(k) {
34
42
  function getObj(k) {
35
43
  try {
36
44
  return JSON.parse(config[k]);
37
- }
38
- catch (err) {
39
- return null;
40
- }
45
+ }
46
+ catch (err) {
47
+ return null;
48
+ }
41
49
  }
42
50
 
43
51
  module.exports = { load, get, getObj }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmmackey/ms-utils",
3
- "version": "4.0.1",
3
+ "version": "4.1.1",
4
4
  "description": "Utility functions for Microservice Architecture",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,7 @@
14
14
  "license": "ISC",
15
15
  "homepage": "https://bitbucket.org/52westlabs/ms-utils#readme",
16
16
  "dependencies": {
17
+ "dotenv": "^16.4.7",
17
18
  "etcd3": "^1.1.2",
18
19
  "redis": "^4.6.12",
19
20
  "uuid": "^9.0.1"
@@ -0,0 +1,9 @@
1
+ const { Config } = require('../index.js');
2
+
3
+ ( async () => {
4
+ console.log('env: before network load', process.env)
5
+ let config = await Config.load('mailrelay')
6
+ console.log('env: after network and file load', process.env)
7
+
8
+
9
+ })()