|
1 | | -const detect = require(`detect-port`) |
| 1 | +const { promisify } = require(`util`) |
| 2 | +const detectPort = promisify(require(`detect-port`)) |
2 | 3 | const report = require(`gatsby-cli/lib/reporter`) |
3 | 4 |
|
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 | +} |
17 | 13 |
|
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 |
26 | 20 | } |
27 | | - }) |
| 21 | + } |
| 22 | + return foundPort |
28 | 23 | } |
29 | 24 |
|
30 | 25 | module.exports = detectPortInUseAndPrompt |
0 commit comments