Skip to content

Commit f05b138

Browse files
committed
Add base command git:push and git:remote
1 parent a9f4732 commit f05b138

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

bin/gitbook.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var fs = require('../lib/generate/fs');
1414
var utils = require('./utils');
1515
var build = require('./build');
1616
var Server = require('./server');
17+
var platform = require("./platform");
1718

1819
// General options
1920
prog
@@ -115,6 +116,22 @@ prog
115116
return initDir(dir);
116117
});
117118

119+
prog
120+
.command('git:push [source_dir]')
121+
.description('Publish content to the associated gitbook.io book')
122+
.action(function(dir) {
123+
dir = dir || process.cwd();
124+
return platform.publish(dir);
125+
});
126+
127+
prog
128+
.command('git:remote [source_dir] [book_id]')
129+
.description('Adds a git remote to a book repository')
130+
.action(function(dir, bookId) {
131+
dir = dir || process.cwd();
132+
return platform.remote(dir, bookId);
133+
});
134+
118135
// Parse and fallback to help if no args
119136
if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) {
120137
prog.help();

bin/platform.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var Q = require("q");
2+
var utils = require("./utils");
3+
4+
var publish = function(folder) {
5+
if (!folder) {
6+
console.log("Need a repository folder");
7+
return process.exit(-1);
8+
}
9+
10+
utils.gitCmd("push", ["gitbook", "master"])
11+
.then(function(out) {
12+
console.log(out.stdout);
13+
}, function(err) {
14+
if (err.code == 128) {
15+
console.log("No book on gitbook.io is configured with this git repository.");
16+
console.log("Run 'gitbook git:remote username/book' to intialize this repository.");
17+
} else {
18+
console.log(err.message);
19+
}
20+
process.exit(-1);
21+
});
22+
};
23+
24+
var remote = function(folder, bookId) {
25+
if (!folder || !bookId) {
26+
console.log("Need a repository folder and a book id");
27+
return process.exit(-1);
28+
}
29+
30+
var url = "https://push.gitbook.io/"+bookId+".git";
31+
var addRemote = function() {
32+
return utils.gitCmd("remote", ["add", "gitbook", url]);
33+
}
34+
35+
addRemote()
36+
.fail(function(err) {
37+
if (err.code == 128) {
38+
return utils.gitCmd("remote", ["rm", "gitbook"]).then(addRemote);
39+
}
40+
return Q.reject(err);
41+
})
42+
.then(function(out) {
43+
console.log("Book remote '"+url+"' added to the folder");
44+
}, function(err) {
45+
console.log(err.message);
46+
process.exit(-1);
47+
});
48+
};
49+
50+
module.exports = {
51+
publish: publish,
52+
remote: remote
53+
};

bin/utils.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Q = require('q');
22
var _ = require('lodash');
33

4+
var exec = require('child_process').exec;
45
var http = require('http');
56
var send = require('send');
67

@@ -36,9 +37,30 @@ function logError(err) {
3637
return Q.reject(err);
3738
};
3839

40+
function runGitCommand(command, args) {
41+
var d = Q.defer(), child;
42+
args = ["git", command].concat(args).join(" ");
43+
44+
child = exec(args, function (error, stdout, stderr) {
45+
if (error !== null) {
46+
error.stdout = stdout;
47+
error.stderr = stderr;
48+
d.reject(error);
49+
} else {
50+
d.resolve({
51+
stdout: stdout,
52+
stderr: stderr
53+
})
54+
}
55+
});
56+
57+
return d.promise;
58+
};
59+
3960

4061
// Exports
4162
module.exports = {
4263
watch: watch,
43-
logError: logError
64+
logError: logError,
65+
gitCmd: runGitCommand
4466
};

0 commit comments

Comments
 (0)