diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-03-09 10:43:12 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-03-09 10:43:12 +0100 |
commit | 34fc2831e0cf0fed01c71cec28d93472d87f455b (patch) | |
tree | a803cc907c20491ba02863b5d3dd5aedf6bfed10 /bin | |
parent | e1594cde2c32e4ff48f6c4eff3d3d461743d74e1 (diff) | |
parent | 1bf68a5aa0703b5a1815cfe4ebb731b5fb6ed9d2 (diff) | |
download | gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.zip gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.tar.gz gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.tar.bz2 |
Merge branch 'version/2.0'
Diffstat (limited to 'bin')
-rw-r--r-- | bin/build.js | 56 | ||||
-rwxr-xr-x | bin/gitbook.js | 160 | ||||
-rw-r--r-- | bin/platform.js | 53 | ||||
-rw-r--r-- | bin/server.js | 96 | ||||
-rw-r--r-- | bin/utils.js | 96 |
5 files changed, 4 insertions, 457 deletions
diff --git a/bin/build.js b/bin/build.js deleted file mode 100644 index 28d7641..0000000 --- a/bin/build.js +++ /dev/null @@ -1,56 +0,0 @@ -var path = require('path'); -var Q = require('q'); -var _ = require('lodash'); -var fs = require('fs'); - -var utils = require('./utils'); -var generate = require("../lib/generate"); -var parse = require("../lib/parse"); -var generators = require("../lib/generate").generators; - -var buildCommand = function(command) { - return command - .option('-v, --verbose', 'Activate verbose mode, useful for debugging errors') - .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('--config <config file>', 'Configuration file to use, defaults to book.js or book.json'); -}; - - -var buildEbookCommand = function(command) { - return buildCommand(command) - .option('-c, --cover <path>', 'Cover image, default is cover.jpg if exists'); -}; - -var makeBuildFunc = function(converter) { - return function(dir, options) { - dir = dir || process.cwd(); - outputDir = options.output; - - // Set debugging - if(options.verbose) { - process.env.DEBUG = "true"; - } - - console.log('Starting build ...'); - return converter( - _.extend({}, options || {}, { - input: dir, - output: outputDir, - generator: options.format, - configFile: options.config - }) - ) - .then(function(output) { - console.log("Successfully built!"); - return output; - }); - }; -}; - -module.exports = { - folder: makeBuildFunc(generate.folder), - file: makeBuildFunc(generate.file), - command: buildCommand, - commandEbook: buildEbookCommand -}; diff --git a/bin/gitbook.js b/bin/gitbook.js index 856d3e3..1487f52 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -1,159 +1,7 @@ #! /usr/bin/env node -var Q = require('q'); -var _ = require('lodash'); -var path = require('path'); -var prog = require('commander'); -var tinylr = require('tiny-lr-fork'); +var color = require('bash-color'); -var pkg = require('../package.json'); -var genbook = require("../lib/generate"); -var initDir = require("../lib/generate/init"); -var fs = require('../lib/generate/fs'); - -var utils = require('./utils'); -var action = utils.action; -var build = require('./build'); -var Server = require('./server'); -var platform = require("./platform"); - -// General options -prog -.version(pkg.version); - -build.command(prog.command('build [source_dir]')) -.description('Build a gitbook from a directory') -.action(action(build.folder)); - -build.command(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('--no-watch', 'Disable restart with file watching') -.action(action(function(dir, options) { - var server = new Server(); - - // init livereload server - var lrOptions = {port: 35729}; - var lrServer = tinylr(lrOptions); - var lrPath = undefined; - lrServer.listen(lrOptions.port, function(err) { - if (err) { return console.log(err); } - console.log('Live reload server started on port: ' + lrOptions.port); - }); - - var generate = function() { - if (server.isRunning()) { - console.log("Stopping server"); - } - - return server.stop() - .then(function() { - return build.folder(dir, _.extend(options || {}, { - defaultsPlugins: ["livereload"] - })); - }) - .then(function(_options) { - console.log(); - console.log('Starting server ...'); - return server.start(_options.output, options.port) - .then(function() { - console.log('Serving book on http://localhost:'+options.port); - - if (lrPath) { - // trigger livereload - lrServer.changed({body:{files:[lrPath]}}) - } - - if (!options.watch) return; - return utils.watch(_options.input) - .then(function(filepath) { - // set livereload path - lrPath = filepath; - console.log("Restart after change in files"); - console.log(''); - return generate(); - }) - }) - }) - .fail(utils.logError); - }; - - console.log('Press CTRL+C to quit ...'); - console.log('') - - return generate(); -})); - -build.commandEbook(prog.command('install [source_dir]')) -.description('Install plugins for a book') -.action(action(function(dir, options) { - dir = dir || process.cwd(); - - console.log("Install plugins in", dir); - - return genbook.config.read({ - input: dir - }) - .then(function(options) { - return genbook.Plugin.install(options); - }) - .then(function() { - console.log("Successfully installed plugins!"); - }); -})); - -build.commandEbook(prog.command('pdf [source_dir]')) -.description('Build a gitbook as a PDF') -.action(action(function(dir, options) { - return build.file(dir, _.extend(options, { - extension: "pdf", - format: "ebook" - })); -})); - -build.commandEbook(prog.command('epub [source_dir]')) -.description('Build a gitbook as a ePub book') -.action(action(function(dir, options) { - return build.file(dir, _.extend(options, { - extension: "epub", - format: "ebook" - })); -})); - -build.commandEbook(prog.command('mobi [source_dir]')) -.description('Build a gitbook as a Mobi book') -.action(action(function(dir, options) { - return build.file(dir, _.extend(options, { - extension: "mobi", - format: "ebook" - })); -})); - -prog -.command('init [source_dir]') -.description('Create files and folders based on contents of SUMMARY.md') -.action(action(function(dir) { - dir = dir || process.cwd(); - return initDir(dir); -})); - -prog -.command('publish [source_dir]') -.description('Publish content to the associated gitbook.com book') -.action(action(function(dir) { - dir = dir || process.cwd(); - return platform.publish(dir); -})); - -prog -.command('git:remote [source_dir] [book_id]') -.description('Adds a git remote to a book repository') -.action(action(function(dir, bookId) { - dir = dir || process.cwd(); - return platform.remote(dir, bookId); -})); - -// Parse and fallback to help if no args -if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) { - prog.help(); -} +console.log(color.red("You need to install 'gitbook-cli' to have access to the gitbook command anywhere on your system.")); +console.log(color.red("If you've installe this package globally, you need to uninstall it.")); +console.log(color.red(">> Run 'npm uninstall -g gitbook' then 'npm install -g gitbook-cli'")); diff --git a/bin/platform.js b/bin/platform.js deleted file mode 100644 index 35d581d..0000000 --- a/bin/platform.js +++ /dev/null @@ -1,53 +0,0 @@ -var Q = require("q"); -var utils = require("./utils"); - -var publish = function(folder) { - if (!folder) { - console.log("Need a repository folder"); - return process.exit(-1); - } - - utils.gitCmd("push", ["gitbook", "master"]) - .then(function(out) { - console.log(out.stdout); - }, function(err) { - if (err.code == 128) { - console.log("No book on gitbook.com is configured with this git repository."); - console.log("Run 'gitbook git:remote username/book' to intialize this repository."); - } else { - console.log(err.message); - } - process.exit(-1); - }); -}; - -var remote = function(folder, bookId) { - if (!folder || !bookId) { - console.log("Need a repository folder and a book id"); - return process.exit(-1); - } - - var url = "https://git.gitbook.com/"+bookId+".git"; - var addRemote = function() { - return utils.gitCmd("remote", ["add", "gitbook", url]); - } - - addRemote() - .fail(function(err) { - if (err.code == 128) { - return utils.gitCmd("remote", ["rm", "gitbook"]).then(addRemote); - } - return Q.reject(err); - }) - .then(function(out) { - console.log("Book remote '"+url+"' added to the folder"); - }, function(err) { - console.log(err.message); - process.exit(-1); - }); -}; - -module.exports = { - publish: publish, - remote: remote -}; diff --git a/bin/server.js b/bin/server.js deleted file mode 100644 index 2b97fe8..0000000 --- a/bin/server.js +++ /dev/null @@ -1,96 +0,0 @@ -var Q = require('q'); -var _ = require('lodash'); - -var events = require('events'); -var http = require('http'); -var send = require('send'); -var util = require('util'); -var url = require('url'); - -var Server = function() { - this.running = null; - this.dir = null; - this.port = 0; - this.sockets = []; -}; -util.inherits(Server, events.EventEmitter); - -// Return true if the server is running -Server.prototype.isRunning = function() { - return this.running != null; -}; - -// Stop the server -Server.prototype.stop = function() { - var that = this; - if (!this.isRunning()) return Q(); - - var d = Q.defer(); - this.running.close(function(err) { - that.running = null; - that.emit("state", false); - - if (err) d.reject(err); - else d.resolve(); - }); - - for (var i = 0; i < this.sockets.length; i++) { - this.sockets[i].destroy(); - } - - return d.promise; -}; - -Server.prototype.start = function(dir, port) { - var that = this, pre = Q(); - port = port || 8004; - - if (that.isRunning()) pre = this.stop(); - return pre - .then(function() { - var d = Q.defer(); - - that.running = 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); - }); - - that.running.on('connection', function (socket) { - that.sockets.push(socket); - socket.setTimeout(4000); - socket.on('close', function () { - that.sockets.splice(that.sockets.indexOf(socket), 1); - }); - }); - - that.running.listen(port, function(err) { - if (err) return d.reject(err); - - that.port = port; - that.dir = dir; - that.emit("state", true); - d.resolve(); - }); - - return d.promise; - }); -} - -module.exports = Server; diff --git a/bin/utils.js b/bin/utils.js deleted file mode 100644 index 84d2e2f..0000000 --- a/bin/utils.js +++ /dev/null @@ -1,96 +0,0 @@ -var Q = require('q'); -var _ = require('lodash'); - -var exec = require('child_process').exec; -var http = require('http'); -var send = require('send'); - -var path = require('path'); -var Gaze = require('gaze').Gaze; - -function watch(dir) { - var d = Q.defer(); - dir = path.resolve(dir); - - var gaze = new Gaze("**/*.md", { - cwd: dir - }); - - gaze.once("all", function(e, filepath) { - gaze.close(); - - d.resolve(filepath); - }); - gaze.once("error", function(err) { - gaze.close(); - - d.reject(err); - }); - - return d.promise; -} - -// exit wraps a promise -// and forcefully quits the program when the promise is resolved -function exit(promise) { - promise - .then(function() { - // Prevent badly behaving plugins - // from making the process hang - process.exit(0); - }, function(err) { - // Log error - logError(err); - - // Exit process with failure code - process.exit(-1); - }); -} - -// CLI action wrapper, calling exit when finished -function action(f) { - return function() { - // Call func and get optional promise - var p = f.apply(null, arguments); - - // Exit process - return exit(Q(p)); - } -} - -function logError(err) { - var message = err.message || err; - if (process.env.DEBUG != null) message = err.stack || message; - console.log(message); - return Q.reject(err); -}; - -function runGitCommand(command, args) { - var d = Q.defer(), child; - args = ["git", command].concat(args).join(" "); - - child = exec(args, function (error, stdout, stderr) { - if (error !== null) { - error.stdout = stdout; - error.stderr = stderr; - d.reject(error); - } else { - d.resolve({ - stdout: stdout, - stderr: stderr - }) - } - }); - - return d.promise; -}; - - -// Exports -module.exports = { - exit: exit, - action: action, - watch: watch, - logError: logError, - gitCmd: runGitCommand -}; |