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

feat: add env-flag for env-specific config (#357) #424

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default defineCommand({
type: 'string',
description: 'Path to .env file',
},
env: {
Copy link
Member

Choose a reason for hiding this comment

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

For more clarity i would suggest to name it envName / --env-name.

--env might also be usable for other purposes in the future.

Copy link
Member

Choose a reason for hiding this comment

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

PS: i think we can make a shared object similar to how legacyRootDirArgs now i guess since this is arg is shared for all commands.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, I appreciate your feedback! I made the necessary changes as you suggested. I noticed that there is a slight inconsistency in the way the flags are named. Some use kebab-case, others lowerCamelCase – e.g. --logLevel and --no-fork. I decided to go with lowerCamelCase as that seems to be the majority and matches the naming in the loadNuxt() function.

I also made a small amendment to the documentation, here: nuxt/nuxt#28909

Please let me know if any other changes are required – looking forward to have this available!

type: 'string',
description: 'Name of the build environment to use (see \'Environment overrides\' in the docs)',
},
...legacyRootDirArgs,
},
async run(ctx) {
Expand All @@ -50,6 +54,7 @@ export default defineCommand({
cwd,
fileName: ctx.args.dotenv,
},
envName: ctx.args.env, // c12 will fall back to NODE_ENV
overrides: {
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
// TODO: remove in 3.8
Expand Down
5 changes: 5 additions & 0 deletions src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default defineCommand({
args: {
...sharedArgs,
...legacyRootDirArgs,
env: {
type: 'string',
description: 'Name of the build environment to use (see \'Environment overrides\' in the docs)',
},
},
async run(ctx) {
const logger = consola.withTag('nuxi')
Expand All @@ -41,6 +45,7 @@ export default defineCommand({
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: !!ctx.args.clear,
dotenv: !!ctx.args.dotenv,
env: ctx.args.env,
port: process.env._PORT ?? undefined,
devContext,
})
Expand Down
6 changes: 6 additions & 0 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const command = defineCommand({
type: 'string',
description: 'Path to .env file',
},
env: {
type: 'string',
description: 'Name of the build environment to use (see \'Environment overrides\' in the docs)',
},
clear: {
type: 'boolean',
description: 'Clear console on restart',
Expand All @@ -55,6 +59,7 @@ const command = defineCommand({
const { loadNuxtConfig } = await loadKit(cwd)
const nuxtOptions = await loadNuxtConfig({
cwd,
envName: ctx.args.env, // c12 will fall back to NODE_ENV
overrides: {
dev: true,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
Expand All @@ -81,6 +86,7 @@ const command = defineCommand({
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: ctx.args.clear,
dotenv: !!ctx.args.dotenv,
env: ctx.args.env,
loadingTemplate: nuxtOptions.devServer.loadingTemplate,
devContext: {},
},
Expand Down
4 changes: 4 additions & 0 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default defineCommand({
type: 'string',
description: 'Path to .env file',
},
env: {
type: 'string',
description: 'Name of the build environment to use (see \'Environment overrides\' in the docs)',
},
},
async run(ctx) {
ctx.args.prerender = true
Expand Down
5 changes: 5 additions & 0 deletions src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default defineCommand({
type: 'string',
description: 'Path to .env file',
},
env: {
type: 'string',
description: 'Name of the build environment to use (see \'Environment overrides\' in the docs)',
},
...sharedArgs,
...legacyRootDirArgs,
},
Expand All @@ -38,6 +42,7 @@ export default defineCommand({
cwd,
fileName: ctx.args.dotenv,
},
envName: ctx.args.env, // c12 will fall back to NODE_ENV
overrides: {
_prepare: true,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
Expand Down
5 changes: 5 additions & 0 deletions src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default defineCommand({
type: 'string',
description: 'Path to .env file',
},
env: {
type: 'string',
description: 'Name of the build environment to use (see \'Environment overrides\' in the docs)',
},
},
async run(ctx) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
Expand All @@ -31,6 +35,7 @@ export default defineCommand({
const { loadNuxtConfig } = await loadKit(cwd)
const config = await loadNuxtConfig({
cwd,
envName: ctx.args.env, // 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 @@ -32,6 +32,7 @@ export interface NuxtDevServerOptions {
cwd: string
logLevel: 'silent' | 'info' | 'verbose'
dotenv: boolean
env: string
clear: boolean
overrides: NuxtConfig
port?: string | number
Expand Down Expand Up @@ -172,6 +173,7 @@ class NuxtDevServer extends EventEmitter {
cwd: this.options.cwd,
dev: true,
ready: false,
envName: this.options.env,
overrides: {
logLevel: this.options.logLevel as 'silent' | 'info' | 'verbose',
vite: {
Expand Down