forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use detect-port to check if desired is already used.
Based on create-react-app and specifically facebook/create-react-app#101 In production, will exit(1) if the port is in use.
- Loading branch information
Showing
6 changed files
with
55 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import rl from 'readline'; | ||
|
||
export default function (question) { | ||
const rlInterface = rl.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
return new Promise((resolve) => { | ||
rlInterface.question(question + '\n', function(answer) { | ||
rlInterface.close(); | ||
resolve(answer); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import prompt from './prompt'; | ||
import detect from 'detect-port'; | ||
import chalk from 'chalk'; | ||
|
||
const defaults = { shouldPrompt: true }; | ||
export default async function run(opts) { | ||
const { srv, port: desiredPort, shouldPrompt } = { ...defaults, ...opts }; | ||
const port = await detect(desiredPort); | ||
if (port !== desiredPort) { | ||
if (!shouldPrompt) { | ||
// Fail early if no prompt | ||
console.error(`Error: Something is already running at port ${desiredPort}. Exiting.`); | ||
process.exit(1); | ||
} | ||
|
||
// Prompt the user to change the port. | ||
let shouldChangePort = false; | ||
if (shouldPrompt) { | ||
const question = chalk.red(`Something is already running at port ${desiredPort}.\n` + | ||
`Would you like to run the app on port ${port} instead? [Y/n]`); | ||
const answer = await prompt(question); | ||
shouldChangePort = !answer.length || answer.match(/^yes|y$/i); | ||
} | ||
if (!shouldChangePort) { | ||
console.log(chalk.red('Exiting.')); | ||
process.exit(0); | ||
} | ||
} | ||
await srv.start(port); | ||
console.log(`Ready on ${chalk.cyan(`http://localhost:${port}`)}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters