Skip to content

Commit

Permalink
Merge pull request #744 from motdotla/configDotenv
Browse files Browse the repository at this point in the history
Expose configDotenv as public method
  • Loading branch information
motdotla committed May 31, 2023
2 parents 66080bd + 53bbc1f commit 78e5a02
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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`. [#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)

### Added
Expand Down
13 changes: 12 additions & 1 deletion lib/main.d.ts
Expand Up @@ -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
*
Expand All @@ -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.
*
Expand Down
12 changes: 6 additions & 6 deletions lib/main.js
Expand Up @@ -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`)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -277,7 +277,7 @@ function populate (processEnv, parsed, options = {}) {
}

const DotenvModule = {
_configDotenv,
configDotenv,
_configVault,
_parseVault,
config,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test-config-vault.js
Expand Up @@ -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 })
Expand Down

0 comments on commit 78e5a02

Please sign in to comment.