Skip to content

Commit 3696b7c

Browse files
Moocarsidharthachatterjee
authored andcommitted
refactor(gatsby): gatsby develop: cleanup port detection (#13006)
1 parent b278a60 commit 3696b7c

3 files changed

Lines changed: 23 additions & 37 deletions

File tree

packages/gatsby/src/commands/develop.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,9 @@ module.exports = async (program: any) => {
288288
})
289289
}
290290

291-
let compiler
292-
await new Promise(resolve => {
293-
detectPortInUseAndPrompt(port, rlInterface, newPort => {
294-
program.port = newPort
295-
startServer(program).then(([c, l]) => {
296-
compiler = c
297-
resolve()
298-
})
299-
})
300-
})
291+
program.port = await detectPortInUseAndPrompt(port, rlInterface)
292+
293+
const [compiler] = await startServer(program)
301294

302295
function prepareUrls(protocol, host, port) {
303296
const formatUrl = hostname =>

packages/gatsby/src/commands/serve.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ module.exports = async program => {
111111
})
112112
}
113113

114-
detectPortInUseAndPrompt(port, rlInterface, newPort => {
115-
port = newPort
116-
startListening()
117-
})
114+
port = await detectPortInUseAndPrompt(port, rlInterface)
115+
startListening()
118116
}
Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
const detect = require(`detect-port`)
1+
const { promisify } = require(`util`)
2+
const detectPort = promisify(require(`detect-port`))
23
const report = require(`gatsby-cli/lib/reporter`)
34

4-
// Checks if a port is in use and prompts the user to enter another one
5-
// Then calls callback with new port
6-
const detectPortInUseAndPrompt = (port, rlInterface, callback) => {
7-
let newPort = port
8-
9-
detect(port, (err, _port) => {
10-
if (err) {
11-
report.panic(err)
12-
}
13-
14-
if (port !== _port) {
15-
// eslint-disable-next-line max-len
16-
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
5+
const readlinePort = (port, rlInterface) => {
6+
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
7+
return new Promise(resolve => {
8+
rlInterface.question(question, answer => {
9+
resolve(answer.length === 0 || answer.match(/^yes|y$/i))
10+
})
11+
})
12+
}
1713

18-
rlInterface.question(question, answer => {
19-
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
20-
newPort = _port
21-
}
22-
callback(newPort)
23-
})
24-
} else {
25-
callback(newPort)
14+
const detectPortInUseAndPrompt = async (port, rlInterface) => {
15+
let foundPort = port
16+
const detectedPort = await detectPort(port).catch(err => report.panic(err))
17+
if (port !== detectedPort) {
18+
if (await readlinePort(port, rlInterface)) {
19+
foundPort = detectedPort
2620
}
27-
})
21+
}
22+
return foundPort
2823
}
2924

3025
module.exports = detectPortInUseAndPrompt

0 commit comments

Comments
 (0)