From 9de0aa89b6fb807aa3ddd4d3c174f02c3a16ec2c Mon Sep 17 00:00:00 2001 From: Dominik Sigmund <dominik.sigmund@br.de> Date: Mon, 1 Mar 2021 18:11:46 +0100 Subject: [PATCH] Added optional BasePath --- README.md | 5 ++++- index.d.ts | 9 ++++++++- index.js | 15 +++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c3664d6..305e6ba 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Simple Config with ENV Support. ## Usage `const Config = require('@plastdev/config')` -`let config = new Config()` +`let config = new Config([basePath])` Then config is your config object. (Use it like config.setting) @@ -28,6 +28,9 @@ You may use the function _reload()_ to reload the config from all sources. This makes *reload* 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. + ## Examples ### Only config.defaults.json diff --git a/index.d.ts b/index.d.ts index 52fd8ac..ee4d70d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1 +1,8 @@ -declare module 'config'; \ No newline at end of file +export as namespace Config; + +export = Config; + +declare class Config { + constructor(); + reload(): void; +} \ No newline at end of file diff --git a/index.js b/index.js index 049096b..28a6813 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,17 @@ var fs = require('fs') var path = require('path') var merge = require('lodash.merge') -module.exports = function() { - let configDefaults = path.join(path.dirname(require.main.filename), 'config.defaults.json') - let configLocal = path.join(path.dirname(require.main.filename), 'config.json') - +module.exports = function(basePath = undefined) { + let configDefaults + let configLocal + if (basePath) { + configDefaults = path.join(basePath, 'config.defaults.json') + configLocal = path.join(basePath, 'config.json') + } else { + configDefaults = path.join(path.dirname(require.main.filename), 'config.defaults.json') + configLocal = path.join(path.dirname(require.main.filename), 'config.json') + } + let config = {} const objectDeepKeys = function (obj) { -- GitLab