Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Webpack Validator Into Webpack.Config.js #381

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ask user for new port if in use
  • Loading branch information
LukeSheard committed Aug 1, 2016
commit f5817803ee7cd6e39c688872c8b272351e33b9a4
60 changes: 43 additions & 17 deletions lib/utils/develop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow weak */
require('node-cjsx').transform()
import detect from 'detect-port'
import Hapi from 'hapi'
import Boom from 'boom'
import React from 'react'
Expand All @@ -13,13 +14,20 @@ import WebpackPlugin from 'hapi-webpack-plugin'
import opn from 'opn'
import fs from 'fs'
import glob from 'glob'
import rl from 'readline'

const rlInterface = rl.createInterface({
input: process.stdin,
output: process.stdout,
})

import globPages from './glob-pages'
import webpackConfig from './webpack.config'
const debug = require('debug')('gatsby:application')

module.exports = (program) => {
function startServer (program, launchPort) {
const directory = program.directory
const serverPort = launchPort || program.port

// Load pages for the site.
return globPages(directory, (err, pages) => {
Expand All @@ -46,11 +54,11 @@ module.exports = (program) => {
const HTML = factory()
debug('Configuring develop server')

// Setup and start Hapi to serve html + static files + webpack-hot-middleware.
const server = new Hapi.Server()

server.connection({
host: program.host,
port: program.port,
port: serverPort,
})

server.route({
Expand Down Expand Up @@ -82,7 +90,7 @@ module.exports = (program) => {
path: '/{path*}',
handler: {
directory: {
path: `${directory}/pages`,
path: `${program.directory}/pages`,
listing: false,
index: false,
},
Expand All @@ -96,7 +104,7 @@ module.exports = (program) => {
const parsed = parsePath(request.path)
const page = _.find(pages, (p) => p.path === (`${parsed.dirname}/`))

let absolutePath = `${directory}/pages`
let absolutePath = `${program.directory}/pages`
let path
if (page) {
path = `/${parsePath(page.requirePath).dirname}/${parsed.basename}`
Expand Down Expand Up @@ -145,7 +153,7 @@ module.exports = (program) => {
},
}

server.register({
return server.register({
register: WebpackPlugin,
options: {
compiler,
Expand All @@ -161,20 +169,13 @@ module.exports = (program) => {
server.start((e) => {
if (e) {
if (e.code === 'EADDRINUSE') {
const finder = require('process-finder')
finder.find({ elevate: false, port: program.port }, (startErr, pids) => {
const msg =
`We were unable to start Gatsby on port ${program.port} as there's already a process
listening on that port (PID: ${pids[0]}). You can either use a different port
(e.g. gatsby develop --port ${parseInt(program.port, 10) + 1}) or stop the process already listening
on your desired port.`
console.log(msg)
process.exit()
})
// eslint-disable-next-line max-len
console.log(`Unable to start Gatsby on port ${serverPort} as there's already a process listing on that port.`)
} else {
console.log(e)
process.exit()
}

process.exit()
} else {
if (program.open) {
opn(server.info.uri)
Expand All @@ -186,3 +187,28 @@ on your desired port.`
})
})
}

module.exports = (program) => {
detect(program.port, (err, _port) => {
if (err) {
console.error(err)
process.exit()
}

if (program.port !== _port) {
// eslint-disable-next-line max-len
const question = `Something is already running at port ${program.port} \nWould you like to run the app at another port instead? [Y/n] `

return rlInterface.question(question, (answer) => {
let launchPort = program.port
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
launchPort = _port
}

return startServer(program, launchPort)
})
}

return startServer(program)
})
}
54 changes: 39 additions & 15 deletions lib/utils/serve-build.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
/* @flow weak */
import detect from 'detect-port'
import Hapi from 'hapi'
import opn from 'opn'
import rl from 'readline'

const rlInterface = rl.createInterface({
input: process.stdin,
output: process.stdout,
})

const debug = require('debug')('gatsby:application')

module.exports = (program) => {
function startServer (program, launchPort) {
const directory = program.directory
const serverPort = launchPort || program.port

debug('Serving /public')

// Setup and start Hapi to static files.

const server = new Hapi.Server()

server.connection({
host: program.host,
port: program.port,
port: serverPort,
})

server.route({
Expand All @@ -34,19 +39,13 @@ module.exports = (program) => {
server.start((e) => {
if (e) {
if (e.code === 'EADDRINUSE') {
const finder = require('process-finder')
finder.find({ elevate: false, port: program.port }, (startErr, pids) => {
const msg =
`We were unable to start Gatsby on port ${program.port} as there's already a process
listening on that port (PID: ${pids[0]}). You can either use a different port
(e.g. gatsby serve-build --port ${parseInt(program.port, 10) + 1}) or stop the process
already listening on your desired port.`
console.log(msg)
process.exit()
})
// eslint-disable-next-line max-len
console.log(`Unable to start Gatsby on port ${serverPort} as there's already a process listing on that port.`)
} else {
console.log(e)
}

process.exit()
} else {
if (program.open) {
opn(server.info.uri)
Expand All @@ -55,3 +54,28 @@ already listening on your desired port.`
}
})
}

module.exports = (program) => {
detect(program.port, (err, _port) => {
if (err) {
console.error(err)
process.exit()
}

if (program.port !== _port) {
// eslint-disable-next-line max-len
const question = `Something is already running at port ${program.port} \nWould you like to run the app at another port instead? [Y/n] `

return rlInterface.question(question, (answer) => {
let launchPort = program.port
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
launchPort = _port
}

return startServer(program, launchPort)
})
}

return startServer(program)
})
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.5.0",
"boom": "^2.7.2",
"chalk": "^1.1.3",
"cjsx-loader": "^3.0.0",
"coffee-loader": "^0.7.2",
"coffee-script": "^1.9.3",
"commander": "^2.9.0",
"css-loader": "^0.23.1",
"debug": "^2.2.0",
"detect-port": "^1.0.0",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.9.0",
"front-matter": "^2.1.0",
Expand Down