UNPKG

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