Skip to content

Commit

Permalink
feat: add --env-name flag for configuration overrides (#357) (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
tillsanders authored Oct 15, 2024
1 parent b8873ed commit d24f287
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/commands/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export const sharedArgs = {
},
} as const

export const envNameArgs = {
envName: {
type: 'string',
description: 'The environment to use when resolving configuration overrides (default is `production` when building, and `development` when running the dev server)',
},
} as const

export const legacyRootDirArgs = {
rootDir: {
type: 'positional',
Expand Down
4 changes: 3 additions & 1 deletion src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { loadKit } from '../utils/kit'
import { clearBuildDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
import { showVersions } from '../utils/banner'
import { sharedArgs, legacyRootDirArgs } from './_shared'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -27,6 +27,7 @@ export default defineCommand({
type: 'string',
description: 'Path to .env file',
},
...envNameArgs,
...legacyRootDirArgs,
},
async run(ctx) {
Expand All @@ -53,6 +54,7 @@ export default defineCommand({
cwd,
fileName: ctx.args.dotenv,
},
envName: ctx.args.envName, // c12 will fall back to NODE_ENV
overrides: {
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
// TODO: remove in 3.8
Expand Down
4 changes: 3 additions & 1 deletion src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isTest } from 'std-env'
import { overrideEnv } from '../utils/env'
import type { NuxtDevContext, NuxtDevIPCMessage } from '../utils/dev'
import { createNuxtDevServer } from '../utils/dev'
import { sharedArgs, legacyRootDirArgs } from './_shared'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -15,6 +15,7 @@ export default defineCommand({
},
args: {
...sharedArgs,
...envNameArgs,
...legacyRootDirArgs,
},
async run(ctx) {
Expand All @@ -41,6 +42,7 @@ export default defineCommand({
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: !!ctx.args.clear,
dotenv: !!ctx.args.dotenv,
envName: ctx.args.envName,
port: process.env._PORT ?? undefined,
devContext,
})
Expand Down
5 changes: 4 additions & 1 deletion src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { loadKit } from '../utils/kit'
import { importModule } from '../utils/esm'
import { overrideEnv } from '../utils/env'
import type { NuxtDevContext, NuxtDevIPCMessage } from '../utils/dev'
import { sharedArgs, legacyRootDirArgs } from './_shared'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'

const forkSupported = !isBun && !isTest

Expand All @@ -28,6 +28,7 @@ const command = defineCommand({
},
args: {
...sharedArgs,
...envNameArgs,
...legacyRootDirArgs,
...getListhenArgs(),
dotenv: {
Expand Down Expand Up @@ -55,6 +56,7 @@ const command = defineCommand({
const { loadNuxtConfig } = await loadKit(cwd)
const nuxtOptions = await loadNuxtConfig({
cwd,
envName: ctx.args.envName, // c12 will fall back to NODE_ENV
overrides: {
dev: true,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
Expand All @@ -81,6 +83,7 @@ const command = defineCommand({
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: ctx.args.clear,
dotenv: !!ctx.args.dotenv,
envName: ctx.args.envName,
loadingTemplate: nuxtOptions.devServer.loadingTemplate,
devContext: {},
},
Expand Down
3 changes: 2 additions & 1 deletion src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineCommand } from 'citty'
import buildCommand from './build'

import { legacyRootDirArgs, sharedArgs } from './_shared'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -10,6 +10,7 @@ export default defineCommand({
},
args: {
...sharedArgs,
...envNameArgs,
...legacyRootDirArgs,
dotenv: {
type: 'string',
Expand Down
4 changes: 3 additions & 1 deletion src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { defineCommand } from 'citty'
import { clearBuildDir } from '../utils/fs'
import { loadKit } from '../utils/kit'

import { legacyRootDirArgs, sharedArgs } from './_shared'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -20,6 +20,7 @@ export default defineCommand({
description: 'Path to .env file',
},
...sharedArgs,
...envNameArgs,
...legacyRootDirArgs,
},
async run(ctx) {
Expand All @@ -38,6 +39,7 @@ export default defineCommand({
cwd,
fileName: ctx.args.dotenv,
},
envName: ctx.args.envName, // c12 will fall back to NODE_ENV
overrides: {
_prepare: true,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
Expand Down
4 changes: 3 additions & 1 deletion src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { box, colors } from 'consola/utils'
import { defineCommand } from 'citty'
import { loadKit } from '../utils/kit'

import { legacyRootDirArgs, sharedArgs } from './_shared'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -17,6 +17,7 @@ export default defineCommand({
},
args: {
...sharedArgs,
...envNameArgs,
...legacyRootDirArgs,
dotenv: {
type: 'string',
Expand All @@ -31,6 +32,7 @@ export default defineCommand({
const { loadNuxtConfig } = await loadKit(cwd)
const config = await loadNuxtConfig({
cwd,
envName: ctx.args.envName, // c12 will fall back to NODE_ENV
overrides: /* ctx.options?.overrides || */ {},
})

Expand Down
2 changes: 2 additions & 0 deletions src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface NuxtDevServerOptions {
cwd: string
logLevel: 'silent' | 'info' | 'verbose'
dotenv: boolean
envName: string
clear: boolean
overrides: NuxtConfig
port?: string | number
Expand Down Expand Up @@ -173,6 +174,7 @@ class NuxtDevServer extends EventEmitter {
cwd: this.options.cwd,
dev: true,
ready: false,
envName: this.options.envName,
overrides: {
logLevel: this.options.logLevel as 'silent' | 'info' | 'verbose',
vite: {
Expand Down

0 comments on commit d24f287

Please sign in to comment.