Skip to content
Draft
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
24 changes: 0 additions & 24 deletions package-lock.json

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

1 change: 0 additions & 1 deletion src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ const getEnvSourceName = (source) => {
// @ts-expect-error TS(7031) FIXME: Binding element 'devConfig' implicitly has an 'any... Remove this comment to see the full error message
export const getDotEnvVariables = async ({ devConfig, env, site }): Promise<EnvironmentVariables> => {
const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root })
// @ts-expect-error TS(2339) FIXME: Property 'env' does not exist on type '{ warning: ... Remove this comment to see the full error message
dotEnvFiles.forEach(({ env: fileEnv, file }) => {
const newSourceName = `${file} file`

Expand Down
50 changes: 38 additions & 12 deletions src/utils/dot-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,53 @@ import { isFileAsync } from '../lib/fs.js'

import { warn } from './command-helpers.js'

// @ts-expect-error TS(7031) FIXME: Binding element 'envFiles' implicitly has an 'any'... Remove this comment to see the full error message
export const loadDotEnvFiles = async function ({ envFiles, projectDir }) {
interface DotEnvFile {
file: string
env: Record<string, string>
}

interface DotEnvWarning {
warning: string
}

type LoadedDotEnvFile = DotEnvFile | DotEnvWarning | undefined

const isDefined = <T>(value: T): value is NonNullable<T> => value !== undefined && value !== null
const isWarning = (result: LoadedDotEnvFile): result is DotEnvWarning => isDefined(result) && 'warning' in result
const isDotEnvFile = (result: LoadedDotEnvFile): result is DotEnvFile =>
isDefined(result) && 'file' in result && 'env' in result

interface LoadDotEnvFilesOptions {
envFiles: string[]
projectDir: string
}

export const loadDotEnvFiles = async function ({
envFiles,
projectDir,
}: LoadDotEnvFilesOptions): Promise<DotEnvFile[]> {
const response = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envFiles })

// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
const filesWithWarning = response.filter((el) => el.warning)
const filesWithWarning = response.filter(isWarning)
filesWithWarning.forEach((el) => {
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
warn(el.warning)
})

// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
return response.filter((el) => el.file && el.env)
return response.filter(isDotEnvFile)
}

// in the user configuration, the order is highest to lowest
const defaultEnvFiles = ['.env.development.local', '.env.local', '.env.development', '.env']

// @ts-expect-error TS(7031) FIXME: Binding element 'projectDir' implicitly has an 'an... Remove this comment to see the full error message
export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projectDir }) => {
interface TryLoadDotEnvFilesOptions {
projectDir: string
dotenvFiles?: string[]
}

export const tryLoadDotEnvFiles = async ({
dotenvFiles = defaultEnvFiles,
projectDir,
}: TryLoadDotEnvFilesOptions): Promise<LoadedDotEnvFile[]> => {
const results = await Promise.all(
dotenvFiles.map(async (file) => {
const filepath = path.resolve(projectDir, file)
Expand All @@ -37,8 +64,7 @@ export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projec
}
} catch (error) {
return {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
warning: `Failed reading env variables from file: ${filepath}: ${error.message}`,
warning: `Failed reading env variables from file: ${filepath}: ${error instanceof Error ? error.message : error}`,
}
}
const content = await readFile(filepath, 'utf-8')
Expand All @@ -48,5 +74,5 @@ export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projec
)

// we return in order of lowest to highest priority
return results.filter(Boolean).reverse()
return results.filter(isDefined).reverse()
}
Loading