From ded6c92a87aa8f715a5f81ca2a8ff6bb636b7ff2 Mon Sep 17 00:00:00 2001
From: "Dominik.Sigmund" <dominik.sigmund@br.de>
Date: Tue, 23 Jan 2024 08:33:45 +0100
Subject: [PATCH] Added Prefix option

---
 README.md                                | 11 ++++++++++-
 examples/env_prefix/config.defaults.json |  6 ++++++
 examples/env_prefix/config.json          |  6 ++++++
 examples/env_prefix/index.js             |  4 ++++
 index.js                                 |  5 +++--
 index.test.js                            | 23 +++++++++++++++++++++++
 package.json                             |  2 +-
 7 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100755 examples/env_prefix/config.defaults.json
 create mode 100755 examples/env_prefix/config.json
 create mode 100755 examples/env_prefix/index.js

diff --git a/README.md b/README.md
index 40ad82c..997f217 100755
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@ Simple Config with ENV and Files Support.
 ## Usage
 
 `const Config = require('@libs/config')`  
-`let config = new Config([basePath])`
+`let config = new Config([basePath], [EnvPrefix])`
 
 Then config is your config object. (Use it like config.setting)
 
@@ -49,6 +49,9 @@ This makes *_show* a reserved keyword
 If you give a basePath, the config-Files are used from there.  
 Else the main dir of the application will be used.
 
+YOu can give a prefix for the enviroment variables.  
+e.g. if you give "PREFIX" as prefix, the setting "setting.deep.key" can be reached with *PREFIX_SETTING_DEEP_KEY*
+
 ### Values
 
 You may use direct values like strings or numbers:
@@ -90,6 +93,12 @@ You can also use files to read the value from. This makes the config compatible
 
 `SETTING=overwritten-by-env node examples/env/index.js`
 
+### enviroment variables with prefix
+
+(Enviroment set by command to not pollute your machine)
+
+`PREFIX_SETTING=overwritten-by-env node examples/env_prefix/index.js`
+
 ### use files to read from
 
 (Enviroment set by command to not pollute your machine)
diff --git a/examples/env_prefix/config.defaults.json b/examples/env_prefix/config.defaults.json
new file mode 100755
index 0000000..8d6cd81
--- /dev/null
+++ b/examples/env_prefix/config.defaults.json
@@ -0,0 +1,6 @@
+{
+  "setting":"value",
+  "another": {
+    "setting":"avalue"
+  }
+}
\ No newline at end of file
diff --git a/examples/env_prefix/config.json b/examples/env_prefix/config.json
new file mode 100755
index 0000000..de6fd1f
--- /dev/null
+++ b/examples/env_prefix/config.json
@@ -0,0 +1,6 @@
+{
+  "setting":"overwritten",
+  "another": {
+    "more":"settings"
+  }
+}
\ No newline at end of file
diff --git a/examples/env_prefix/index.js b/examples/env_prefix/index.js
new file mode 100755
index 0000000..048faca
--- /dev/null
+++ b/examples/env_prefix/index.js
@@ -0,0 +1,4 @@
+const Config = require('../../index')
+let config = new Config(undefined, 'PREFIX')
+
+console.log(JSON.stringify(config, undefined, 2))
\ No newline at end of file
diff --git a/index.js b/index.js
index a5d37ae..5c4aa1a 100755
--- a/index.js
+++ b/index.js
@@ -2,9 +2,10 @@ var fs = require('fs')
 var path = require('path')
 var merge = require('lodash.merge')
 
-module.exports = function(basePath = undefined) {
+module.exports = function(basePath = undefined, envPrefix = undefined) {
   let configDefaults
   let configLocal
+  let envPrefixUpper = envPrefix ? envPrefix.toUpperCase() + "_" : ''
   if (basePath) {
     configDefaults = path.join(basePath, 'config.defaults.json')
     configLocal = path.join(basePath, 'config.json')
@@ -58,7 +59,7 @@ module.exports = function(basePath = undefined) {
     let keys = objectDeepKeys(this)
     for (let index = 0; index < keys.length; index++) {
       const element = keys[index]
-      let env = process.env[element.toUpperCase().replace(/\./g, '_')]
+      let env = process.env[envPrefixUpper + element.toUpperCase().replace(/\./g, '_')]
       if (env) {
         env = (env == 'true') ? true : env
         env = (env == 'false') ? false : env
diff --git a/index.test.js b/index.test.js
index f2905a2..39bea63 100755
--- a/index.test.js
+++ b/index.test.js
@@ -90,6 +90,29 @@ describe('config', function() {
     expect(config.another.setting).toBe('avalue')
   })
 
+  it('should have all values with preference to env and prefix', async function() {
+    await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
+    await fs.writeFile('config.json', JSON.stringify(jsonLocals))
+    
+    process.env['P_SETTING'] = 'overwritten-by-env'
+    process.env['P_ANOTHER_MORE'] = 'false'
+    process.env['P_EVEN_DEEPER_KEY'] = 'true'
+    
+    let config = new Config(undefined, 'p')
+
+    await fs.unlink('config.json')
+    await fs.unlink('config.defaults.json')
+
+    delete process.env['P_SETTING']
+    delete process.env['P_ANOTHER_MORE']
+    delete process.env['P_EVEN_DEEPER_KEY']
+
+    expect(config.setting).toBe('overwritten-by-env')
+    expect(config.another.more).toBe(false)
+    expect(config.even.deeper.key).toBe(true)
+    expect(config.another.setting).toBe('avalue')
+  })
+
   it('should read in a file when given', async function() {
     jsonLocals.setting = 'file:file.txt'
     await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
diff --git a/package.json b/package.json
index a6b4e7c..097153d 100755
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@libs/config",
-  "version": "1.12.4",
+  "version": "1.13.0",
   "description": "Simple Config with ENV Support",
   "main": "index.js",
   "scripts": {
-- 
GitLab