Skip to content

Commit

Permalink
feat: adding the ability to force overrides
Browse files Browse the repository at this point in the history
relates to #489
  • Loading branch information
mrsteele committed May 5, 2023
1 parent 97a864d commit 95d59a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -118,6 +118,7 @@ Use the following properties to configure your instance.
* **defaults** (`false`) - Adds support for `dotenv-defaults`. If set to `true`, uses `./.env.defaults`. If a string, uses that location for a defaults file. Read more at [npm](https://www.npmjs.com/package/dotenv-defaults).
* **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing).
* **prefix** (`'process.env.'`) - The prefix to use before the name of your env variables.
* **force** (`false`) - This will override existing env variables with anything you pass in from your file.

The following example shows how to set any/all arguments.

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -57,7 +57,7 @@ class Dotenv {
const { env, blueprint } = this.getEnvs()

Object.keys(blueprint).forEach(key => {
const value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key]
const value = !this.config.force && Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key]

const isMissing = typeof value === 'undefined' || value === null ||
(!allowEmptyValues && value === '')
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Expand Up @@ -236,6 +236,22 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
})
})

describe('force configuration', () => {
test('Should override if forced', (done) => {
// defaults
// TEST=nope
// TEST2=youcanseethis

// missingone
// TEST2=Hello
expectResultsToContainReplacements(
new DotenvPlugin({ force: true }),
{...defaultEnvResult, ...missingOneResult},

Check failure on line 249 in test/index.test.js

View workflow job for this annotation

GitHub Actions / lint

A space is required after '{'

Check failure on line 249 in test/index.test.js

View workflow job for this annotation

GitHub Actions / lint

A space is required before '}'

Check failure on line 249 in test/index.test.js

View workflow job for this annotation

GitHub Actions / Standard

test/index.test.js#L249

[object-curly-spacing] A space is required after '{'.

Check failure on line 249 in test/index.test.js

View workflow job for this annotation

GitHub Actions / Standard

test/index.test.js#L249

[object-curly-spacing] A space is required before '}'.
done
)
})
})

describe('Defaults configuration', () => {
test('should support default configurations', (done) => {
expectResultsToContainReplacements(
Expand Down

0 comments on commit 95d59a5

Please sign in to comment.