Skip to content

Commit 2f323b9

Browse files
authored
Merge pull request #879 from motdotla/smarter-keys-logged
correctly track which keys were written to process.env
2 parents 3010120 + 2561ad1 commit 2f323b9

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/main.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export interface DotenvParseOutput {
66
[name: string]: string;
77
}
88

9+
export interface DotenvPopulateOutput {
10+
[name: string]: string;
11+
}
12+
913
/**
1014
* Parses a string or buffer in the .env file format into an object.
1115
*
@@ -144,10 +148,14 @@ export function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput;
144148
* @param processEnv - the target JSON object. in most cases use process.env but you can also pass your own JSON object
145149
* @param parsed - the source JSON object
146150
* @param options - additional options. example: `{ quiet: false, debug: true, override: false }`
147-
* @returns {void}
151+
* @returns an object with the keys and values that were actually set
148152
*
149153
*/
150-
export function populate(processEnv: DotenvPopulateInput, parsed: DotenvPopulateInput, options?: DotenvConfigOptions): void;
154+
export function populate(
155+
processEnv: DotenvPopulateInput,
156+
parsed: DotenvPopulateInput,
157+
options?: DotenvConfigOptions
158+
): DotenvPopulateOutput;
151159

152160
/**
153161
* Decrypt ciphertext

lib/main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ function configDotenv (options) {
257257
processEnv = options.processEnv
258258
}
259259

260-
DotenvModule.populate(processEnv, parsedAll, options)
260+
const populated = DotenvModule.populate(processEnv, parsedAll, options)
261261

262262
if (debug || !quiet) {
263-
const keysCount = Object.keys(parsedAll).length
263+
const keysCount = Object.keys(populated).length
264264
const shortPaths = []
265265
for (const filePath of optionPaths) {
266266
try {
@@ -274,7 +274,7 @@ function configDotenv (options) {
274274
}
275275
}
276276

277-
_log(`injecting env (${keysCount}) from ${shortPaths.join(',')}🔐 encrypt with dotenvx: https://dotenvx.com`)
277+
_log(`injecting env (${keysCount}) from ${shortPaths.join(',')}[tip] encrypt with dotenvx: https://dotenvx.com`)
278278
}
279279

280280
if (lastError) {
@@ -338,6 +338,7 @@ function decrypt (encrypted, keyStr) {
338338
function populate (processEnv, parsed, options = {}) {
339339
const debug = Boolean(options && options.debug)
340340
const override = Boolean(options && options.override)
341+
const populated = {}
341342

342343
if (typeof parsed !== 'object') {
343344
const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate')
@@ -350,6 +351,7 @@ function populate (processEnv, parsed, options = {}) {
350351
if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
351352
if (override === true) {
352353
processEnv[key] = parsed[key]
354+
populated[key] = parsed[key]
353355
}
354356

355357
if (debug) {
@@ -361,8 +363,11 @@ function populate (processEnv, parsed, options = {}) {
361363
}
362364
} else {
363365
processEnv[key] = parsed[key]
366+
populated[key] = parsed[key]
364367
}
365368
}
369+
370+
return populated
366371
}
367372

368373
const DotenvModule = {

0 commit comments

Comments
 (0)