All files / mod/config config.js

96.67% Statements 29/30
92.86% Branches 13/14
100% Functions 7/7
96.67% Lines 29/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75      6x 6x 6x 6x   6x 6x 6x   6x           6x       6x         6x   38x   18x 17x   1x         9x   9x     9x         11x       11x 5x   11x   6x 5x   1x         8x 8x 7x         6x  
/**
 * This file must be in javascript because it wil be loaded by js scripts
 */
require("../../../configEnv");
const dotenv = require("dotenv");
const path = require("path");
const fs = require("fs");
 
let secrets = {};
try {
  secrets = dotenv.parse(fs.readFileSync(path.resolve(__dirname, "../../../config/secret/.env")));
} catch (e) {
  console.warn("config/secret/.env is missing");
}
 
/**
 * @type {VersedConfig.DynamicGetterDelegate}
 */
let dynamicGetterDelegate = null;
/**
 * @type {DefinedDynamicMap}
 */
const definedDynamics = {};
 
/**
 * @type {VersedConfig}
 */
const configModule = {
  env(name, defaultValue) {
    return {
      get: () => {
        if (typeof process.env[name] !== "undefined") {
          return process.env[name];
        }
        return defaultValue;
      },
    };
  },
  secret(name, defaultValue) {
    return {
      get: () => {
        Iif (typeof secrets[name] !== "undefined") {
          return secrets[name];
        }
        return defaultValue;
      },
    };
  },
  dynamic(version, name, defaultValue) {
    definedDynamics[name] = {
      defaultValue,
      version,
    };
    if (dynamicGetterDelegate) {
      dynamicGetterDelegate.onDefine(version, name, defaultValue);
    }
    return {
      get() {
        if (dynamicGetterDelegate && dynamicGetterDelegate.get) {
          return dynamicGetterDelegate.get(name, defaultValue);
        }
        return defaultValue;
      },
    };
  },
  async setDelegate(delegate) {
    dynamicGetterDelegate = delegate;
    if (delegate && delegate.init) {
      await delegate.init(definedDynamics);
    }
  },
};
 
module.exports = configModule;