Skip to content

Commit

Permalink
Allow dynamic ports
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Oct 13, 2017
1 parent b75d613 commit e92fd30
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
"cross-spawn": "^5.1.0",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"find-port": "^2.0.1",
"fs-extra": "^4.0.2",
"inquirer": "^3.3.0",
"inquirer-autocomplete-prompt": "^0.11.1",
"match-sorter": "^2.0.2",
"openport": "^0.0.4",
"postcss-flexbugs-fixes": "^3.2.0",
"postcss-loader": "^2.0.6",
"prop-types": "^15.5.10",
Expand Down
22 changes: 8 additions & 14 deletions src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@ import fs from 'fs-extra'
import path from 'path'
import { renderToStaticMarkup } from 'react-dom/server'
import WebpackConfigurator from 'webpack-configurator'
import findPort from 'find-port'
import express from 'express'
import cors from 'cors'
//
import DefaultHtml from '../DefaultHtml'
import copyPublicFolder from '../copyPublicFolder'
import { getConfig, writeRouteComponentsToFile } from '../static'
import { getConfig, writeRouteComponentsToFile, findAvailablePort } from '../static'
import { DIST, SRC } from '../paths'

const port = process.env.PORT || '3000'

let first = true
let compiler

const config = getConfig()

async function startConfigServer () {
// scan a range
const ports = await new Promise(resolve =>
findPort('127.0.0.1', 8000, 8500, ports => {
resolve(ports)
}),
)
const port = ports[0]
const port = await findAvailablePort(8000)
process.env.STATIC_ENDPOINT = `http://127.0.0.1:${port}`

const configApp = express()
Expand Down Expand Up @@ -77,7 +69,7 @@ async function startConfigServer () {
})
}

function buildCompiler () {
function buildCompiler ({ port }) {
const webpackConfig = new WebpackConfigurator()

const webpackConfigDev = require('../webpack.config.dev').default
Expand Down Expand Up @@ -128,7 +120,7 @@ function buildCompiler () {
})
}

function startDevServer () {
function startDevServer ({ port }) {
const devServer = new WebpackDevServer(compiler, {
port,
hot: true,
Expand Down Expand Up @@ -157,6 +149,8 @@ export default async () => {
const config = getConfig()
await fs.remove(DIST)

const port = await findAvailablePort(3000)

const siteProps = await config.getSiteProps({ dev: true })

const HtmlTemplate = config.Html || DefaultHtml
Expand Down Expand Up @@ -196,8 +190,8 @@ export default async () => {
console.log('=> Building...')
console.time(chalk.green('=> [\u2713] Build Complete'))
await startConfigServer()
buildCompiler()
startDevServer()
buildCompiler({ port })
startDevServer({ port })
} catch (err) {
console.log(err)
process.exit(1)
Expand Down
17 changes: 17 additions & 0 deletions src/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { renderToString } from 'react-dom/server'
import fs from 'fs-extra'
import path from 'path'
import Helmet from 'react-helmet'
import OpenPort from 'openport'
//
import DefaultHtml from './DefaultHtml'
import { pathJoin } from './shared'
Expand Down Expand Up @@ -312,3 +313,19 @@ export const writeRouteComponentsToFile = async routes => {
const then = now - 1000
fs.utimesSync(filepath, then, then)
}

export const findAvailablePort = start =>
new Promise((resolve, reject) =>
OpenPort.find(
{
startingPort: start,
endingPort: start + 1000,
},
(err, port) => {
if (err) {
return reject(err)
}
resolve(port)
},
),
)
14 changes: 4 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,6 @@ async@^2.1.2:
dependencies:
lodash "^4.14.0"

async@~0.2.9:
version "0.2.10"
resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
Expand Down Expand Up @@ -2535,12 +2531,6 @@ find-cache-dir@^1.0.0:
make-dir "^1.0.0"
pkg-dir "^2.0.0"

find-port@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/find-port/-/find-port-2.0.1.tgz#e59923ce3283fcdfb1dfe029f8c19cc5c020a827"
dependencies:
async "~0.2.9"

find-up@^1.0.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f"
Expand Down Expand Up @@ -3936,6 +3926,10 @@ opener@^1.4.3:
version "1.4.3"
resolved "https://registry.yarnpkg.com/opener/-/opener-1.4.3.tgz#5c6da2c5d7e5831e8ffa3964950f8d6674ac90b8"

openport@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/openport/-/openport-0.0.4.tgz#1d6715d8a8789695f985fa84f68dd4cd1ba426cb"

opn@5.1.0, opn@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/opn/-/opn-5.1.0.tgz#72ce2306a17dbea58ff1041853352b4a8fc77519"
Expand Down

0 comments on commit e92fd30

Please sign in to comment.