Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
Prompt user to provide a new port if 4000 is already in use (#27)
Browse files Browse the repository at this point in the history
* Prompt user to provide a new port if 4000 is already in use

* fixing lint errors
  • Loading branch information
Yegor Sytnyk authored and egoist committed Oct 24, 2016
1 parent af8ee4f commit e280ce8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lib/detect-free-port.js
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
})
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const merge = require('lodash.merge')
const build = require('./build')
const dev = require('./dev')
const updateConfig = require('./update-config')
const detectFreePort = require('./detect-free-port')

module.exports = co.wrap(function * (options) {
options.config = options.config === false
Expand Down Expand Up @@ -36,8 +37,14 @@ module.exports = co.wrap(function * (options) {

process.env.NODE_ENV = isDev ? 'development' : 'production'

let port = yield detectFreePort(4000)

if (!port) {
return
}

const buildOptions = merge(
{port: 4000},
{port},
fileConfig,
isDev ? devConfig : prodConfig,
options
Expand Down
43 changes: 43 additions & 0 deletions lib/terminal-utils.js
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
}

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"compression-webpack-plugin": "^0.3.1",
"css-loader": "^0.25.0",
"detect-installed": "^1.0.4",
"detect-port": "^1.0.1",
"eslint": "^3.6.1",
"eslint-config-egoist-vue": "^2.0.6",
"eslint-loader": "^1.5.0",
Expand Down
8 changes: 7 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ commander@^2.2.0, commander@^2.5.0, commander@^2.8.1, commander@^2.9.0, commande
dependencies:
graceful-readlink ">= 1.0.0"

commander@2.8.x:
commander@~2.8.1, commander@2.8.x:
version "2.8.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.8.1.tgz#06be367febfda0c330aa1e2a072d3dc9762425d4"
dependencies:
Expand Down Expand Up @@ -2126,6 +2126,12 @@ detect-installed@^1.0.4:
dependencies:
global-modules "^0.2.0"

detect-port:
version "1.0.1"
resolved "https://registry.yarnpkg.com/detect-port/-/detect-port-1.0.1.tgz#3e1aa6a7ff6677bb60894b291172529d880c1e85"
dependencies:
commander "~2.8.1"

detective@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/detective/-/detective-4.3.1.tgz#9fb06dd1ee8f0ea4dbcc607cda39d9ce1d4f726f"
Expand Down

0 comments on commit e280ce8

Please sign in to comment.