Skip to content
Merged
Changes from all 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
37 changes: 35 additions & 2 deletions packages/nuxi/src/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { ParsedArgs } from 'citty'
import { existsSync, promises as fsp } from 'node:fs'
import { dirname, relative } from 'node:path'
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'

import { loadKit } from '../utils/kit'
import { logger } from '../utils/logger'
import { cwdArgs, dotEnvArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared'

export default defineCommand({
const command = defineCommand({
meta: {
name: 'preview',
description: 'Launches Nitro server for local testing after `nuxi build`.',
Expand All @@ -22,6 +24,7 @@ export default defineCommand({
...logLevelArgs,
...envNameArgs,
...legacyRootDirArgs,
port: getListhenArgs().port,
...dotEnvArgs,
},
async run(ctx) {
Expand Down Expand Up @@ -92,19 +95,49 @@ 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 })
}

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<typeof command.args>,
undefined | ((...args: unknown[]) => unknown)
>

function _resolveListenOptions(args: ParsedArgs<ArgsT>) {
const _port = args.port
?? args.p
?? process.env.NUXT_PORT
?? process.env.NITRO_PORT
?? process.env.PORT

return {
port: _port,
}
}
Loading