@@ -13,6 +13,7 @@ const { formatError } = require(`graphql`)
1313const request = require ( `request` )
1414const rl = require ( `readline` )
1515const webpack = require ( `webpack` )
16+ const { promisify } = require ( `util` )
1617const webpackConfig = require ( `../utils/webpack.config` )
1718const bootstrap = require ( `../bootstrap` )
1819const { store } = require ( `../redux` )
@@ -32,6 +33,7 @@ const slash = require(`slash`)
3233const { initTracer } = require ( `../utils/tracer` )
3334const apiRunnerNode = require ( `../utils/api-runner-node` )
3435const 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 ( / ^ y e s | 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 ( / ^ y e s | 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