Skip to content

Dotenv-Flow > 12.1.4 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
types
types
node_modules
1 change: 1 addition & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 24 additions & 25 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as fs from 'fs'
import * as path from 'path'
import * as dotenv from 'dotenv'
import * as dotEnvFlow from 'dotenv-flow'
import { expand as dotenvExpand } from 'dotenv-expand'

export type Env = { [key: string]: string }
Expand Down Expand Up @@ -33,17 +32,17 @@ export function processEnv(
process.env.__NEXT_PROCESSED_ENV = 'true'

const origEnv = Object.assign({}, process.env)
const parsed: dotenv.DotenvParseOutput = {}
const parsed: dotEnvFlow.DotenvParseOutput = {}

for (const envFile of loadedEnvFiles) {
try {
let result: dotenv.DotenvConfigOutput = {}
result.parsed = dotenv.parse(envFile.contents)
let result: dotEnvFlow.DotenvLoadOutput = {}
result.parsed = dotEnvFlow.parse(envFile.path)

result = dotenvExpand(result)

if (result.parsed) {
log.info(`Loaded env from ${path.join(dir || '', envFile.path)}`)
log.info(`Loaded env from ${envFile.path}`)
}

for (const key of Object.keys(result.parsed || {})) {
Expand All @@ -56,7 +55,7 @@ export function processEnv(
}
} catch (err) {
log.error(
`Failed to load env from ${path.join(dir || '', envFile.path)}`,
`Failed to load env from ${envFile.path}`,
err
)
}
Expand All @@ -78,37 +77,37 @@ export function loadEnvConfig(
if (combinedEnv) return { combinedEnv, loadedEnvFiles: cachedLoadedEnvFiles }

const isTest = process.env.NODE_ENV === 'test'
const mode = isTest ? 'test' : dev ? 'development' : 'production'
const dotenvFiles = [
`.env.${mode}.local`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
mode !== 'test' && `.env.local`,
`.env.${mode}`,
'.env',
].filter(Boolean) as string[]

for (const envFile of dotenvFiles) {
const isDev = dev ?? process.env.NODE_ENV === 'development'
const _environment = [
process.env.BUILD_ENV,
process.env.SITE_ENV,
process.env.ENVIRONMENT,
isTest ? 'test' : isDev ? 'development' : 'production'
].find(Boolean) as string

// move to listFiles instead of listDotenvFiles after version 4.0.0
const dotenvFiles = dotEnvFlow.listDotenvFiles(dir, {
node_env: _environment,
})

for (const dotEnvFile of dotenvFiles) {
// only load .env if the user provided has an env config file
const dotEnvPath = path.join(dir, envFile)

try {
const stats = fs.statSync(dotEnvPath)
const stats = fs.statSync(dotEnvFile)

// make sure to only attempt to read files
if (!stats.isFile()) {
continue
}

const contents = fs.readFileSync(dotEnvPath, 'utf8')
const contents = fs.readFileSync(dotEnvFile, 'utf8')
cachedLoadedEnvFiles.push({
path: envFile,
path: dotEnvFile,
contents,
})
} catch (err: any) {
if (err.code !== 'ENOENT') {
log.error(`Failed to load env from ${envFile}`, err)
log.error(`Failed to load env from ${dotEnvFile}`, err)
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
"types"
],
"scripts": {
"tsc": "npx -p typescript tsc",
"dev": "ncc build ./index.ts -w -o dist/",
"prerelease": "rimraf ./dist/",
"types": "tsc index.ts --declaration --emitDeclarationOnly --declarationDir types --esModuleInterop",
"prerelease": "npx rimraf@4.4.1 ./dist/",
"types": "yarn tsc index.ts --declaration --emitDeclarationOnly --declarationDir types --esModuleInterop",
"release": "ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
"prepublish": "yarn release && yarn types"
},
"devDependencies": {
"@types/dotenv-flow": "3.2.0",
"@types/node": "18.19.1",
"@vercel/ncc": "0.33.1",
"dotenv": "10.0.0",
"dotenv-expand": "8.0.1"
"dotenv-flow": "3.2.0",
"dotenv-expand": "8.0.1",
"typescript": "5.4.2"
}
}
47 changes: 47 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/[email protected]":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@types/dotenv-flow/-/dotenv-flow-3.2.0.tgz#3a632bcb72d5a6fa770da644a1f63d69a6c7075e"
integrity sha512-A79hbPwocbYkcTwGcDOFbKDuqyVo5mLAz/6Iq465YZ7R7Go5bT1PIM8I2jlPQkaD9u9fbotGVLkUPhX+9XUHfw==

"@types/[email protected]":
version "18.19.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.1.tgz#e3ed7d5ab5ea21f33a4503decb2171e0d8f53070"
integrity sha512-mZJ9V11gG5Vp0Ox2oERpeFDl+JvCwK24PGy76vVY/UgBtjwJWc5rYBThFxmbnYOm9UPZNm6wEl/sxHt2SU7x9A==
dependencies:
undici-types "~5.26.4"

"@vercel/[email protected]":
version "0.33.1"
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.33.1.tgz#b240080a3c1ded9446a30955a06a79851bb38f71"
integrity sha512-Mlsps/P0PLZwsCFtSol23FGqT3FhBGb4B1AuGQ52JTAtXhak+b0Fh/4T55r0/SVQPeRiX9pNItOEHwakGPmZYA==

[email protected]:
version "8.0.1"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-8.0.1.tgz#332aa17c14b12e28e2e230f8d183eecc1c014fdc"
integrity sha512-j/Ih7bIERDR5PzI89Zu8ayd3tXZ6E3dbY0ljQ9Db0K87qBO8zdLsi2dIvDHMWtjC3Yxb8XixOTHAtia0fDHRpg==

[email protected]:
version "3.2.0"
resolved "https://registry.yarnpkg.com/dotenv-flow/-/dotenv-flow-3.2.0.tgz#a5d79dd60ddb6843d457a4874aaf122cf659a8b7"
integrity sha512-GEB6RrR4AbqDJvNSFrYHqZ33IKKbzkvLYiD5eo4+9aFXr4Y4G+QaFrB/fNp0y6McWBmvaPn3ZNjIufnj8irCtg==
dependencies:
dotenv "^8.0.0"

dotenv@^8.0.0:
version "8.6.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b"
integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==

[email protected]:
version "5.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.2.tgz#0ae9cebcfae970718474fe0da2c090cad6577372"
integrity sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==

undici-types@~5.26.4:
version "5.26.5"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==