1
1
#! /usr/bin/env node
2
2
3
- var Q = require ( "q" ) ;
4
- var _ = require ( " lodash" ) ;
5
- var path = require ( " path" ) ;
3
+ var Q = require ( 'q' ) ;
4
+ var _ = require ( ' lodash' ) ;
5
+ var path = require ( ' path' ) ;
6
6
var program = require ( 'commander' ) ;
7
7
var parsedArgv = require ( 'optimist' ) . argv ;
8
8
var color = require ( 'bash-color' ) ;
9
9
10
- var pkg = require ( "../package.json" ) ;
11
- var config = require ( "../lib/config" ) ;
12
- var versions = require ( "../lib/versions" ) ;
13
- var commands = require ( "../lib/commands" ) ;
10
+ var pkg = require ( '../package.json' ) ;
11
+ var config = require ( '../lib/config' ) ;
12
+ var manager = require ( '../lib' ) ;
13
+ var registry = require ( '../lib/registry' ) ;
14
+ var commands = require ( '../lib/commands' ) ;
15
+
16
+ // Which book is concerned?
17
+ var bookRoot = parsedArgv . _ [ 1 ] || process . cwd ( ) ;
14
18
15
19
function runPromise ( p ) {
16
20
return p
17
21
. then ( function ( ) {
18
22
process . exit ( 0 ) ;
19
23
} , function ( err ) {
20
- console . log ( "" ) ;
24
+ console . log ( '' ) ;
21
25
console . log ( color . red ( err . toString ( ) ) ) ;
22
- if ( program . debug || process . env . DEBUG ) console . log ( err . stack || "" ) ;
26
+ if ( program . debug || process . env . DEBUG ) console . log ( err . stack || '' ) ;
23
27
process . exit ( 1 ) ;
24
28
} ) ;
25
29
}
@@ -35,128 +39,129 @@ program
35
39
36
40
37
41
program
38
- . command ( 'versions ' )
39
- . description ( 'list installed versions ' )
42
+ . command ( 'ls ' )
43
+ . description ( 'List versions installed locally ' )
40
44
. action ( function ( ) {
41
- var _versions = versions . list ( ) ;
45
+ var versions = manager . versions ( ) ;
42
46
43
- if ( _versions . length > 0 ) {
47
+ if ( versions . length > 0 ) {
44
48
console . log ( 'GitBook Versions Installed:' ) ;
45
49
console . log ( '' ) ;
46
- _ . each ( _versions , function ( v ) {
47
- var text = v . tag ;
48
- if ( v . link ) text = text + ' (-> ' + v . link + ' = ' + v . version + ')' ;
50
+ _ . each ( versions , function ( v , i ) {
51
+ var text = v . name ;
52
+ if ( v . name != v . version ) text += ' [' + v . version + ']' ;
53
+ if ( v . link ) text = text + ' (alias of ' + v . link + ')' ;
49
54
50
- console . log ( ' ' , v . latest ? '*' : ' ' , text ) ;
55
+ console . log ( ' ' , i == 0 ? '*' : ' ' , text ) ;
51
56
} ) ;
52
57
console . log ( '' ) ;
53
58
} else {
54
59
console . log ( 'There is no versions installed' ) ;
55
- console . log ( 'You can install the latest version using: "gitbook versions:install latest "' ) ;
60
+ console . log ( 'You can install the latest version using: "gitbook fetch "' ) ;
56
61
}
57
62
} ) ;
58
63
59
64
program
60
- . command ( 'versions:print ' )
61
- . description ( 'print current version to use in the current directory ' )
65
+ . command ( 'current ' )
66
+ . description ( 'Display currently activated version ' )
62
67
. action ( function ( ) {
63
68
runPromise (
64
- versions . current ( program . gitbook )
69
+ manager . ensure ( bookRoot , program . gitbook )
65
70
. then ( function ( v ) {
66
- console . log ( "GitBook version is" , v . tag , ( v . tag != v . version ? '(' + v . version + ')' : '' ) ) ;
71
+ console . log ( 'GitBook version is' , v . name , ( v . name != v . version ? '(' + v . version + ')' : '' ) ) ;
72
+ console . log ( 'Run "gitbook update" to update to the latest version.' ) ;
67
73
} )
68
74
) ;
69
75
} ) ;
70
76
71
77
program
72
- . command ( 'versions:available ' )
73
- . description ( 'list available versions on NPM ' )
78
+ . command ( 'ls-remote ' )
79
+ . description ( 'List remote versions available for install ' )
74
80
. action ( function ( ) {
75
81
runPromise (
76
- versions . available ( )
82
+ manager . available ( )
77
83
. then ( function ( available ) {
78
84
console . log ( 'Available GitBook Versions:' ) ;
79
85
console . log ( '' ) ;
80
- console . log ( ' ' , available . versions . join ( ", " ) ) ;
86
+ console . log ( ' ' , available . versions . join ( ', ' ) ) ;
81
87
console . log ( '' ) ;
82
88
console . log ( 'Tags:' ) ;
83
89
console . log ( '' ) ;
84
90
_ . each ( available . tags , function ( version , tagName ) {
85
- console . log ( ' ' , tagName , ":" , version ) ;
91
+ console . log ( ' ' , tagName , ':' , version ) ;
86
92
} ) ;
87
93
console . log ( '' ) ;
88
94
} )
89
95
) ;
90
96
} ) ;
91
97
92
98
program
93
- . command ( 'versions:install [version]' )
94
- . description ( 'force install a specific version of gitbook ' )
99
+ . command ( 'fetch [version]' )
100
+ . description ( 'Download and install a < version> ' )
95
101
. action ( function ( version ) {
96
- version = version || "*" ;
102
+ version = version || '*' ;
97
103
98
104
runPromise (
99
- versions . install ( version )
105
+ manager . install ( version )
100
106
. then ( function ( installedVersion ) {
101
- console . log ( "" ) ;
102
- console . log ( color . green ( " GitBook " + installedVersion + " has been installed" ) ) ;
107
+ console . log ( '' ) ;
108
+ console . log ( color . green ( ' GitBook ' + installedVersion + ' has been installed' ) ) ;
103
109
} )
104
110
) ;
105
111
} ) ;
106
112
107
113
program
108
- . command ( 'versions:link [folder] [version]' )
109
- . description ( 'link a version to a local folder' )
114
+ . command ( 'alias [folder] [version]' )
115
+ . description ( 'Set an alias named <version> pointing to < folder> ' )
110
116
. action ( function ( folder , version ) {
111
117
folder = path . resolve ( folder || process . cwd ( ) ) ;
112
118
version = version || 'latest' ;
113
119
114
120
runPromise (
115
- versions . link ( version , folder )
121
+ manager . link ( version , folder )
116
122
. then ( function ( ) {
117
- console . log ( "" ) ;
118
- console . log ( color . green ( " GitBook " + version + " point to " + folder ) ) ;
123
+ console . log ( '' ) ;
124
+ console . log ( color . green ( ' GitBook ' + version + ' point to ' + folder ) ) ;
119
125
} )
120
126
) ;
121
127
} ) ;
122
128
123
129
program
124
- . command ( 'versions: uninstall [version]' )
125
- . description ( 'uninstall a specific version of gitbook ' )
130
+ . command ( 'uninstall [version]' )
131
+ . description ( 'Uninstall a version' )
126
132
. action ( function ( version ) {
127
133
runPromise (
128
- versions . uninstall ( version )
134
+ manager . uninstall ( version )
129
135
. then ( function ( ) {
130
- console . log ( "" ) ;
131
- console . log ( color . green ( "GitBook " + version + " has been uninstalled" ) ) ;
136
+ console . log ( color . green ( 'GitBook ' + version + ' has been uninstalled.' ) ) ;
132
137
} )
133
138
) ;
134
139
} ) ;
135
140
136
141
program
137
- . command ( 'versions: update [tag]' )
138
- . description ( 'update to the latest version of gitbook ' )
142
+ . command ( 'update [tag]' )
143
+ . description ( 'Update to the latest version of GitBook ' )
139
144
. action ( function ( tag ) {
140
145
runPromise (
141
- versions . update ( tag )
146
+ manager . update ( tag )
142
147
. then ( function ( version ) {
143
148
if ( ! version ) {
144
- console . log ( " No update found!" ) ;
149
+ console . log ( ' No update found!' ) ;
145
150
} else {
146
- console . log ( "" ) ;
147
- console . log ( color . green ( " GitBook has been updated to " + version ) ) ;
151
+ console . log ( '' ) ;
152
+ console . log ( color . green ( ' GitBook has been updated to ' + version ) ) ;
148
153
}
149
154
} )
150
155
) ;
151
156
} ) ;
152
157
153
158
program
154
159
. command ( 'help' )
155
- . description ( 'list commands for a specific version of gitbook ' )
160
+ . description ( 'List commands for GitBook ' )
156
161
. action ( function ( ) {
157
162
runPromise (
158
- versions . get ( program . gitbook )
159
- . get ( " commands" )
163
+ manager . load ( program . gitbook )
164
+ . get ( ' commands' )
160
165
. then ( commands . help )
161
166
) ;
162
167
} ) ;
@@ -169,7 +174,7 @@ program
169
174
var kwargs = _ . omit ( parsedArgv , '$0' , '_' ) ;
170
175
171
176
runPromise (
172
- versions . get ( program . gitbook )
177
+ manager . load ( program . gitbook )
173
178
. then ( function ( gitbook ) {
174
179
return commands . exec ( gitbook . commands , commandName , args , kwargs ) ;
175
180
} )
0 commit comments