diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-07-12 09:23:15 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-07-12 09:23:15 +0200 |
commit | f05b138fd6cbd46ff72ca45bb8f08994df9035b0 (patch) | |
tree | fa14cd4828dbe80c91567d72b1d5b3975ebe44f5 /bin | |
parent | a9f473260cdbf7944a8d95c4292a93897f9210eb (diff) | |
download | gitbook-f05b138fd6cbd46ff72ca45bb8f08994df9035b0.zip gitbook-f05b138fd6cbd46ff72ca45bb8f08994df9035b0.tar.gz gitbook-f05b138fd6cbd46ff72ca45bb8f08994df9035b0.tar.bz2 |
Add base command git:push and git:remote
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/gitbook.js | 17 | ||||
-rw-r--r-- | bin/platform.js | 53 | ||||
-rw-r--r-- | bin/utils.js | 24 |
3 files changed, 93 insertions, 1 deletions
diff --git a/bin/gitbook.js b/bin/gitbook.js index b868925..2020519 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -14,6 +14,7 @@ var fs = require('../lib/generate/fs'); var utils = require('./utils'); var build = require('./build'); var Server = require('./server'); +var platform = require("./platform"); // General options prog @@ -115,6 +116,22 @@ prog return initDir(dir); }); +prog +.command('git:push [source_dir]') +.description('Publish content to the associated gitbook.io book') +.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(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(); diff --git a/bin/platform.js b/bin/platform.js new file mode 100644 index 0000000..6bf0b94 --- /dev/null +++ b/bin/platform.js @@ -0,0 +1,53 @@ +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.io 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://push.gitbook.io/"+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/utils.js b/bin/utils.js index 74a09b5..821112e 100644 --- a/bin/utils.js +++ b/bin/utils.js @@ -1,6 +1,7 @@ var Q = require('q'); var _ = require('lodash'); +var exec = require('child_process').exec; var http = require('http'); var send = require('send'); @@ -36,9 +37,30 @@ function logError(err) { 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 = { watch: watch, - logError: logError + logError: logError, + gitCmd: runGitCommand }; |