diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-01 01:14:56 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-01 01:14:56 -0700 |
commit | cc340db0faa412edec47a7d37ce591501ab454fb (patch) | |
tree | 9e59af9d89e028aeb590753ccf0f5b54e6a31ed2 /bin | |
parent | f7767484371bac0948cddffe916de014a2413c93 (diff) | |
download | gitbook-cc340db0faa412edec47a7d37ce591501ab454fb.zip gitbook-cc340db0faa412edec47a7d37ce591501ab454fb.tar.gz gitbook-cc340db0faa412edec47a7d37ce591501ab454fb.tar.bz2 |
Add cli command serve
This does a build, then spins up a HTTP server to serve the books
contents
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/gitbook.js | 34 | ||||
-rw-r--r-- | bin/utils.js | 34 |
2 files changed, 65 insertions, 3 deletions
diff --git a/bin/gitbook.js b/bin/gitbook.js index 6cb4798..fc9bfc3 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -20,18 +20,20 @@ prog .option('-d, --dir <source_directory>', 'Source directory of book, containing Markdown files'); +var buildFunc; prog .command('build [source_dir]') .description('Build a gitbook from a directory') .option('-o, --output <directory>', 'Path to output directory, defaults to ./_book') .option('-t, --title <name>', 'Name of the book to generate, defaults to repo name') .option('-g, --github <repo_path>', 'ID of github repo like : username/repo') -.action(function(dir, options) { +.action(buildFunc = function(dir, options) { dir = dir || process.cwd(); outputDir = options.output || path.join(dir, '_book'); + console.log('Starting build ...'); // Get repo's URL - utils.gitURL(dir) + return utils.gitURL(dir) .then(function(url) { // Get ID of repo return utils.githubID(url); @@ -50,9 +52,35 @@ prog ); }) .then(function(output) { - console.log("Done!"); + console.log("Successfuly built !"); }, function(err) { console.log(err.stack || err); + throw err; + }) + .then(_.constant(outputDir)); +}); + +prog +.command('serve [source_dir]') +.description('Build then serve a gitbook from a directory') +.option('-p, --port <port>', 'Port for server to listen on', 4000) +.option('-o, --output <directory>', 'Path to output directory, defaults to ./_book') +.option('-t, --title <name>', 'Name of the book to generate, defaults to repo name') +.option('-g, --github <repo_path>', 'ID of github repo like : username/repo') +.action(function(dir, options) { + buildFunc(dir, options) + .then(function(outputDir) { + console.log(); + console.log('Starting server ...'); + return utils.serveDir(outputDir, options.port); + }) + .then(function() { + console.log('Serving book on http://localhost:'+options.port); + console.log(); + console.log('Press CTRL+C to quit ...'); + }) + .fail(function(err) { + console.error(err); }); }); diff --git a/bin/utils.js b/bin/utils.js index 804d6f0..77d70c6 100644 --- a/bin/utils.js +++ b/bin/utils.js @@ -1,6 +1,9 @@ var Q = require('q'); var _ = require('lodash'); +var http = require('http'); +var send = require('send'); + var url = require('url'); var cp = require('child_process'); @@ -49,10 +52,41 @@ function titleCase(str) return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); } +function serveDir(dir, port) { + var d = Q.defer(); + + var server = http.createServer(function(req, res){ + // Render error + function error(err) { + res.statusCode = err.status || 500; + res.end(err.message); + } + + // Redirect to directory's index.html + function redirect() { + res.statusCode = 301; + res.setHeader('Location', req.url + '/'); + res.end('Redirecting to ' + req.url + '/'); + } + + // Send file + send(req, url.parse(req.url).pathname) + .root(dir) + .on('error', error) + .on('directory', redirect) + .pipe(res); + }).listen(port); + + d.resolve(server); + + return d.promise; +} + // Exports module.exports = { gitURL: gitURL, githubID: githubID, titleCase: titleCase, + serveDir: serveDir, }; |