diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-05 20:22:26 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-05 20:22:27 -0700 |
commit | a4c1cf93340eb3938ef39b9d455892ae0afcd4ce (patch) | |
tree | f2a6a53e020730c8519922fe972a138cb4c00c4c /bin/gitbook.js | |
parent | 9ffec175a5cd5cd4e4e310ea14d6c28cb576bfc7 (diff) | |
download | gitbook-a4c1cf93340eb3938ef39b9d455892ae0afcd4ce.zip gitbook-a4c1cf93340eb3938ef39b9d455892ae0afcd4ce.tar.gz gitbook-a4c1cf93340eb3938ef39b9d455892ae0afcd4ce.tar.bz2 |
Refactor bin/gitbook.js
Diffstat (limited to 'bin/gitbook.js')
-rwxr-xr-x | bin/gitbook.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/bin/gitbook.js b/bin/gitbook.js new file mode 100755 index 0000000..6248581 --- /dev/null +++ b/bin/gitbook.js @@ -0,0 +1,87 @@ +#! /usr/bin/env node + +var Q = require('q'); +var _ = require('lodash'); +var path = require('path'); +var prog = require('commander'); +var tmp = require('tmp'); + +var pkg = require('../package.json'); +var generators = require("../lib/generate").generators; +var fs = require('../lib/generate/fs'); + +var utils = require('./utils'); +var buildFunc = require('./build'); + +// General options +prog +.version(pkg.version) +.option('-o, --output <directory>', 'Path to output directory, defaults to ./_book') +.option('-f, --format <name>', 'Change generation format, defaults to site, availables are: '+_.keys(generators).join(", ")) +.option('-t, --title <name>', 'Name of the book to generate, defaults to repo name') +.option('-i, --intro <intro>', 'Description of the book to generate') +.option('-g, --github <repo_path>', 'ID of github repo like : username/repo') +.option('-gh, --githubHost <url>', 'The url of the github host (defaults to https://github.com/') +.option('--theme <path>', 'Path to theme directory'); + + +var buildFunc; +prog +.command('build [source_dir]') +.description('Build a gitbook from a directory') +.action(); + +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) +.action(function(dir, options) { + buildFunc(dir, options) + .then(function(outputDir) { + console.log(); + console.log('Starting server ...'); + return utils.serveDir(outputDir, options.port) + .fail(utils.logError); + }) + .then(function() { + console.log('Serving book on http://localhost:'+options.port); + console.log(); + console.log('Press CTRL+C to quit ...'); + }); +}); + +prog +.command('pdf [source_dir] [output_file]') +.description('Build a gitbook as a PDF') +.option('-pf, --paperformat <format>', 'PDF paper format (default is A4): "5in*7.5in", "10cm*20cm", "A4", "Letter"') +.action(function(dir, outputFile, options) { + outputFile = outputFile || path.resolve(dir, "book.pdf"); + + Q.nfcall(tmp.dir) + .then(function(tmpDir) { + return buildFunc( + dir, + _.extend(options, { + output: tmpDir, + format: "pdf" + }) + ) + .then(function() { + console.log("Generating PDF in", outputFile); + return fs.copy( + path.join(tmpDir, "index.pdf"), + outputFile + ) + return fs.remove(tmpDir) + .fail(utils.logError); + }); + }) + +}); + + + +// Parse and fallback to help if no args +if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) { + prog.help(); +} |