Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose configDotenv as public method #744

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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