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..a8b6120fd1401 --- /dev/null +++ b/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) + }) +}