Skip to content

Commit 95d59a5

Browse files
committed
feat: adding the ability to force overrides
relates to #489
1 parent 97a864d commit 95d59a5

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Use the following properties to configure your instance.
118118
* **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).
119119
* **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing).
120120
* **prefix** (`'process.env.'`) - The prefix to use before the name of your env variables.
121+
* **force** (`false`) - This will override existing env variables with anything you pass in from your file.
121122

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

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Dotenv {
5757
const { env, blueprint } = this.getEnvs()
5858

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

6262
const isMissing = typeof value === 'undefined' || value === null ||
6363
(!allowEmptyValues && value === '')

test/index.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
236236
})
237237
})
238238

239+
describe('force configuration', () => {
240+
test('Should override if forced', (done) => {
241+
// defaults
242+
// TEST=nope
243+
// TEST2=youcanseethis
244+
245+
// missingone
246+
// TEST2=Hello
247+
expectResultsToContainReplacements(
248+
new DotenvPlugin({ force: true }),
249+
{...defaultEnvResult, ...missingOneResult},
250+
done
251+
)
252+
})
253+
})
254+
239255
describe('Defaults configuration', () => {
240256
test('should support default configurations', (done) => {
241257
expectResultsToContainReplacements(

0 commit comments

Comments
 (0)