Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Use node-portfinder to avoid error when port is used (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
thangngoc89 authored and MoOx committed Apr 9, 2016
1 parent 45fb018 commit f960c3d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- Added: Use node-portfinder to avoid error when port is used
([#320](https://github.com/MoOx/statinamic/issues/320))

# 0.9.3 - 2016-04-04

## Boilerplate
Expand Down
33 changes: 33 additions & 0 deletions __tests__/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,42 @@ test.cb("should NOT throw if a CLI flag is recognized", (t) => {
}
)

// ...or be ok quickly
// (so we assume it's ok and kill the process, we don't need the actual build)
const timeout = setTimeout(() => {
child.kill()
t.pass()
t.end()
}, 500)
})

test.cb("should NOT throw if port is used", (t) => {
const app = require("express")()

const server = app.listen(8081, (err) => {
if (err) {
t.fail()
t.end()
}

const child = exec(
`${ statinamic } start --devPort=8081`, execOpts,

(err) => {
if (err) {
console.log(err)
clearTimeout(timeout)
t.fail()
t.end()
}
}
)

const timeout = setTimeout(() => {
child.kill()
server.close()
t.pass()
t.end()
}, 2000)
})
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"lru-memoize": "^1.0.0",
"mkdirp": "^0.5.1",
"opn": "^3.0.2",
"portfinder": "^1.0.2",
"redbox-react": "^1.2.2",
"remark": "^4.1.1",
"remark-autolink-headings": "^3.0.0",
Expand Down
26 changes: 18 additions & 8 deletions src/builder/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import WebpackErrorNotificationPlugin from "webpack-error-notification"

import opn from "opn"
import debug from "debug"
import portFinder from "portfinder"

import collection from "../content-loader/cache.js"
import urlAsHtml from "../static/to-html/url-as-html"
Expand Down Expand Up @@ -196,17 +197,26 @@ export default (options = {}) => {
// THAT'S IT
const { devHost, devPort } = config

server.listen(devPort, devHost, (err) => {
if (err) {
log(err)
portFinder.basePort = devPort

return
portFinder.getPort((err, port) => {
if (err) {
throw err
}
const href = `http://${ devHost }:${ devPort }${ config.baseUrl.pathname }`
log(`Dev server started on ${ href }`)
if (config.open) {
opn(href.replace(devHost, "localhost"))

if (port !== devHost) {
log(`Port ${ devPort } is not available. Using port ${ port } instead.`)
}
server.listen(port, devHost, (err) => {
if (err) {
throw err
}
const href = `http://${ devHost }:${ port }${ config.baseUrl.pathname }`
log(`Dev server started on ${ href }`)
if (config.open) {
opn(href.replace(devHost, "localhost"))
}
})
})
}

Expand Down

0 comments on commit f960c3d

Please sign in to comment.