Skip to content

Commit bc581cc

Browse files
committed
Simplify commands
1 parent 8e3e881 commit bc581cc

11 files changed

+418
-346
lines changed

.eslintrc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"rules": {
3+
"indent": [ 2, 4 ],
4+
"quotes": [ 2, "single" ],
5+
"linebreak-style": [ 2, "unix" ],
6+
"semi": [ 2, "always" ],
7+
"no-unused-vars": [ 2, {
8+
"vars": "all",
9+
"args": "none"
10+
} ],
11+
"spaced-comment": [ 2, "always" ]
12+
},
13+
"env": {
14+
"node": true,
15+
"mocha": true,
16+
"browser": true
17+
},
18+
"extends": "eslint:recommended"
19+
}

.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "4.2"
5-
- "4.1"
4+
- "stable"
65
- "0.12"
7-
- "0.11"
86
- "0.10"
97

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ Install this globally and you'll have access to the gitbook command anywhere on
1212
$ npm install -g gitbook-cli
1313
```
1414

15-
**Note:** The job of the gitbook command is to load and run the version of GitBook you have specified in your book (or the latest one), irrespective of its version. The GitBook CLI only support versions `>=2.0.0` of GitBook.
15+
**Note:** The purpose of the gitbook command is to load and run the version of GitBook you have specified in your book (or the latest one), irrespective of its version. The GitBook CLI only support versions `>=2.0.0` of GitBook.
16+
17+
`gitbook-cli` store GitBook's versions into `~/.gitbook`, you can set the `GITBOOK_DIR` environment variable to use another directory.
1618

1719
## How to install it?
1820

bin/gitbook.js

+58-53
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
#! /usr/bin/env node
22

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');
66
var program = require('commander');
77
var parsedArgv = require('optimist').argv;
88
var color = require('bash-color');
99

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();
1418

1519
function runPromise(p) {
1620
return p
1721
.then(function() {
1822
process.exit(0);
1923
}, function(err) {
20-
console.log("");
24+
console.log('');
2125
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 || '');
2327
process.exit(1);
2428
});
2529
}
@@ -35,128 +39,129 @@ program
3539

3640

3741
program
38-
.command('versions')
39-
.description('list installed versions')
42+
.command('ls')
43+
.description('List versions installed locally')
4044
.action(function(){
41-
var _versions = versions.list();
45+
var versions = manager.versions();
4246

43-
if (_versions.length > 0) {
47+
if (versions.length > 0) {
4448
console.log('GitBook Versions Installed:');
4549
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 + ')';
4954

50-
console.log(' ', v.latest? '*' : ' ', text);
55+
console.log(' ', i == 0? '*' : ' ', text);
5156
});
5257
console.log('');
5358
} else {
5459
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"');
5661
}
5762
});
5863

5964
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')
6267
.action(function(){
6368
runPromise(
64-
versions.current(program.gitbook)
69+
manager.ensure(bookRoot, program.gitbook)
6570
.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.');
6773
})
6874
);
6975
});
7076

7177
program
72-
.command('versions:available')
73-
.description('list available versions on NPM')
78+
.command('ls-remote')
79+
.description('List remote versions available for install')
7480
.action(function(){
7581
runPromise(
76-
versions.available()
82+
manager.available()
7783
.then(function(available) {
7884
console.log('Available GitBook Versions:');
7985
console.log('');
80-
console.log(' ', available.versions.join(", "));
86+
console.log(' ', available.versions.join(', '));
8187
console.log('');
8288
console.log('Tags:');
8389
console.log('');
8490
_.each(available.tags, function(version, tagName) {
85-
console.log(' ', tagName, ":", version);
91+
console.log(' ', tagName, ':', version);
8692
});
8793
console.log('');
8894
})
8995
);
9096
});
9197

9298
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>')
95101
.action(function(version){
96-
version = version || "*";
102+
version = version || '*';
97103

98104
runPromise(
99-
versions.install(version)
105+
manager.install(version)
100106
.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'));
103109
})
104110
);
105111
});
106112

107113
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>')
110116
.action(function(folder, version) {
111117
folder = path.resolve(folder || process.cwd());
112118
version = version || 'latest';
113119

114120
runPromise(
115-
versions.link(version, folder)
121+
manager.link(version, folder)
116122
.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));
119125
})
120126
);
121127
});
122128

123129
program
124-
.command('versions:uninstall [version]')
125-
.description('uninstall a specific version of gitbook')
130+
.command('uninstall [version]')
131+
.description('Uninstall a version')
126132
.action(function(version){
127133
runPromise(
128-
versions.uninstall(version)
134+
manager.uninstall(version)
129135
.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.'));
132137
})
133138
);
134139
});
135140

136141
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')
139144
.action(function(tag){
140145
runPromise(
141-
versions.update(tag)
146+
manager.update(tag)
142147
.then(function(version) {
143148
if (!version) {
144-
console.log("No update found!");
149+
console.log('No update found!');
145150
} 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));
148153
}
149154
})
150155
);
151156
});
152157

153158
program
154159
.command('help')
155-
.description('list commands for a specific version of gitbook')
160+
.description('List commands for GitBook')
156161
.action(function(){
157162
runPromise(
158-
versions.get(program.gitbook)
159-
.get("commands")
163+
manager.load(program.gitbook)
164+
.get('commands')
160165
.then(commands.help)
161166
);
162167
});
@@ -169,7 +174,7 @@ program
169174
var kwargs = _.omit(parsedArgv, '$0', '_');
170175

171176
runPromise(
172-
versions.get(program.gitbook)
177+
manager.load(program.gitbook)
173178
.then(function(gitbook) {
174179
return commands.exec(gitbook.commands, commandName, args, kwargs);
175180
})

lib/commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function exec(commands, command, args, kwargs) {
3333
// Apply defaults
3434
_.each(cmd.options || [], function(option) {
3535
kwargs[option.name] = (kwargs[option.name] === undefined)? option.defaults : kwargs[option.name];
36-
if (option.values && !_.contains(option.values, kwargs[option.name])) {
36+
if (option.values && !_.includes(option.values, kwargs[option.name])) {
3737
throw new Error('Invalid value for option "'+option.name+'"');
3838
}
3939
});

lib/config.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ var fs = require('fs-extra');
33
var color = require('bash-color');
44
var userHome = require('user-home');
55

6-
if (!userHome) {
7-
console.log(color.red('HOME directory needs to be defined'));
8-
process.exit(1);
6+
var CONFIG_ROOT = process.env.GITBOOK_DIR;
7+
if (!CONFIG_ROOT) {
8+
if (!userHome) {
9+
console.log(color.red('HOME or GITBOOK_DIR needs to be defined'));
10+
process.exit(1);
11+
}
12+
13+
CONFIG_ROOT = path.resolve(userHome, '.gitbook');
914
}
10-
11-
var CONFIG_ROOT = path.resolve(userHome, '.gitbook');
1215
var VERSIONS_ROOT = path.resolve(CONFIG_ROOT, 'versions');
1316

1417

lib/index.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var Q = require('q');
2+
var _ = require('lodash');
3+
4+
var local = require('./local');
5+
var registry = require('./registry');
6+
7+
// Ensure that a version exists
8+
// or install it
9+
function ensureVersion(bookRoot, version, opts) {
10+
opts = _.defaults(opts || {}, {
11+
install: true
12+
});
13+
14+
return Q()
15+
16+
// If not defined, load version required from book.json
17+
.then(function() {
18+
if (version) return;
19+
try {
20+
var bookJson = require(path.resolve(bookRoot, 'book'));
21+
version = bookJson.gitbook;
22+
} catch (e) {}
23+
})
24+
25+
// Resolve version locally
26+
.then(function() {
27+
version = version || '*';
28+
return local.resolve(version)
29+
})
30+
31+
// Install if needed
32+
.fail(function(err) {
33+
if (opts.install == false) throw err;
34+
35+
return registry.install(version)
36+
.then(function() {
37+
return ensureVersion(bookRoot, version, {
38+
install: false
39+
});
40+
});
41+
});
42+
}
43+
44+
45+
// Update current version
46+
// -> Check that a newer version exists
47+
// -> Install it
48+
// -> Remove previous version
49+
function updateVersion(tag) {
50+
tag = tag || 'latest';
51+
52+
return local.current(null, {
53+
install: false
54+
})
55+
.fail(function(err) {
56+
return Q(null);
57+
})
58+
.then(function(currentV) {
59+
return registry.versions()
60+
.then(function(result) {
61+
var remoteVersion = result.tags[tag];
62+
if (!remoteVersion) throw new Error('Tag doesn\'t exist: '+tag);
63+
64+
if (currentV && tags.sort(remoteVersion, currentV.version) >= 0) return null;
65+
66+
return registry.install(remoteVersion)
67+
.then(function() {
68+
if (!currentV) return;
69+
return local.remove(currentV.tag);
70+
})
71+
.thenResolve(remoteVersion);
72+
});
73+
});
74+
}
75+
76+
module.exports = {
77+
load: local.load,
78+
ensure: ensureVersion,
79+
current: local.current,
80+
uninstall: local.remove,
81+
link: local.link,
82+
versions: local.versions,
83+
84+
update: updateVersion,
85+
86+
install: registry.install,
87+
available: registry.versions
88+
};

0 commit comments

Comments
 (0)