File tree Expand file tree Collapse file tree 3 files changed +93
-1
lines changed Expand file tree Collapse file tree 3 files changed +93
-1
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ var fs = require('../lib/generate/fs');
14
14
var utils = require ( './utils' ) ;
15
15
var build = require ( './build' ) ;
16
16
var Server = require ( './server' ) ;
17
+ var platform = require ( "./platform" ) ;
17
18
18
19
// General options
19
20
prog
@@ -115,6 +116,22 @@ prog
115
116
return initDir ( dir ) ;
116
117
} ) ;
117
118
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
+
118
135
// Parse and fallback to help if no args
119
136
if ( _ . isEmpty ( prog . parse ( process . argv ) . args ) && process . argv . length === 2 ) {
120
137
prog . help ( ) ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 1
1
var Q = require ( 'q' ) ;
2
2
var _ = require ( 'lodash' ) ;
3
3
4
+ var exec = require ( 'child_process' ) . exec ;
4
5
var http = require ( 'http' ) ;
5
6
var send = require ( 'send' ) ;
6
7
@@ -36,9 +37,30 @@ function logError(err) {
36
37
return Q . reject ( err ) ;
37
38
} ;
38
39
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
+
39
60
40
61
// Exports
41
62
module . exports = {
42
63
watch : watch ,
43
- logError : logError
64
+ logError : logError ,
65
+ gitCmd : runGitCommand
44
66
} ;
You can’t perform that action at this time.
0 commit comments