This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prompt user to provide a new port if 4000 is already in use (#27)
* Prompt user to provide a new port if 4000 is already in use * fixing lint errors
- Loading branch information
Showing
5 changed files
with
82 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
const co = require('co') | ||
const detect = require('detect-port') | ||
const chalk = require('chalk') | ||
const terminalUtils = require('./terminal-utils') | ||
|
||
module.exports = co.wrap(function * (DEFAULT_PORT) { | ||
let port = yield detect(DEFAULT_PORT) | ||
|
||
if (port === DEFAULT_PORT) { | ||
return port | ||
} | ||
|
||
terminalUtils.clearConsole() | ||
|
||
let question | ||
= chalk.yellow(`Something is already running on port ${DEFAULT_PORT}.`) | ||
+ '\n\nWould you like to run the app on another port instead?' | ||
|
||
let shouldChangePort = yield terminalUtils.prompt(question, true) | ||
|
||
return shouldChangePort ? port : null | ||
}) |
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,43 @@ | ||
'use strict' | ||
const rl = require('readline') | ||
|
||
const _ = module.exports = {} | ||
|
||
// Convention: "no" should be the conservative choice. | ||
// If you mistype the answer, we'll always take it as a "no". | ||
// You can control the behavior on <Enter> with `isYesDefault`. | ||
_.prompt = function (question, isYesDefault) { | ||
if (typeof isYesDefault !== 'boolean') { | ||
throw new Error('Provide explicit boolean isYesDefault as second argument.') | ||
} | ||
return new Promise(resolve => { | ||
let rlInterface = rl.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
let hint = isYesDefault === true ? '[Y/n]' : '[y/N]' | ||
let message = question + ' ' + hint + '\n' | ||
|
||
rlInterface.question(message, function (answer) { | ||
rlInterface.close() | ||
|
||
let useDefault = answer.trim().length === 0 | ||
if (useDefault) { | ||
return resolve(isYesDefault) | ||
} | ||
|
||
let isYes = answer.match(/^(yes|y)$/i) | ||
return resolve(isYes) | ||
}) | ||
}) | ||
} | ||
|
||
let isFirstClear = true | ||
_.clearConsole = function clearConsole() { | ||
// On first run, clear completely so it doesn't show half screen on Windows. | ||
// On next runs, use a different sequence that properly scrolls back. | ||
process.stdout.write(isFirstClear ? '\x1bc' : '\x1b[2J\x1b[0f') | ||
isFirstClear = false | ||
} | ||
|
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