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

[chore] Re-use loadEnvFiles from cli-helpers lib, elimininating code duplicatation #11885

Merged
merged 5 commits into from
Jan 8, 2025
Merged
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
6 changes: 6 additions & 0 deletions packages/cli-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

export * from './lib/index.js'
export * from './lib/colors.js'
export { loadEnvFiles } from './lib/loadEnvFiles.js'
export {
loadDefaultEnvFiles,
loadNodeEnvDerivedEnvFile,
loadUserSpecifiedEnvFiles,
} from './lib/loadEnvFiles.js'
export * from './lib/paths.js'
export * from './lib/project.js'
export * from './lib/version.js'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
loadDefaultEnvFiles,
loadNodeEnvDerivedEnvFile,
loadUserSpecifiedEnvFiles,
} from '../lib/loadEnvFiles'
} from '../loadEnvFiles.js'

describe('loadEnvFiles', () => {
let originalProcessEnv
let originalProcessEnv: NodeJS.Process['env']
beforeAll(() => {
originalProcessEnv = { ...process.env }
})
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('loadEnvFiles', () => {
loadUserSpecifiedEnvFiles(cwd, ['missing'])
} catch (error) {
// Just testing that the error message reports the file it tried to load.
expect(error.message).toMatch(/\.env\.missing/)
expect((error as Error).message).toMatch(/\.env\.missing/)
}
})
})
3 changes: 1 addition & 2 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from 'fs-extra'
import { hideBin, Parser } from 'yargs/helpers'
import yargs from 'yargs/yargs'

import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'
import { loadEnvFiles, recordTelemetryAttributes } from '@redwoodjs/cli-helpers'
import { telemetryMiddleware } from '@redwoodjs/telemetry'

import * as buildCommand from './commands/build'
Expand All @@ -34,7 +34,6 @@ import * as typeCheckCommand from './commands/type-check'
import * as upgradeCommand from './commands/upgrade'
import { findUp } from './lib'
import { exitWithError } from './lib/exit'
import { loadEnvFiles } from './lib/loadEnvFiles'
import * as updateCheck from './lib/updateCheck'
import { loadPlugins } from './plugin'
import { startTelemetry, shutdownTelemetry } from './telemetry/index'
Expand Down
85 changes: 11 additions & 74 deletions packages/cli/src/lib/loadEnvFiles.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,16 @@
// @ts-check

import path from 'path'

import { config as dotenvConfig } from 'dotenv'
import { config as dotenvDefaultsConfig } from 'dotenv-defaults'
import fs from 'fs-extra'
import { hideBin, Parser } from 'yargs/helpers'

import { getPaths } from '@redwoodjs/project-config'

export function loadEnvFiles() {
if (process.env.REDWOOD_ENV_FILES_LOADED) {
return
}

const { base } = getPaths()

loadDefaultEnvFiles(base)
loadNodeEnvDerivedEnvFile(base)

const { loadEnvFiles } = Parser(hideBin(process.argv), {
array: ['load-env-files'],
default: {
loadEnvFiles: [],
},
})
if (loadEnvFiles.length > 0) {
loadUserSpecifiedEnvFiles(base, loadEnvFiles)
}

process.env.REDWOOD_ENV_FILES_LOADED = 'true'
}

/**
* @param {string} cwd
*/
export function loadDefaultEnvFiles(cwd) {
dotenvDefaultsConfig({
path: path.join(cwd, '.env'),
defaults: path.join(cwd, '.env.defaults'),
multiline: true,
})
}

/**
* @param {string} cwd
*/
export function loadNodeEnvDerivedEnvFile(cwd) {
if (!process.env.NODE_ENV) {
return
}

const nodeEnvDerivedEnvFilePath = path.join(
cwd,
`.env.${process.env.NODE_ENV}`,
)
if (!fs.existsSync(nodeEnvDerivedEnvFilePath)) {
return
}

dotenvConfig({ path: nodeEnvDerivedEnvFilePath, override: true })
}
export {
loadEnvFiles,
loadDefaultEnvFiles,
loadNodeEnvDerivedEnvFile,
loadUserSpecifiedEnvFiles,
} from '@redwoodjs/cli-helpers'

/**
* @param {string} cwd
* Retaining the file here as per https://github.com/redwoodjs/redwood/pull/11885#issuecomment-2575847407
*
* This ensures there are no breaking changes in case people are deep-importing any of these methods.
*
* This file can finally be removed in the next major version as a documented breaking change.
*/
export function loadUserSpecifiedEnvFiles(cwd, loadEnvFiles) {
for (const suffix of loadEnvFiles) {
const envPath = path.join(cwd, `.env.${suffix}`)
if (!fs.pathExistsSync(envPath)) {
throw new Error(
`Couldn't find an .env file at '${envPath}' as specified by '--load-env-files'`,
)
}

dotenvConfig({ path: envPath, override: true })
}
}
Loading