Skip to content

Commit

Permalink
[chore] Re-use loadEnvFiles from cli-helpers lib, elimininating code …
Browse files Browse the repository at this point in the history
…duplicatation (#11885)
  • Loading branch information
Philzen authored Jan 8, 2025
1 parent 5e1ba7e commit f66ca2e
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 79 deletions.
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 })
}
}

0 comments on commit f66ca2e

Please sign in to comment.