Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface DotenvParseOutput {
[name: string]: string;
}

export interface DotenvPopulateOutput {
[name: string]: string;
}

/**
* Parses a string or buffer in the .env file format into an object.
*
Expand Down Expand Up @@ -144,10 +148,14 @@ export function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput;
* @param processEnv - the target JSON object. in most cases use process.env but you can also pass your own JSON object
* @param parsed - the source JSON object
* @param options - additional options. example: `{ quiet: false, debug: true, override: false }`
* @returns {void}
* @returns an object with the keys and values that were actually set
*
*/
export function populate(processEnv: DotenvPopulateInput, parsed: DotenvPopulateInput, options?: DotenvConfigOptions): void;
export function populate(
processEnv: DotenvPopulateInput,
parsed: DotenvPopulateInput,
options?: DotenvConfigOptions
): DotenvPopulateOutput;

/**
* Decrypt ciphertext
Expand Down
11 changes: 8 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ function configDotenv (options) {
processEnv = options.processEnv
}

DotenvModule.populate(processEnv, parsedAll, options)
const populated = DotenvModule.populate(processEnv, parsedAll, options)

if (debug || !quiet) {
const keysCount = Object.keys(parsedAll).length
const keysCount = Object.keys(populated).length
const shortPaths = []
for (const filePath of optionPaths) {
try {
Expand All @@ -274,7 +274,7 @@ function configDotenv (options) {
}
}

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

if (lastError) {
Expand Down Expand Up @@ -338,6 +338,7 @@ function decrypt (encrypted, keyStr) {
function populate (processEnv, parsed, options = {}) {
const debug = Boolean(options && options.debug)
const override = Boolean(options && options.override)
const populated = {}

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

if (debug) {
Expand All @@ -361,8 +363,11 @@ function populate (processEnv, parsed, options = {}) {
}
} else {
processEnv[key] = parsed[key]
populated[key] = parsed[key]
}
}

return populated
}

const DotenvModule = {
Expand Down