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

fix(dev,preview): correctly infer .env filename from --dotenv arg #381

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
13 changes: 10 additions & 3 deletions src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import type { NuxtDevContext, NuxtDevIPCMessage } from '../utils/dev'

import process from 'node:process'

import { setupDotenv } from 'c12'
import { defineCommand } from 'citty'
import { resolve } from 'pathe'
import { isTest } from 'std-env'

import { isTest } from 'std-env'
import { createNuxtDevServer } from '../utils/dev'
import { overrideEnv } from '../utils/env'
import { logger } from '../utils/logger'
import { cwdArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared'
import { cwdArgs, dotEnvArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -22,6 +23,7 @@ export default defineCommand({
...logLevelArgs,
...envNameArgs,
...legacyRootDirArgs,
...dotEnvArgs,
},
async run(ctx) {
if (!process.send && !isTest) {
Expand All @@ -30,9 +32,14 @@ export default defineCommand({
)
}

const envFileName = typeof ctx.args.dotenv === 'string'
? (ctx.args.dotenv || '.env')
: undefined

// Prepare
overrideEnv('development')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
await setupDotenv({ cwd, fileName: envFileName })

// Get dev context info
const devContext: NuxtDevContext
Expand Down Expand Up @@ -64,7 +71,7 @@ export default defineCommand({
overrides: ctx.data?.overrides,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: !!ctx.args.clear,
dotenv: !!ctx.args.dotenv,
dotenv: envFileName,
envName: ctx.args.envName,
port: process.env._PORT ?? undefined,
devContext,
Expand Down
8 changes: 6 additions & 2 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ const command = defineCommand({
},
},
async run(ctx) {
const envFileName = typeof ctx.args.dotenv === 'string'
? (ctx.args.dotenv || '.env')
: undefined

// Prepare
overrideEnv('development')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
await showVersions(cwd)
await setupDotenv({ cwd, fileName: ctx.args.dotenv })
await setupDotenv({ cwd, fileName: envFileName })

// Load Nuxt Config
const { loadNuxtConfig } = await loadKit(cwd)
Expand Down Expand Up @@ -82,7 +86,7 @@ const command = defineCommand({
overrides: ctx.data?.overrides,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: ctx.args.clear,
dotenv: !!ctx.args.dotenv,
dotenv: envFileName,
envName: ctx.args.envName,
loadingTemplate: nuxtOptions.devServer.loadingTemplate,
devContext: {},
Expand Down
24 changes: 16 additions & 8 deletions src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,22 @@ export default defineCommand({
),
)

const envExists = ctx.args.dotenv
? existsSync(resolve(cwd, ctx.args.dotenv))
: existsSync(cwd)
if (envExists) {
logger.info(
`Loading \`${ctx.args.dotenv || '.env'}\`. This will not be loaded when running the server in production.`,
)
await setupDotenv({ cwd, fileName: ctx.args.dotenv })
if (typeof ctx.args.dotenv === 'string') {
const envFileName = ctx.args.dotenv || '.env'

const envExists = existsSync(resolve(cwd, envFileName))

if (envExists) {
logger.info(
`Loading \`${envFileName}\`. This will not be loaded when running the server in production.`,
)

await setupDotenv({ cwd, fileName: envFileName })
}
else {
logger.error(`Cannot find \`${envFileName}\`.`)
process.exit(1)
Comment on lines +105 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just warn in this case?

}
}

logger.info(`Starting preview command: \`${nitroJSON.commands.preview}\``)
Expand Down
10 changes: 8 additions & 2 deletions src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface NuxtDevContext {
interface NuxtDevServerOptions {
cwd: string
logLevel: 'silent' | 'info' | 'verbose'
dotenv: boolean
dotenv: string | undefined
envName: string
clear: boolean
overrides: NuxtConfig
Expand Down Expand Up @@ -177,6 +177,12 @@ class NuxtDevServer extends EventEmitter {
const kit = await loadKit(this.options.cwd)
this._currentNuxt = await kit.loadNuxt({
cwd: this.options.cwd,
dotenv: this.options.dotenv
? {
cwd: this.options.cwd,
fileName: this.options.dotenv,
}
: undefined,
dev: true,
ready: false,
envName: this.options.envName,
Expand Down Expand Up @@ -315,7 +321,7 @@ class NuxtDevServer extends EventEmitter {
return
}
const file = relative(this.options.cwd, _file)
if (file === (this.options.dotenv || '.env')) {
if (this.options.dotenv && file === this.options.dotenv) {
this.emit('restart')
}
if (RESTART_RE.test(file)) {
Expand Down
Loading