Skip to content

Commit fc2f6f0

Browse files
authored
fix: Restore CLI port in use prompt feature (#25863)
* fix: Restore CLI port in use prompt feature Seems like it got accidentally removed during a big PR? * fix: Ensure port var types are the same The CLI option and default value for `port` is a string, despite the TypeScript typing the arg to `number`. Unclear if `port` should be a `number` elsewhere, so checking for and converting to a number within the utility.
1 parent 0506123 commit fc2f6f0

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/gatsby/src/commands/develop.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import tmp from "tmp"
55
import { spawn, ChildProcess } from "child_process"
66
import chokidar from "chokidar"
77
import getRandomPort from "detect-port"
8+
import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt"
89
import socket from "socket.io"
910
import fs from "fs-extra"
1011
import { isCI, slash } from "gatsby-core-utils"
@@ -163,6 +164,17 @@ const REGEX_IP = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25
163164

164165
module.exports = async (program: IProgram): Promise<void> => {
165166
const developProcessPath = slash(require.resolve(`./develop-process`))
167+
168+
try {
169+
program.port = await detectPortInUseAndPrompt(program.port)
170+
} catch (e) {
171+
if (e.message === `USER_REJECTED`) {
172+
process.exit(0)
173+
}
174+
175+
throw e
176+
}
177+
166178
// Run the actual develop server on a random port, and the proxy on the program port
167179
// which users will access
168180
const proxyPort = program.port

packages/gatsby/src/utils/detect-port-in-use-and-prompt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import prompts from "prompts"
55
export const detectPortInUseAndPrompt = async (
66
port: number
77
): Promise<number> => {
8-
let foundPort = port
9-
const detectedPort = await detectPort(port).catch((err: Error) =>
8+
let foundPort = typeof port === `string` ? parseInt(port, 10) : port
9+
const detectedPort = await detectPort(foundPort).catch((err: Error) =>
1010
report.panic(err)
1111
)
12-
if (port !== detectedPort) {
12+
if (foundPort !== detectedPort) {
1313
report.log(`\nSomething is already running at port ${port}`)
1414
const response = await prompts({
1515
type: `confirm`,

0 commit comments

Comments
 (0)