Fix port assignment to respect user-defined PORT vars

This commit is contained in:
Chris Wanstrath 2026-03-10 10:08:53 -07:00
parent e97be11a0c
commit 0a1100aaf7

View File

@ -94,16 +94,15 @@ $ true
console.error("--port-from must be an integer") console.error("--port-from must be an integer")
process.exit(1) process.exit(1)
} }
let nextPort = portFrom ?? 0 let nextPort = portFrom
const runOne = async (filePath: string) => { const runOne = async (filePath: string, port: number | undefined) => {
const content = await readFile(filePath, "utf-8") const content = await readFile(filePath, "utf-8")
const parsed = parse(relative(cwd, filePath), content) const parsed = parse(relative(cwd, filePath), content)
// Resolve directives in a single pass. Setup @env is collected separately // Resolve directives in a single pass. Setup @env is collected separately
// so that the user file's @env always takes precedence. // so that the user file's @env always takes precedence.
const envVars: Record<string, string> = {} const envVars: Record<string, string> = {}
if (portFrom !== undefined) envVars["PORT"] = String(nextPort++)
const setupEnvVars: Record<string, string> = {} const setupEnvVars: Record<string, string> = {}
const userEnvVars: Record<string, string> = {} const userEnvVars: Record<string, string> = {}
const setupCommands: Command[] = [] const setupCommands: Command[] = []
@ -124,6 +123,9 @@ $ true
} }
} }
Object.assign(envVars, setupEnvVars, userEnvVars) Object.assign(envVars, setupEnvVars, userEnvVars)
if (port !== undefined && !("PORT" in userEnvVars) && !("PORT" in setupEnvVars)) {
envVars["PORT"] = String(port)
}
const merged: ShoutFile = { ...parsed, commands: [...setupCommands, ...parsed.commands] } const merged: ShoutFile = { ...parsed, commands: [...setupCommands, ...parsed.commands] }
const fileResult = await runFile(merged, { const fileResult = await runFile(merged, {
@ -188,8 +190,7 @@ $ true
} }
if (opts.parallel) { if (opts.parallel) {
const tasks = files.map(f => runOne(f)) const all = await Promise.all(files.map(f => runOne(f, nextPort !== undefined ? nextPort++ : undefined)))
const all = await Promise.all(tasks)
for (const r of all) { for (const r of all) {
printDots(r) printDots(r)
results.push(r) results.push(r)
@ -197,7 +198,7 @@ $ true
process.stdout.write("\n") process.stdout.write("\n")
} else { } else {
for (const filePath of files) { for (const filePath of files) {
const r = await runOne(filePath) const r = await runOne(filePath, nextPort !== undefined ? nextPort++ : undefined)
printDots(r) printDots(r)
results.push(r) results.push(r)
} }