From 03dc859a5aa513cc450e15e5c9a732cba0bb8c47 Mon Sep 17 00:00:00 2001 From: Saeid Zareie Date: Mon, 3 Feb 2025 13:09:11 +0330 Subject: [PATCH 1/2] feat: adding port option to preview command --- packages/nuxi/src/commands/preview.ts | 40 +++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index e25f48b8f..791ae3eef 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -1,3 +1,4 @@ +import type { ParsedArgs } from 'citty' import { existsSync, promises as fsp } from 'node:fs' import { dirname, relative } from 'node:path' import process from 'node:process' @@ -5,6 +6,7 @@ import process from 'node:process' import { setupDotenv } from 'c12' import { defineCommand } from 'citty' import { box, colors } from 'consola/utils' +import { getArgs as getListhenArgs } from 'listhen/cli' import { resolve } from 'pathe' import { x } from 'tinyexec' @@ -12,7 +14,10 @@ import { loadKit } from '../utils/kit' import { logger } from '../utils/logger' import { cwdArgs, dotEnvArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared' -export default defineCommand({ +const listenArgs = getListhenArgs() +const portArg = listenArgs.port + +const command = defineCommand({ meta: { name: 'preview', description: 'Launches Nitro server for local testing after `nuxi build`.', @@ -22,6 +27,7 @@ export default defineCommand({ ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, + port: portArg, ...dotEnvArgs, }, async run(ctx) { @@ -92,6 +98,7 @@ 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.`, @@ -99,12 +106,41 @@ export default defineCommand({ await setupDotenv({ cwd, fileName: ctx.args.dotenv }) } + const { port } = _resolveListenOptions(ctx.args) + logger.info(`Starting preview command: \`${nitroJSON.commands.preview}\``) const [command, ...commandArgs] = nitroJSON.commands.preview.split(' ') logger.log('') await x(command, commandArgs, { throwOnError: true, - nodeOptions: { stdio: 'inherit', cwd: outputPath }, + nodeOptions: { + stdio: 'inherit', + cwd: outputPath, + env: { + ...process.env, + NUXT_PORT: port, + NITRO_PORT: port, + }, + }, }) }, }) + +export default command + +type ArgsT = Exclude< + Awaited, + undefined | ((...args: unknown[]) => unknown) +> + +function _resolveListenOptions(args: ParsedArgs) { + const _port = args.port + ?? args.p + ?? process.env.NUXT_PORT + ?? process.env.NITRO_PORT + ?? process.env.PORT + + return { + port: _port, + } +} From bca82cb69330fa19f4e9f982bdc8e5f2f9de5489 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 13 Feb 2025 20:33:25 +0100 Subject: [PATCH 2/2] refactor: simplify port arg resolution --- packages/nuxi/src/commands/preview.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index 791ae3eef..1e2d06625 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -14,9 +14,6 @@ import { loadKit } from '../utils/kit' import { logger } from '../utils/logger' import { cwdArgs, dotEnvArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared' -const listenArgs = getListhenArgs() -const portArg = listenArgs.port - const command = defineCommand({ meta: { name: 'preview', @@ -27,7 +24,7 @@ const command = defineCommand({ ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, - port: portArg, + port: getListhenArgs().port, ...dotEnvArgs, }, async run(ctx) {