diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-04-28 21:25:58 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-04-28 21:25:58 +0200 |
commit | 826f90505c1a70ad2a8cf3715bac6d5efaeba22c (patch) | |
tree | a35600d555ff0268f6eb2fc1bd9c44c06438eb27 /lib/init.js | |
parent | 7ae36f16c84238340dd6b39a75423f5e95f27bbc (diff) | |
download | gitbook-826f90505c1a70ad2a8cf3715bac6d5efaeba22c.zip gitbook-826f90505c1a70ad2a8cf3715bac6d5efaeba22c.tar.gz gitbook-826f90505c1a70ad2a8cf3715bac6d5efaeba22c.tar.bz2 |
Add command "init"
Diffstat (limited to 'lib/init.js')
-rw-r--r-- | lib/init.js | 105 |
1 files changed, 59 insertions, 46 deletions
diff --git a/lib/init.js b/lib/init.js index b7bb7f5..3e3cdca 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,66 +1,79 @@ var path = require('path'); +var createNodeFS = require('./fs/node'); var fs = require('./utils/fs'); var Promise = require('./utils/promise'); +var File = require('./models/file'); +var Readme = require('./models/readme'); +var Book = require('./models/book'); +var Parse = require('./parse'); -// Initialize folder structure for a book -// Read SUMMARY to created the right chapter -function initBook(book) { - var extensionToUse = '.md'; +/** + Initialize folder structure for a book + Read SUMMARY to created the right chapter - book.log.info.ln('init book at', book.root); - return fs.mkdirp(book.root) - .then(function() { - return book.config.load(); - }) - .then(function() { - book.log.info.ln('detect structure from SUMMARY (if it exists)'); - return book.summary.load(); - }) - .then(function() { - var summary = book.summary.path || 'SUMMARY.md'; - var articles = book.summary.flatten(); + @param {Book} + @param {String} + @return {Promise} +*/ +function initBook(rootFolder) { + var extension = '.md'; + + return fs.mkdirp(rootFolder) - // Use extension of summary - extensionToUse = path.extname(summary); + // Parse the summary and readme + .then(function() { + var fs = createNodeFS(rootFolder); + var book = Book.createForFS(fs); - // Readme doesn't have a path - if (!articles[0].path) { - articles[0].path = 'README' + extensionToUse; - } + return Parse.parseReadme(book) - // Summary doesn't exists? create one - if (!book.summary.path) { - articles.push({ - title: 'Summary', - path: 'SUMMARY'+extensionToUse - }); - } + // Setup default readme if doesn't found one + .fail(function() { + var readmeFile = File.createWithFilepath('README' + extension); + var readme = Readme.create(readmeFile); + return book.setReadme(readme); + }); + }) + .then(Parse.parseSummary) - // Create files that don't exist - return Promise.serie(articles, function(article) { - if (!article.path) return; + .then(function(book) { + var logger = book.getLogger(); + var summary = book.getSummary(); + var summaryFile = summary.getFile(); + var summaryFilename = summaryFile.getPath() || ('SUMMARY' + extension); - var absolutePath = book.resolve(article.path); + var articles = summary.getArticlesAsList(); - return fs.exists(absolutePath) - .then(function(exists) { - if(exists) { - book.log.info.ln('found', article.path); - return; - } else { - book.log.info.ln('create', article.path); - } + // Write pages + return Promise.forEach(articles, function(article) { + var filePath = path.join(rootFolder, article.getPath()); + if (!filePath) return; - return fs.mkdirp(path.dirname(absolutePath)) + return fs.assertFile(filePath, function() { + return fs.ensureFile(filePath) .then(function() { - return fs.writeFile(absolutePath, '# '+article.title+'\n\n'); + logger.info.ln('create', article.getPath()); + return fs.writeFile(filePath, '# ' + article.getTitle() + '\n\n'); }); }); + }) + + // Write summary + .then(function() { + var filePath = path.join(rootFolder, summaryFilename); + + return fs.ensureFile(filePath) + .then(function() { + logger.info.ln('create ' + path.basename(filePath)); + return fs.writeFile(filePath, summary.toText(extension)); + }); + }) + + // Log end + .then(function() { + logger.info.ln('initialization is finished'); }); - }) - .then(function() { - book.log.info.ln('initialization is finished'); }); } |