Skip to content

Commit 6f23c95

Browse files
committed
[gatsby develop]: cleanup port detection
1 parent 8a2b8ed commit 6f23c95

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

packages/gatsby/src/commands/develop.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { formatError } = require(`graphql`)
1313
const request = require(`request`)
1414
const rl = require(`readline`)
1515
const webpack = require(`webpack`)
16+
const { promisify } = require(`util`)
1617
const webpackConfig = require(`../utils/webpack.config`)
1718
const bootstrap = require(`../bootstrap`)
1819
const { store } = require(`../redux`)
@@ -32,6 +33,7 @@ const slash = require(`slash`)
3233
const { initTracer } = require(`../utils/tracer`)
3334
const apiRunnerNode = require(`../utils/api-runner-node`)
3435
const telemetry = require(`gatsby-telemetry`)
36+
const detectPort = promisify(require(`detect-port`))
3537

3638
// const isInteractive = process.stdout.isTTY
3739

@@ -260,7 +262,6 @@ module.exports = async (program: any) => {
260262
telemetry.trackCli(`DEVELOP_START`)
261263
telemetry.startBackgroundUpdate()
262264

263-
const detect = require(`detect-port`)
264265
const port =
265266
typeof program.port === `string` ? parseInt(program.port, 10) : program.port
266267

@@ -284,35 +285,27 @@ module.exports = async (program: any) => {
284285
})
285286
}
286287

287-
let compiler
288-
await new Promise(resolve => {
289-
detect(port, (err, _port) => {
290-
if (err) {
291-
report.panic(err)
292-
}
293-
294-
if (port !== _port) {
295-
// eslint-disable-next-line max-len
296-
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
297-
298-
rlInterface.question(question, answer => {
299-
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
300-
program.port = _port // eslint-disable-line no-param-reassign
301-
}
288+
const readlinePort = port => {
289+
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
290+
return new Promise(resolve => {
291+
rlInterface.question(question, answer => {
292+
resolve(answer.length === 0 || answer.match(/^yes|y$/i))
293+
})
294+
})
295+
}
302296

303-
startServer(program).then(([c, l]) => {
304-
compiler = c
305-
resolve()
306-
})
307-
})
308-
} else {
309-
startServer(program).then(([c, l]) => {
310-
compiler = c
311-
resolve()
312-
})
297+
const selectPort = async port => {
298+
let foundPort = port
299+
const detectedPort = await detectPort(port)
300+
if (port !== detectedPort) {
301+
if (await readlinePort(port)) {
302+
foundPort = detectedPort
313303
}
314-
})
315-
})
304+
}
305+
return foundPort
306+
}
307+
program.port = await selectPort(port)
308+
const [compiler] = await startServer(program)
316309

317310
function prepareUrls(protocol, host, port) {
318311
const formatUrl = hostname =>

0 commit comments

Comments
 (0)