From 2a49718034005ad019f6f3c96f479968f4b43c57 Mon Sep 17 00:00:00 2001 From: alexlopez Date: Thu, 7 Apr 2016 22:01:42 -0400 Subject: [PATCH 1/2] Add serve command in cli --- lib/bin/cli.js | 16 ++++++++++++++++ lib/utils/serve.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 lib/utils/serve.js diff --git a/lib/bin/cli.js b/lib/bin/cli.js index 041ac56e32896..3755c1fb9e3f9 100644 --- a/lib/bin/cli.js +++ b/lib/bin/cli.js @@ -54,6 +54,22 @@ program.command('build') }) }) +program.command('serve') + .description('Serve built site') // eslint-disable-line max-len + .option('-h, --host ', + `Set host. Defaults to ${defaultHost}`, + defaultHost + ) + .option('-p, --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 [.].') diff --git a/lib/utils/serve.js b/lib/utils/serve.js new file mode 100644 index 0000000000000..b518b6da5ec6c --- /dev/null +++ b/lib/utils/serve.js @@ -0,0 +1,40 @@ +import Hapi from 'hapi' +import Path from 'path' + +const debug = require('debug')('gatsby:application') + +module.exports = (program) => { + const directory = program.directory + + debug('Serving /public') + + // 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 + }) + + 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) + }) + +} From 5e4b71f8f344e0e7c370e2af76bfe4ba517ffb81 Mon Sep 17 00:00:00 2001 From: alexlopez Date: Thu, 7 Apr 2016 22:11:09 -0400 Subject: [PATCH 2/2] pass tests --- lib/utils/serve.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/utils/serve.js b/lib/utils/serve.js index b518b6da5ec6c..a8b6120fd1401 100644 --- a/lib/utils/serve.js +++ b/lib/utils/serve.js @@ -1,5 +1,4 @@ import Hapi from 'hapi' -import Path from 'path' const debug = require('debug')('gatsby:application') @@ -8,13 +7,13 @@ module.exports = (program) => { debug('Serving /public') - // Setup and start Hapi to serve html + static files + webpack-hot-middleware. + // Setup and start Hapi to static files. - const server = new Hapi.Server(); + const server = new Hapi.Server() server.connection({ host: program.host, - port: program.port + port: program.port, }) server.route({ @@ -36,5 +35,4 @@ module.exports = (program) => { } return console.log('Listening at:', server.info.uri) }) - }