From d0675f2b5bc860bc49c97dd17a7780fa9a4b3879 Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Wed, 31 May 2023 08:22:55 -0700 Subject: [PATCH 1/2] Expose configDotenv as public method --- CHANGELOG.md | 6 ++++++ lib/main.d.ts | 13 ++++++++++++- lib/main.js | 12 ++++++------ tests/test-config-vault.js | 2 +- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31228df2..2118beb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. See [standa ## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.1.0...master) +## [16.1.2](https://github.com/motdotla/dotenv/compare/v16.1.1...v16.1.2) (2023-05-31) + +### Changed + +- Exposed private function `_configDotenv` as `configDotenv`. + ## [16.1.1](https://github.com/motdotla/dotenv/compare/v16.1.0...v16.1.1) (2023-05-30) ### Added diff --git a/lib/main.d.ts b/lib/main.d.ts index 35ca384f..a2c8d073 100644 --- a/lib/main.d.ts +++ b/lib/main.d.ts @@ -90,7 +90,7 @@ export interface DotenvPopulateInput { } /** - * Loads `.env` file contents into process.env. + * Loads `.env` file contents into process.env by default. If `DOTENV_KEY` is present, it smartly attempts to load encrypted `.env.vault` file contents into process.env. * * See https://docs.dotenv.org * @@ -100,6 +100,17 @@ export interface DotenvPopulateInput { */ export function config(options?: DotenvConfigOptions): DotenvConfigOutput; +/** + * Loads `.env` file contents into process.env. + * + * See https://docs.dotenv.org + * + * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', debug: true, override: false }` + * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } } + * + */ +export function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput; + /** * Loads `source` json contents into `target` like process.env. * diff --git a/lib/main.js b/lib/main.js index 1f4633fa..367d02e7 100644 --- a/lib/main.js +++ b/lib/main.js @@ -51,7 +51,7 @@ function _parseVault (options) { const vaultPath = _vaultPath(options) // Parse .env.vault - const result = DotenvModule._configDotenv({ path: vaultPath }) + const result = DotenvModule.configDotenv({ path: vaultPath }) if (!result.parsed) { throw new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`) } @@ -167,7 +167,7 @@ function _configVault (options) { return { parsed } } -function _configDotenv (options) { +function configDotenv (options) { let dotenvPath = path.resolve(process.cwd(), '.env') let encoding = 'utf8' const debug = Boolean(options && options.debug) @@ -203,14 +203,14 @@ function config (options) { // fallback to original dotenv if DOTENV_KEY is not set if (_dotenvKey().length === 0) { - return DotenvModule._configDotenv(options) + return DotenvModule.configDotenv(options) } // dotenvKey exists but .env.vault file does not exist if (!fs.existsSync(vaultPath)) { _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`) - return DotenvModule._configDotenv(options) + return DotenvModule.configDotenv(options) } return DotenvModule._configVault(options) @@ -277,7 +277,7 @@ function populate (processEnv, parsed, options = {}) { } const DotenvModule = { - _configDotenv, + configDotenv, _configVault, _parseVault, config, @@ -286,7 +286,7 @@ const DotenvModule = { populate } -module.exports._configDotenv = DotenvModule._configDotenv +module.exports.configDotenv = DotenvModule.configDotenv module.exports._configVault = DotenvModule._configVault module.exports._parseVault = DotenvModule._parseVault module.exports.config = DotenvModule.config diff --git a/tests/test-config-vault.js b/tests/test-config-vault.js index 011f1525..16d3e774 100644 --- a/tests/test-config-vault.js +++ b/tests/test-config-vault.js @@ -83,7 +83,7 @@ t.test('throws not found if .env.vault is empty', ct => { t.test('throws missing data when somehow parsed badly', ct => { ct.plan(1) - const configDotenvStub = sinon.stub(dotenv, '_configDotenv').returns({ parsed: undefined }) + const configDotenvStub = sinon.stub(dotenv, 'configDotenv').returns({ parsed: undefined }) try { dotenv.config({ path: testPath }) From 53bbc1f64448dbbecb9b6baecd1fd0c6ec8661eb Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Wed, 31 May 2023 08:24:47 -0700 Subject: [PATCH 2/2] update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2118beb6..89208a99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. See [standa ### Changed -- Exposed private function `_configDotenv` as `configDotenv`. +- Exposed private function `_configDotenv` as `configDotenv`. [#744](https://github.com/motdotla/dotenv/pull/744) ## [16.1.1](https://github.com/motdotla/dotenv/compare/v16.1.0...v16.1.1) (2023-05-30)