Skip to content

Commit

Permalink
Fix/follow up 25863 (#25915)
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.

* Force program.port to a number early

Co-authored-by: polarathene <5098581+polarathene@users.noreply.github.com>
  • Loading branch information
blainekasten and polarathene committed Jul 21, 2020
1 parent fc2f6f0 commit 99a573c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/gatsby/src/commands/develop.ts
Expand Up @@ -163,6 +163,9 @@ let isRestarting
const REGEX_IP = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/

module.exports = async (program: IProgram): Promise<void> => {
// In some cases, port can actually be a string. But our codebase is expecting it to be a number.
// So we want to early just force it to a number to ensure we always act on a correct type.
program.port = parseInt(program.port + ``, 10)
const developProcessPath = slash(require.resolve(`./develop-process`))

try {
Expand Down
9 changes: 4 additions & 5 deletions packages/gatsby/src/utils/detect-port-in-use-and-prompt.ts
Expand Up @@ -5,11 +5,10 @@ import prompts from "prompts"
export const detectPortInUseAndPrompt = async (
port: number
): Promise<number> => {
let foundPort = typeof port === `string` ? parseInt(port, 10) : port
const detectedPort = await detectPort(foundPort).catch((err: Error) =>
const detectedPort = await detectPort(port).catch((err: Error) =>
report.panic(err)
)
if (foundPort !== detectedPort) {
if (port !== detectedPort) {
report.log(`\nSomething is already running at port ${port}`)
const response = await prompts({
type: `confirm`,
Expand All @@ -18,11 +17,11 @@ export const detectPortInUseAndPrompt = async (
initial: true,
})
if (response.newPort) {
foundPort = detectedPort
port = detectedPort
} else {
throw new Error(`USER_REJECTED`)
}
}

return foundPort
return port
}

0 comments on commit 99a573c

Please sign in to comment.