Skip to content
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

Use native .env loading functionality #159

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -32,8 +32,8 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: '**/package.json'
# cache: 'npm'
# cache-dependency-path: '**/package.json'

- name: Install dependencies
run: npm install
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -8,12 +8,12 @@
"url": "https://github.com/supercharge/framework/issues"
},
"devDependencies": {
"@supercharge/eslint-config-typescript": "~4.0.1",
"@supercharge/eslint-config-typescript": "~4.0.2",
"@supercharge/tsconfig": "~7.0.0",
"@types/node": "~20.10.5",
"@types/node": "~20.12.8",
"eslint": "~8.56.0",
"lerna": "~8.0.1",
"nx": "17.1.2"
"lerna": "~8.1.2",
"nx": "18.3.4"
},
"engines": {
"node": ">=22"
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -21,12 +21,11 @@
"@supercharge/console": "^4.0.0-alpha.2",
"@supercharge/contracts": "^4.0.0-alpha.2",
"@supercharge/errors": "~2.0.1",
"@supercharge/fs": "~3.4.0",
"@supercharge/fs": "~3.4.2",
"@supercharge/goodies": "~2.0.0",
"@supercharge/http": "^4.0.0-alpha.2",
"@supercharge/set": "~2.2.1",
"@supercharge/support": "^4.0.0-alpha.2",
"dotenv": "~16.3.1",
"type-fest": "~4.8.3",
"youch": "~3.3.3",
"youch-terminal": "~2.2.3"
@@ -35,7 +34,6 @@
"@supercharge/view": "^4.0.0-alpha.2",
"c8": "~9.1.0",
"expect": "~29.7.0",
"mocked-env": "~1.3.5",
"sinon": "~17.0.1",
"supertest": "~7.0.0",
"typescript": "~5.4.5",
23 changes: 11 additions & 12 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'

@@ -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
})
}

/**
@@ -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)
}
}
}
5 changes: 0 additions & 5 deletions packages/core/test/http-kernel.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import { test } from 'uvu'
import Path from 'node:path'
import { expect } from 'expect'
import Supertest from 'supertest'
import MockedEnv from 'mocked-env'
import { fileURLToPath } from 'node:url'
import { Server } from '@supercharge/http'
import { HttpKernel, Application, ErrorHandler } from '../dist/index.js'
@@ -67,8 +66,6 @@ test('fails to bootstrap the HTTP kernel when missing a .env file', async () =>
})

test('loads specific environment from .env.testing file because of NODE_ENV=testing', async () => {
const restoreEnv = MockedEnv({ restore: true })

const env = app.env().get('NODE_ENV')
app.env().set('NODE_ENV', 'testing')

@@ -78,8 +75,6 @@ test('loads specific environment from .env.testing file because of NODE_ENV=test
expect(app.env().get('OVERWRITE')).toEqual('1')

app.env().set('NODE_ENV', env)

restoreEnv()
})

test('registers and calls booted callbacks', async () => {
3 changes: 1 addition & 2 deletions packages/env/package.json
Original file line number Diff line number Diff line change
@@ -18,8 +18,7 @@
"dependencies": {
"@supercharge/contracts": "^4.0.0-alpha.2",
"@supercharge/fs": "~3.4.0",
"@supercharge/strings": "~2.0.0",
"dotenv": "~16.3.1"
"@supercharge/strings": "~2.0.0"
},
"devDependencies": {
"@types/lodash": "~4.14.202",