Skip to content

Commit

Permalink
use native node tools to load environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed May 7, 2024
1 parent 74d6f8e commit 307a942
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions packages/core/src/bootstrappers/load-environment-variables.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import Path from 'node:path'
import Fs from '@supercharge/fs'
import { parseEnv } from 'node:util'
import { Str } from '@supercharge/strings'
import Dotenv, { DotenvConfigOptions } from 'dotenv'
import { Application, Bootstrapper } from '@supercharge/contracts'
import { EnvironmentFileError } from '../errors/environment-file-error.js'

Expand Down Expand Up @@ -31,8 +31,8 @@ export class LoadEnvironmentVariables implements Bootstrapper {
* in case the environment file does not exist, an error will be thrown.
*/
async loadEnvironment (): Promise<void> {
await this.loadDefaultEnvironmentFile()
await this.loadSpecificEnvironmentFile()
await this.loadDefaultEnvironmentFile()
}

/**
Expand All @@ -42,23 +42,22 @@ export class LoadEnvironmentVariables implements Bootstrapper {
const envPath = this.app.environmentFilePath()

if (await Fs.notExists(envPath)) {
throw new Error(`Invalid environment file. Cannot find env file "${envPath}".`)
throw new EnvironmentFileError(`Invalid environment file. Cannot find env file "${envPath}".`)
}

await this.loadEnvironmentFile(
this.app.environmentFilePath(), { override: false }
)
await this.loadEnvironmentFile(envPath)
}

/**
* Load the given environment `file` content into `process.env`.
* Load the environment file from the given `path` into `process.env`.
*/
async loadEnvironmentFile (path: string, options: DotenvConfigOptions): Promise<void> {
const { error } = Dotenv.config({ path, ...options })
async loadEnvironmentFile (envFilePath: string): Promise<void> {
const content = await Fs.content(envFilePath)
const environmentVariables = parseEnv(content)

if (error) {
throw new EnvironmentFileError(`Failed to load environment file "${path}"`, { cause: error })
}
Object.entries(environmentVariables).forEach(([key, value]) => {
process.env[key] = value
})
}

/**
Expand All @@ -77,7 +76,7 @@ export class LoadEnvironmentVariables implements Bootstrapper {
: this.app.resolveFromBasePath(this.app.environmentPath(), `.env.${env}`)

if (await Fs.exists(envFilePath)) {
await this.loadEnvironmentFile(envFilePath, { override: true })
await this.loadEnvironmentFile(envFilePath)
}
}
}

0 comments on commit 307a942

Please sign in to comment.