Skip to content

Commit

Permalink
Update serve command (#2763)
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense authored and KyleAMathews committed Nov 2, 2017
1 parent f659fe4 commit bff0cd3
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 378 deletions.
32 changes: 14 additions & 18 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,17 @@
"babel-traverse": "^6.24.1",
"babylon": "^6.17.3",
"bluebird": "^3.5.0",
"boom": "^2.7.2",
"chalk": "^1.1.3",
"chokidar": "^1.7.0",
"chunk-manifest-webpack-plugin": "0.1.0",
"commander": "^2.11.0",
"common-tags": "^1.4.0",
"convert-hrtime": "^2.0.0",
"copyfiles": "^1.2.0",
"core-js": "^2.5.0",
"css-loader": "^0.26.1",
"debug": "^2.6.0",
"del": "^3.0.0",
"detect-port": "^1.2.1",
"domready": "^1.0.8",
"dotenv": "^4.0.0",
"eventemitter2": "^4.1.0",
"express": "^4.14.0",
"express-graphql": "^0.6.6",
"extract-text-webpack-plugin": "^1.0.1",
Expand All @@ -57,8 +52,6 @@
"graphql": "^0.11.7",
"graphql-relay": "^0.5.1",
"graphql-skip-limit": "^1.0.7",
"gray-matter": "^2.1.0",
"hapi": "^8.5.1",
"history": "^4.6.2",
"invariant": "^2.2.2",
"is-relative": "^0.2.1",
Expand All @@ -78,7 +71,6 @@
"node-libs-browser": "^2.0.0",
"normalize-path": "^2.1.1",
"null-loader": "^0.1.1",
"observable": "^2.1.4",
"opn": "^5.1.0",
"parse-filepath": "^1.0.1",
"path-exists": "^3.0.0",
Expand All @@ -96,14 +88,15 @@
"redux": "^3.6.0",
"relay-compiler": "^1.4.1",
"remote-redux-devtools": "^0.5.7",
"serve": "^6.4.0",
"sift": "^3.2.6",
"signal-exit": "^3.0.2",
"slash": "^1.0.0",
"socket.io": "^2.0.3",
"static-site-generator-webpack-plugin": "^3.4.1",
"string-similarity": "^1.2.0",
"style-loader": "^0.13.0",
"type-of": "^2.0.1",
"underscore.string": "^3.3.4",
"url-loader": "^0.5.7",
"webpack": "^1.13.3",
"webpack-configurator": "^0.3.0",
Expand All @@ -118,9 +111,6 @@
"devDependencies": {
"babel-cli": "^6.26.0",
"cross-env": "^5.0.5",
"iflow-debug": "^1.0.15",
"iflow-lodash": "^1.1.24",
"iflow-react-router": "^1.2.1",
"nyc": "^7.0.0",
"rimraf": "^2.6.1"
},
Expand All @@ -147,13 +137,19 @@
"url": "git+https://github.com/gatsbyjs/gatsby.git"
},
"scripts": {
"build": "rimraf dist && npm run build:src && npm run build:internal-plugins && npm run build:rawfiles",
"build:internal-plugins": "copyfiles -u 1 src/internal-plugins/**/package.json dist",
"build":
"rimraf dist && npm run build:src && npm run build:internal-plugins && npm run build:rawfiles",
"build:internal-plugins":
"copyfiles -u 1 src/internal-plugins/**/package.json dist",
"build:rawfiles": "copyfiles -u 1 src/internal-plugins/**/raw_* dist",
"build:src": "babel src --out-dir dist --source-maps --ignore gatsby-cli.js,raw_*,__tests__",
"clean-test-bundles": "find test/ -type f -name bundle.js* -exec rm -rf {} +",
"build:src":
"babel src --out-dir dist --source-maps --ignore gatsby-cli.js,raw_*,__tests__",
"clean-test-bundles":
"find test/ -type f -name bundle.js* -exec rm -rf {} +",
"prepublish": "cross-env NODE_ENV=production npm run build",
"test-coverage": "node_modules/.bin/nyc --reporter=lcov --reporter=text npm test",
"watch": "rimraf dist && mkdir dist && npm run build:internal-plugins && npm run build:rawfiles && npm run build:src -- --watch"
"test-coverage":
"node_modules/.bin/nyc --reporter=lcov --reporter=text npm test",
"watch":
"rimraf dist && mkdir dist && npm run build:internal-plugins && npm run build:rawfiles && npm run build:src -- --watch"
}
}
84 changes: 7 additions & 77 deletions packages/gatsby/src/commands/serve.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,14 @@
/* @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`)

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

debug(`Serving /public`)
const server = new Hapi.Server()

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

server.route({
method: `GET`,
path: `/{path*}`,
handler: {
directory: {
path: `${directory}/public`,
listing: false,
index: true,
},
},
})

server.start(e => {
if (e) {
if (e.code === `EADDRINUSE`) {
// 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)
}
console.log(`Listening at:`, server.info.uri)
}
})
}
const serve = require(`serve`)
const signalExit = require(`signal-exit`)

module.exports = program => {
const port =
typeof program.port === `string` ? parseInt(program.port, 10) : program.port

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

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

return rlInterface.question(question, answer => {
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
program.port = _port
}
let { port, open, directory } = program
port = typeof port === `string` ? parseInt(port, 10) : port

return startServer(program, program.port)
})
}
let server = serve(`${directory}/public`, { port, open })

return startServer(program)
signalExit((code, signal) => {
server.stop()
})
}
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const Promise = require(`bluebird`)
const _ = require(`lodash`)
const { composeWithDevTools } = require(`remote-redux-devtools`)
const fs = require(`fs`)
const EventEmitter = require(`eventemitter2`)
const mitt = require(`mitt`)
const stringify = require(`json-stringify-safe`)

// Create event emitter for actions
const emitter = new EventEmitter()
const emitter = mitt()

// Reducers
const reducers = require(`./reducers`)
Expand Down
5 changes: 0 additions & 5 deletions packages/gatsby/src/utils/globals.js

This file was deleted.

1 change: 1 addition & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"build": "gatsby build",
"deploy": "gatsby build --prefix-paths && gh-pages -d public",
"develop": "gatsby develop",
"serve": "gatsby serve",
"test": "echo \"Error: no test specified\" && exit 1"
}
}
Loading

0 comments on commit bff0cd3

Please sign in to comment.