Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gatsby): gatsby develop: cleanup port detection #13006

Merged
Merged
Show file tree
Hide file tree
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
[gatsby develop]: promisify port detection
  • Loading branch information
Moocar committed Apr 2, 2019
commit 08dd8688b7db042ca85d3d8ceedbf047f4eb46ac
13 changes: 3 additions & 10 deletions packages/gatsby/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,9 @@ module.exports = async (program: any) => {
})
}

let compiler
await new Promise(resolve => {
detectPortInUseAndPrompt(port, rlInterface, newPort => {
program.port = newPort
startServer(program).then(([c, l]) => {
compiler = c
resolve()
})
})
})
program.port = await detectPortInUseAndPrompt(port, rlInterface)

const [compiler] = await startServer(program)

function prepareUrls(protocol, host, port) {
const formatUrl = hostname =>
Expand Down
6 changes: 2 additions & 4 deletions packages/gatsby/src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ module.exports = async program => {
})
}

detectPortInUseAndPrompt(port, rlInterface, newPort => {
port = newPort
startListening()
})
port = await detectPortInUseAndPrompt(port, rlInterface)
startListening()
}
41 changes: 18 additions & 23 deletions packages/gatsby/src/utils/detect-port-in-use-and-prompt.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
const detect = require(`detect-port`)
const { promisify } = require(`util`)
const detectPort = promisify(require(`detect-port`))
const report = require(`gatsby-cli/lib/reporter`)

// Checks if a port is in use and prompts the user to enter another one
// Then calls callback with new port
const detectPortInUseAndPrompt = (port, rlInterface, callback) => {
let newPort = port

detect(port, (err, _port) => {
if (err) {
report.panic(err)
}

if (port !== _port) {
// eslint-disable-next-line max-len
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
const readlinePort = (port, rlInterface) => {
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
return new Promise(resolve => {
rlInterface.question(question, answer => {
resolve(answer.length === 0 || answer.match(/^yes|y$/i))
})
})
}

rlInterface.question(question, answer => {
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
newPort = _port
}
callback(newPort)
})
} else {
callback(newPort)
const detectPortInUseAndPrompt = async (port, rlInterface) => {
let foundPort = port
const detectedPort = await detectPort(port).catch(err => report.panic(err))
if (port !== detectedPort) {
if (await readlinePort(port, rlInterface)) {
foundPort = detectedPort
}
})
}
return foundPort
}

module.exports = detectPortInUseAndPrompt