-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitbook.js
More file actions
executable file
·198 lines (175 loc) · 5.77 KB
/
gitbook.js
File metadata and controls
executable file
·198 lines (175 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#! /usr/bin/env node
var Q = require('q');
var _ = require('lodash');
var path = require('path');
var program = require('commander');
var parsedArgv = require('optimist').argv;
var color = require('bash-color');
var pkg = require('../package.json');
var manager = require('../lib');
var tags = require('../lib/tags');
var commands = require('../lib/commands');
// Which book is concerned?
var bookRoot = parsedArgv._[1] || process.cwd();
function runPromise(p) {
return p
.then(function() {
process.exit(0);
}, function(err) {
console.log('');
console.log(color.red(err.toString()));
if (program.debug || process.env.DEBUG) console.log(err.stack || '');
process.exit(1);
});
}
function printGitbookVersion(v) {
var actualVersion = (v.name != v.version)? ' ('+v.version+')' : '';
return v.name + actualVersion;
}
// Init gitbook-cli
manager.init();
program
.option('-v, --gitbook [version]', 'specify GitBook version to use')
.option('-d, --debug', 'enable verbose error')
.option('-V, --version', 'Display running versions of gitbook and gitbook-cli', function() {
console.log('CLI version:', pkg.version);
runPromise(
manager.ensure(bookRoot, program.gitbook)
.then(function(v) {
console.log('GitBook version:', printGitbookVersion(v));
process.exit(0);
})
);
});
program
.command('ls')
.description('List versions installed locally')
.action(function(){
var versions = manager.versions();
if (versions.length > 0) {
console.log('GitBook Versions Installed:');
console.log('');
_.each(versions,function(v, i) {
var text = v.name;
if (v.name != v.version) text += ' [' + v.version + ']';
if (v.link) text = text + ' (alias of ' + v.link + ')';
console.log(' ', i == 0? '*' : ' ', text);
});
console.log('');
console.log('Run "gitbook update" to update to the latest version.');
} else {
console.log('There is no versions installed');
console.log('You can install the latest version using: "gitbook fetch"');
}
});
program
.command('current')
.description('Display currently activated version')
.action(function(){
runPromise(
manager.ensure(bookRoot, program.gitbook)
.then(function(v) {
console.log('GitBook version is', printGitbookVersion(v));
})
);
});
program
.command('ls-remote')
.description('List remote versions available for install')
.action(function(){
runPromise(
manager.available()
.then(function(available) {
console.log('Available GitBook Versions:');
console.log('');
console.log(' ', available.versions.join(', '));
console.log('');
console.log('Tags:');
console.log('');
_.each(available.tags, function(version, tagName) {
console.log(' ', tagName, ':', version);
});
console.log('');
})
);
});
program
.command('fetch [version]')
.description('Download and install a <version>')
.action(function(version){
version = version || '*';
runPromise(
manager.install(version)
.then(function(installedVersion) {
console.log('');
console.log(color.green('GitBook '+installedVersion+' has been installed'));
})
);
});
program
.command('alias [folder] [version]')
.description('Set an alias named <version> pointing to <folder>')
.action(function(folder, version) {
folder = path.resolve(folder || process.cwd());
version = version || 'latest';
runPromise(
manager.link(version, folder)
.then(function() {
console.log(color.green('GitBook '+version+' point to '+folder));
})
);
});
program
.command('uninstall [version]')
.description('Uninstall a version')
.action(function(version){
runPromise(
manager.uninstall(version)
.then(function() {
console.log(color.green('GitBook '+version+' has been uninstalled.'));
})
);
});
program
.command('update [tag]')
.description('Update to the latest version of GitBook')
.action(function(tag){
runPromise(
manager.update(tag)
.then(function(version) {
if (!version) {
console.log('No update found!');
} else {
console.log('');
console.log(color.green('GitBook has been updated to '+version));
}
})
);
});
program
.command('help')
.description('List commands for GitBook')
.action(function(){
runPromise(
manager.ensureAndLoad(bookRoot, program.gitbook)
.get('commands')
.then(commands.help)
);
});
program
.command('*')
.description('run a command with a specific gitbook version')
.action(function(commandName){
var args = parsedArgv._.slice(1);
var kwargs = _.omit(parsedArgv, '$0', '_');
runPromise(
manager.ensureAndLoad(bookRoot, program.gitbook)
.then(function(gitbook) {
return commands.exec(gitbook.commands, commandName, args, kwargs);
})
);
});
// Parse and fallback to help if no args
if(_.isEmpty(program.parse(process.argv).args) && process.argv.length === 2) {
program.help();
}