Skip to content

Commit

Permalink
Merge pull request #237 from alehlopeh/master
Browse files Browse the repository at this point in the history
Add serve command in cli
  • Loading branch information
KyleAMathews committed Apr 9, 2016
2 parents 5ab7a0c + 5e4b71f commit 9e43993
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/bin/cli.js
Expand Up @@ -54,6 +54,22 @@ program.command('build')
})
})

program.command('serve')
.description('Serve built site') // eslint-disable-line max-len
.option('-h, --host <url>',
`Set host. Defaults to ${defaultHost}`,
defaultHost
)
.option('-p, --port <port>', 'Set port. Defaults to 8000', '8000')
.action((command) => {
const serve = require('../utils/serve')
const p = {
...command,
directory,
}
serve(p)
})

program
.command('new [rootPath] [starter]')
.description('Create new Gatsby project in path [.].')
Expand Down
38 changes: 38 additions & 0 deletions lib/utils/serve.js
@@ -0,0 +1,38 @@
import Hapi from 'hapi'

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

module.exports = (program) => {
const directory = program.directory

debug('Serving /public')

// Setup and start Hapi to static files.

const server = new Hapi.Server()

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

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


server.start((e) => {
if (e) {
console.log(e)
}
return console.log('Listening at:', server.info.uri)
})
}

0 comments on commit 9e43993

Please sign in to comment.