Skip to content

Commit a879e95

Browse files
committed
fix(config): reorder configuration file lookup
loadEnvFile will not overwrite environment variables that were already set. Therefore, it is important to defer reading .env files that might be in the current directory or a parent directory until the point the passed configuration file was read and has set the more specific settings. The cascade is as follows 1. set environment variables 2. environment variables in .evn files specified by passing it via --config 3. look for .env files in the current directory 4. check parent directories for a .env file
1 parent c7210ea commit a879e95

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

utility/configure.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { readFileSync, existsSync } from 'node:fs'
22
import { loadEnvFile } from 'node:process'
3-
import { findUpSync } from 'find-up-simple'
4-
// read connection options from .env file in current working directory or any parent directory
5-
// existing environment variables take precedence
6-
const path = findUpSync('.env')
7-
if (path) {
8-
loadEnvFile(path)
9-
}
103

114
/**
125
* read configuration files in .env, .existdb.json or node-exist format

utility/connection.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import { readOptionsFromEnv } from '@existdb/node-exist'
1313
import { defaultConnectionOptions } from '@existdb/node-exist/util/connect.js'
1414
import { getAccountInfo, AdminGroup } from '../utility/account.js'
15+
import { findUpSync } from 'find-up-simple'
16+
import { loadEnvFile } from 'node:process'
1517

1618
/**
1719
* @typedef { import("@existdb/node-exist").NodeExistXmlRpcClient } NodeExistXmlRpcClient
@@ -26,6 +28,13 @@ import { getAccountInfo, AdminGroup } from '../utility/account.js'
2628
* @returns {object} argv with connection options resolved
2729
*/
2830
export function readConnection (argv) {
31+
// read connection options from .env file in current working directory or any parent directory
32+
// existing environment variables take precedence
33+
const path = findUpSync('.env')
34+
if (path) {
35+
loadEnvFile(path)
36+
}
37+
2938
const connectionOptions = Object.assign({}, defaultConnectionOptions, readOptionsFromEnv(), argv.connectionOptions)
3039
if (argv.verbose) {
3140
const { protocol, host, port } = connectionOptions

0 commit comments

Comments
 (0)