summaryrefslogtreecommitdiffstats
path: root/lib/index.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-10-05 09:54:40 +0200
committerSamy Pessé <samypesse@gmail.com>2015-10-05 09:54:40 +0200
commit6c2539e886a2893aa7d9bd4c4f84260145de6bd1 (patch)
treec90dce4bb73fd29f4c75cfffd6e65c508b21084d /lib/index.js
parentaba25d8521a8f7129721c7b52621da7abc6222c5 (diff)
downloadgitbook-6c2539e886a2893aa7d9bd4c4f84260145de6bd1.zip
gitbook-6c2539e886a2893aa7d9bd4c4f84260145de6bd1.tar.gz
gitbook-6c2539e886a2893aa7d9bd4c4f84260145de6bd1.tar.bz2
Move Book.init in another file
Diffstat (limited to 'lib/index.js')
-rw-r--r--lib/index.js126
1 files changed, 66 insertions, 60 deletions
diff --git a/lib/index.js b/lib/index.js
index 89d5d3c..5a93a54 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,50 +1,56 @@
-var Q = require("q");
-var _ = require("lodash");
-var path = require("path");
-var tinylr = require("tiny-lr");
-var color = require("bash-color");
-
-var Book = require("./book");
-var Server = require("./utils/server");
-var stringUtils = require("./utils/string");
-var watch = require("./utils/watch");
-var logger = require("./utils/logger");
+/*eslint no-console: 0*/
+
+var Q = require('q');
+var _ = require('lodash');
+var path = require('path');
+var tinylr = require('tiny-lr');
+var color = require('bash-color');
+
+var Book = require('./book');
+var initBook = require('./init');
+var Server = require('./utils/server');
+var stringUtils = require('./utils/string');
+var watch = require('./utils/watch');
+var logger = require('./utils/logger');
var LOG_OPTION = {
- name: "log",
- description: "Minimum log level to display",
+ name: 'log',
+ description: 'Minimum log level to display',
values: _.chain(logger.LEVELS).keys().map(stringUtils.toLowerCase).value(),
- defaults: "info"
+ defaults: 'info'
};
var FORMAT_OPTION = {
- name: "format",
- description: "Format to build to",
- values: ["website", "json", "ebook"],
- defaults: "website"
+ name: 'format',
+ description: 'Format to build to',
+ values: ['website', 'json', 'ebook'],
+ defaults: 'website'
};
+// Export init to gitbook library
+Book.init = initBook;
+
module.exports = {
Book: Book,
LOG_LEVELS: logger.LEVELS,
commands: _.flatten([
{
- name: "build [book] [output]",
- description: "build a book",
+ name: 'build [book] [output]',
+ description: 'build a book',
options: [
FORMAT_OPTION,
LOG_OPTION
],
exec: function(args, kwargs) {
var input = args[0] || process.cwd();
- var output = args[1] || path.join(input, "_book");
+ var output = args[1] || path.join(input, '_book');
var book = new Book(input, _.extend({}, {
- "config": {
- "output": output
+ 'config': {
+ 'output': output
},
- "logLevel": kwargs.log
+ 'logLevel': kwargs.log
}));
return book.parse()
@@ -52,16 +58,16 @@ module.exports = {
return book.generate(kwargs.format);
})
.then(function(){
- console.log("");
- console.log(color.green("Done, without error"));
+ console.log('');
+ console.log(color.green('Done, without error'));
});
}
},
- _.map(["pdf", "epub", "mobi"], function(ebookType) {
+ _.map(['pdf', 'epub', 'mobi'], function(ebookType) {
return {
- name: ebookType+" [book] [output]",
- description: "build a book to "+ebookType,
+ name: ebookType+' [book] [output]',
+ description: 'build a book to '+ebookType,
options: [
LOG_OPTION
],
@@ -70,7 +76,7 @@ module.exports = {
var output = args[1];
var book = new Book(input, _.extend({}, {
- "logLevel": kwargs.log
+ 'logLevel': kwargs.log
}));
return book.parse()
@@ -80,30 +86,30 @@ module.exports = {
});
})
.then(function(){
- console.log("");
- console.log(color.green("Done, without error"));
+ console.log('');
+ console.log(color.green('Done, without error'));
});
}
};
}),
{
- name: "serve [book]",
- description: "Build then serve a gitbook from a directory",
+ name: 'serve [book]',
+ description: 'Build then serve a gitbook from a directory',
options: [
{
- name: "port",
- description: "Port for server to listen on",
+ name: 'port',
+ description: 'Port for server to listen on',
defaults: 4000
},
{
- name: "lrport",
- description: "Port for livereload server to listen on",
+ name: 'lrport',
+ description: 'Port for livereload server to listen on',
defaults: 35729
},
{
- name: "watch",
- description: "Enable/disable file watcher",
+ name: 'watch',
+ description: 'Enable/disable file watcher',
defaults: true
},
FORMAT_OPTION,
@@ -118,15 +124,15 @@ module.exports = {
var lrPath;
var generate = function() {
- if (server.isRunning()) console.log("Stopping server");
+ if (server.isRunning()) console.log('Stopping server');
return server.stop()
.then(function() {
var book = new Book(input, _.extend({}, {
- "config": {
- "defaultsPlugins": ["livereload"]
+ 'config': {
+ 'defaultsPlugins': ['livereload']
},
- "logLevel": kwargs.log
+ 'logLevel': kwargs.log
}));
return book.parse()
@@ -137,10 +143,10 @@ module.exports = {
})
.then(function(book) {
console.log();
- console.log("Starting server ...");
+ console.log('Starting server ...');
return server.start(book.options.output, kwargs.port)
.then(function() {
- console.log("Serving book on http://localhost:"+kwargs.port);
+ console.log('Serving book on http://localhost:'+kwargs.port);
if (lrPath) {
// trigger livereload
@@ -157,8 +163,8 @@ module.exports = {
.then(function(filepath) {
// set livereload path
lrPath = filepath;
- console.log("Restart after change in file", filepath);
- console.log("");
+ console.log('Restart after change in file', filepath);
+ console.log('');
return generate();
});
});
@@ -167,17 +173,17 @@ module.exports = {
return Q.nfcall(lrServer.listen.bind(lrServer), kwargs.lrport)
.then(function() {
- console.log("Live reload server started on port:", kwargs.lrport);
- console.log("Press CTRL+C to quit ...");
- console.log("");
+ console.log('Live reload server started on port:', kwargs.lrport);
+ console.log('Press CTRL+C to quit ...');
+ console.log('');
return generate();
});
}
},
{
- name: "install [book]",
- description: "install plugins dependencies",
+ name: 'install [book]',
+ description: 'install plugins dependencies',
exec: function(args) {
var input = args[0] || process.cwd();
@@ -188,20 +194,20 @@ module.exports = {
return book.plugins.install();
})
.then(function(){
- console.log("");
- console.log(color.green("Done, without error"));
+ console.log('');
+ console.log(color.green('Done, without error'));
});
}
},
{
- name: "init [directory]",
- description: "create files and folders based on contents of SUMMARY.md",
+ name: 'init [directory]',
+ description: 'create files and folders based on contents of SUMMARY.md',
exec: function(args) {
- return Book.init(args[0] || process.cwd())
+ return initBook(args[0] || process.cwd())
.then(function(){
- console.log("");
- console.log(color.green("Done, without error"));
+ console.log('');
+ console.log(color.green('Done, without error'));
});
}
}