Skip to content

Commit

Permalink
fix: Restore CLI port in use prompt feature (#25863)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
polarathene committed Jul 21, 2020
1 parent 0506123 commit fc2f6f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/gatsby/src/commands/develop.ts
Expand Up @@ -5,6 +5,7 @@ import tmp from "tmp"
import { spawn, ChildProcess } from "child_process"
import chokidar from "chokidar"
import getRandomPort from "detect-port"
import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt"
import socket from "socket.io"
import fs from "fs-extra"
import { isCI, slash } from "gatsby-core-utils"
Expand Down Expand Up @@ -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

module.exports = async (program: IProgram): Promise<void> => {
const developProcessPath = slash(require.resolve(`./develop-process`))

try {
program.port = await detectPortInUseAndPrompt(program.port)
} catch (e) {
if (e.message === `USER_REJECTED`) {
process.exit(0)
}

throw e
}

// Run the actual develop server on a random port, and the proxy on the program port
// which users will access
const proxyPort = program.port
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/utils/detect-port-in-use-and-prompt.ts
Expand Up @@ -5,11 +5,11 @@ import prompts from "prompts"
export const detectPortInUseAndPrompt = async (
port: number
): Promise<number> => {
let foundPort = port
const detectedPort = await detectPort(port).catch((err: Error) =>
let foundPort = typeof port === `string` ? parseInt(port, 10) : port
const detectedPort = await detectPort(foundPort).catch((err: Error) =>
report.panic(err)
)
if (port !== detectedPort) {
if (foundPort !== detectedPort) {
report.log(`\nSomething is already running at port ${port}`)
const response = await prompts({
type: `confirm`,
Expand Down

0 comments on commit fc2f6f0

Please sign in to comment.