diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-10-05 09:54:40 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-10-05 09:54:40 +0200 |
commit | 6c2539e886a2893aa7d9bd4c4f84260145de6bd1 (patch) | |
tree | c90dce4bb73fd29f4c75cfffd6e65c508b21084d | |
parent | aba25d8521a8f7129721c7b52621da7abc6222c5 (diff) | |
download | gitbook-6c2539e886a2893aa7d9bd4c4f84260145de6bd1.zip gitbook-6c2539e886a2893aa7d9bd4c4f84260145de6bd1.tar.gz gitbook-6c2539e886a2893aa7d9bd4c4f84260145de6bd1.tar.bz2 |
Move Book.init in another file
-rw-r--r-- | lib/book.js | 74 | ||||
-rw-r--r-- | lib/index.js | 126 | ||||
-rw-r--r-- | lib/init.js | 83 | ||||
-rw-r--r-- | test/init.js | 26 |
4 files changed, 162 insertions, 147 deletions
diff --git a/lib/book.js b/lib/book.js index 514a719..fa72618 100644 --- a/lib/book.js +++ b/lib/book.js @@ -812,78 +812,4 @@ Book.prototype.normError = function(err, opts, defs) { return err; }; -// Init and return a book -Book.init = function(root, opts) { - var book = new Book(root, opts); - var extensionToUse = ".md"; - - var chaptersPaths = function(chapters) { - return _.reduce(chapters || [], function(accu, chapter) { - var o = { - title: chapter.title - }; - if (chapter.path) o.path = chapter.path; - - return accu.concat( - [o].concat(chaptersPaths(chapter.articles)) - ); - }, []); - }; - - book.log.info.ln("init book at", root); - return fs.mkdirp(root) - .then(function() { - book.log.info.ln("detect structure from SUMMARY (if it exists)"); - return book.parseSummary(); - }) - .fail(function() { - return Q(); - }) - .then(function() { - var summary = book.summaryFile || "SUMMARY.md"; - var chapters = book.summary.chapters || []; - extensionToUse = path.extname(summary); - - if (chapters.length === 0) { - chapters = [ - { - title: "Summary", - path: "SUMMARY"+extensionToUse - }, - { - title: "Introduction", - path: "README"+extensionToUse - } - ]; - } - - return Q(chaptersPaths(chapters)); - }) - .then(function(chapters) { - // Create files that don"t exist - return Q.all(_.map(chapters, function(chapter) { - if (!chapter.path) return Q(); - var absolutePath = path.resolve(book.root, chapter.path); - - return fs.exists(absolutePath) - .then(function(exists) { - if(exists) { - book.log.info.ln("found", chapter.path); - return; - } else { - book.log.info.ln("create", chapter.path); - } - - return fs.mkdirp(path.dirname(absolutePath)) - .then(function() { - return fs.writeFile(absolutePath, "# "+chapter.title+"\n"); - }); - }); - })); - }) - .then(function() { - book.log.info.ln("initialization is finished"); - }); -}; - module.exports= Book; 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')); }); } } diff --git a/lib/init.js b/lib/init.js new file mode 100644 index 0000000..2fc8016 --- /dev/null +++ b/lib/init.js @@ -0,0 +1,83 @@ +var _ = require('lodash'); +var Q = require('q'); +var path = require('path'); + +var Book = require('./book'); +var fs = require('./utils/fs'); + +// Initialize folder structure for a book +// Read SUMMARY to created the right chapter +function initBook(root, opts) { + var book = new Book(root, opts); + var extensionToUse = '.md'; + + var chaptersPaths = function(chapters) { + return _.reduce(chapters || [], function(accu, chapter) { + var o = { + title: chapter.title + }; + if (chapter.path) o.path = chapter.path; + + return accu.concat( + [o].concat(chaptersPaths(chapter.articles)) + ); + }, []); + }; + + book.log.info.ln('init book at', root); + return fs.mkdirp(root) + .then(function() { + book.log.info.ln('detect structure from SUMMARY (if it exists)'); + return book.parseSummary(); + }) + .fail(function() { + return Q(); + }) + .then(function() { + var summary = book.summaryFile || 'SUMMARY.md'; + var chapters = book.summary.chapters || []; + extensionToUse = path.extname(summary); + + if (chapters.length === 0) { + chapters = [ + { + title: 'Summary', + path: 'SUMMARY'+extensionToUse + }, + { + title: 'Introduction', + path: 'README'+extensionToUse + } + ]; + } + + return Q(chaptersPaths(chapters)); + }) + .then(function(chapters) { + // Create files that don't exist + return Q.all(_.map(chapters, function(chapter) { + if (!chapter.path) return Q(); + var absolutePath = path.resolve(book.root, chapter.path); + + return fs.exists(absolutePath) + .then(function(exists) { + if(exists) { + book.log.info.ln('found', chapter.path); + return; + } else { + book.log.info.ln('create', chapter.path); + } + + return fs.mkdirp(path.dirname(absolutePath)) + .then(function() { + return fs.writeFile(absolutePath, '# '+chapter.title+'\n'); + }); + }); + })); + }) + .then(function() { + book.log.info.ln('initialization is finished'); + }); +} + +module.exports = initBook; diff --git a/test/init.js b/test/init.js index 7e5ffee..625d77c 100644 --- a/test/init.js +++ b/test/init.js @@ -1,24 +1,24 @@ -var fs = require("fs"); -var path = require("path"); -var should = require("should"); +var fs = require('fs'); +var path = require('path'); +var should = require('should'); -var Book = require("../").Book; -var LOG_LEVELS = require("../").LOG_LEVELS; +var Book = require('../').Book; +var LOG_LEVELS = require('../').LOG_LEVELS; -describe("Init Books", function () { +describe('Init Books', function () { var initRoot; before(function() { - initRoot = path.resolve(__dirname, "books/init"); + initRoot = path.resolve(__dirname, 'books/init'); return Book.init(initRoot, { - logLevel: LOG_LEVELS.DISABLED, + logLevel: LOG_LEVELS.DISABLED }); }); - it("should create all chapters", function() { - should(fs.existsSync(path.resolve(initRoot, "hello.md"))).be.ok(); - should(fs.existsSync(path.resolve(initRoot, "hello2.md"))).be.ok(); - should(fs.existsSync(path.resolve(initRoot, "hello3/hello4.md"))).be.ok(); - should(fs.existsSync(path.resolve(initRoot, "hello3/hello5/hello6.md"))).be.ok(); + it('should create all chapters', function() { + should(fs.existsSync(path.resolve(initRoot, 'hello.md'))).be.ok(); + should(fs.existsSync(path.resolve(initRoot, 'hello2.md'))).be.ok(); + should(fs.existsSync(path.resolve(initRoot, 'hello3/hello4.md'))).be.ok(); + should(fs.existsSync(path.resolve(initRoot, 'hello3/hello5/hello6.md'))).be.ok(); }); }); |