(host, defaultPort)
| 389 | } |
| 390 | |
| 391 | function choosePort(host, defaultPort) { |
| 392 | return detect(defaultPort, host).then( |
| 393 | port => |
| 394 | new Promise(resolve => { |
| 395 | if (port === defaultPort) { |
| 396 | return resolve(port); |
| 397 | } |
| 398 | const message = |
| 399 | process.platform !== 'win32' && defaultPort < 1024 && !isRoot() |
| 400 | ? `Admin permissions are required to run a server on a port below 1024.` |
| 401 | : `Something is already running on port ${defaultPort}.`; |
| 402 | if (isInteractive) { |
| 403 | clearConsole(); |
| 404 | const existingProcess = getProcessForPort(defaultPort); |
| 405 | const question = { |
| 406 | type: 'confirm', |
| 407 | name: 'shouldChangePort', |
| 408 | message: |
| 409 | chalk.yellow( |
| 410 | message + |
| 411 | `${existingProcess ? ` Probably:\n ${existingProcess}` : ''}` |
| 412 | ) + '\n\nWould you like to run the app on another port instead?', |
| 413 | initial: true, |
| 414 | }; |
| 415 | prompts(question).then(answer => { |
| 416 | if (answer.shouldChangePort) { |
| 417 | resolve(port); |
| 418 | } else { |
| 419 | resolve(null); |
| 420 | } |
| 421 | }); |
| 422 | } else { |
| 423 | console.log(chalk.red(message)); |
| 424 | resolve(null); |
| 425 | } |
| 426 | }), |
| 427 | err => { |
| 428 | throw new Error( |
| 429 | chalk.red(`Could not find an open port at ${chalk.bold(host)}.`) + |
| 430 | '\n' + |
| 431 | ('Network error message: ' + err.message || err) + |
| 432 | '\n' |
| 433 | ); |
| 434 | } |
| 435 | ); |
| 436 | } |
| 437 | |
| 438 | module.exports = { |
| 439 | choosePort, |
no test coverage detected