Skip to content
Open
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
47 changes: 34 additions & 13 deletions packages/nuxi/src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { box, outro } from '@clack/prompts'
import { setupDotenv } from 'c12'
import { defineCommand } from 'citty'
import { colors } from 'consola/utils'
import { resolve } from 'pathe'
import { join, resolve } from 'pathe'
import { x } from 'tinyexec'

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

const command = defineCommand({
meta: {
Expand Down Expand Up @@ -53,12 +60,20 @@ const command = defineCommand({
modules: [
function (_, nuxt) {
nuxt.hook('nitro:init', (nitro) => {
res(resolve(nuxt.options.srcDir || cwd, nitro.options.output.dir || '.output', 'nitro.json'))
res(
resolve(
nuxt.options.srcDir || cwd,
nitro.options.output.dir || '.output',
'nitro.json',
),
)
})
},
],
},
}).then(nuxt => nuxt.close()).catch(() => '')
})
.then(nuxt => nuxt.close())
.catch(() => '')
})

const defaultOutput = resolve(cwd, '.output', 'nitro.json') // for backwards compatibility
Expand All @@ -82,7 +97,7 @@ const command = defineCommand({
const info = [
['Node.js:', `v${process.versions.node}`],
['Nitro preset:', nitroJSON.preset],
['Working directory:', relativeToProcess(outputPath)],
['Working directory:', relativeToProcess(cwd)],
] as const
const _infoKeyLen = Math.max(...info.map(([label]) => label.length))

Expand Down Expand Up @@ -126,19 +141,25 @@ const command = defineCommand({
logger.error(`Cannot find ${colors.cyan(envFileName)}.`)
}

const port = ctx.args.port
?? process.env.NUXT_PORT
?? process.env.NITRO_PORT
?? process.env.PORT
const port
= ctx.args.port
?? process.env.NUXT_PORT
?? process.env.NITRO_PORT
?? process.env.PORT

outro(`Running ${colors.cyan(nitroJSON.commands.preview)} in ${colors.cyan(relativeToProcess(outputPath))}`)
outro(
`Running ${colors.cyan(nitroJSON.commands.preview)} in ${colors.cyan(relativeToProcess(cwd))}`,
)

const [command, ...commandArgs] = nitroJSON.commands.preview.split(' ')
await x(command, commandArgs, {
const [cmd, ...cmdArgs] = nitroJSON.commands.preview.split(' ')
const resolvedCmdArgs = cmdArgs.map((arg: string) =>
existsSync(join(outputPath, arg)) ? join(outputPath, arg) : arg,
)
Comment on lines +155 to +157
Copy link
Member

Choose a reason for hiding this comment

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

this feels hacky - we're rewriting args.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review. You're right that process.cwd() isn't the right approach for Nitro endpoints — and the arg rewriting is indeed hacky.
My use case was resolving paths for file uploads on disk (e.g. a local upload directory relative to the project). I opened nuxt/nuxt#34397 because the inconsistency was confusing, but I take your point that process.cwd() isn't something Nitro should be designed around.
Would you be open to at least fixing the displayed suggested command — changing node server/index.mjs to node .output/server/index.mjs in the info box? That part is purely a docs/UX inconsistency regardless of the cwd question, and it contradicts the official preview docs.

await x(cmd, resolvedCmdArgs, {
throwOnError: true,
nodeOptions: {
stdio: 'inherit',
cwd: outputPath,
cwd,
env: {
...process.env,
NUXT_PORT: port,
Expand Down
Loading