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 | |
parent | 7ae36f16c84238340dd6b39a75423f5e95f27bbc (diff) | |
download | gitbook-826f90505c1a70ad2a8cf3715bac6d5efaeba22c.zip gitbook-826f90505c1a70ad2a8cf3715bac6d5efaeba22c.tar.gz gitbook-826f90505c1a70ad2a8cf3715bac6d5efaeba22c.tar.bz2 |
Add command "init"
Diffstat (limited to 'lib')
-rw-r--r-- | lib/__tests__/gitbook.js | 2 | ||||
-rw-r--r-- | lib/__tests__/init.js | 16 | ||||
-rw-r--r-- | lib/api/encodeGlobal.js | 2 | ||||
-rw-r--r-- | lib/cli/index.js | 5 | ||||
-rw-r--r-- | lib/cli/init.js | 17 | ||||
-rw-r--r-- | lib/index.js | 7 | ||||
-rw-r--r-- | lib/init.js | 105 | ||||
-rw-r--r-- | lib/models/book.js | 10 | ||||
-rw-r--r-- | lib/models/file.js | 11 | ||||
-rw-r--r-- | lib/models/readme.js | 6 | ||||
-rw-r--r-- | lib/models/summary.js | 15 | ||||
-rw-r--r-- | lib/output/helper/writeFile.js | 2 | ||||
-rw-r--r-- | lib/output/website/onAsset.js | 2 | ||||
-rw-r--r-- | lib/utils/fs.js | 2 |
14 files changed, 143 insertions, 59 deletions
diff --git a/lib/__tests__/gitbook.js b/lib/__tests__/gitbook.js index b45fe70..c3669bb 100644 --- a/lib/__tests__/gitbook.js +++ b/lib/__tests__/gitbook.js @@ -1,5 +1,3 @@ -jest.autoMockOff(); - var gitbook = require('../gitbook'); describe('satisfies', function() { diff --git a/lib/__tests__/init.js b/lib/__tests__/init.js new file mode 100644 index 0000000..5665cf1 --- /dev/null +++ b/lib/__tests__/init.js @@ -0,0 +1,16 @@ +var tmp = require('tmp'); +var initBook = require('../init'); + +describe('initBook', function() { + + pit('should create a README and SUMMARY for empty book', function() { + var dir = tmp.dirSync(); + + return initBook(dir.name) + .then(function() { + expect(dir.name).toHaveFile('README.md'); + expect(dir.name).toHaveFile('SUMMARY.md'); + }); + }); + +}); diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js index fa33696..bbd693b 100644 --- a/lib/api/encodeGlobal.js +++ b/lib/api/encodeGlobal.js @@ -70,7 +70,7 @@ function encodeGlobal(output) { .then(function() { var filePath = PathUtils.resolveInRoot(outputFolder, fileName); - return fs.ensure(filePath) + return fs.ensureFile(filePath) .then(function() { return fs.writeFile(filePath, content); }); diff --git a/lib/cli/index.js b/lib/cli/index.js index b93b7b1..4afb090 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -1,9 +1,10 @@ module.exports = { commands: [ - require('./parse'), require('./build'), require('./serve'), - require('./install') + require('./install'), + require('./parse'), + require('./init') ] }; diff --git a/lib/cli/init.js b/lib/cli/init.js new file mode 100644 index 0000000..9a1bff8 --- /dev/null +++ b/lib/cli/init.js @@ -0,0 +1,17 @@ +var path = require('path'); + +var options = require('./options'); +var initBook = require('../init'); + +module.exports = { + name: 'install [book]', + description: 'setup and create files for chapters', + options: [ + options.log + ], + exec: function(args, kwargs) { + var bookRoot = path.resolve(process.cwd(), args[0] || './'); + + return initBook(bookRoot); + } +}; diff --git a/lib/index.js b/lib/index.js index ffaea25..0a76e08 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,8 +3,11 @@ var extend = require('extend'); var common = require('./browser'); var Output = require('./output'); var cli = require('./cli'); +var initBook = require('./initBook'); module.exports = extend({ - Output: Output, - commands: cli.commands + initBook: initBook, + createNodeFS: require('./fs/node'), + Output: Output, + commands: cli.commands }, common); 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'); }); } diff --git a/lib/models/book.js b/lib/models/book.js index be055d7..030223f 100644 --- a/lib/models/book.js +++ b/lib/models/book.js @@ -191,7 +191,6 @@ Book.prototype.addLanguageBook = function(language, book) { return this.set('books', books); }; - /** Set the summary for this book @@ -202,6 +201,15 @@ Book.prototype.setSummary = function(summary) { return this.set('summary', summary); }; +/** + Set the readme for this book + + @param {Readme} + @return {Book} +*/ +Book.prototype.setReadme = function(readme) { + return this.set('readme', readme); +}; /** Change log level diff --git a/lib/models/file.js b/lib/models/file.js index ff7b899..d1726a7 100644 --- a/lib/models/file.js +++ b/lib/models/file.js @@ -74,5 +74,16 @@ File.createFromStat = function createFromStat(filepath, stat) { }); }; +/** + Create a file with only a path + + @param {String} filepath + @return {File} +*/ +File.createWithFilepath = function createWithFilepath(filepath) { + return new File({ + path: filepath + }); +}; module.exports = File; diff --git a/lib/models/readme.js b/lib/models/readme.js index 4ad3992..c655c82 100644 --- a/lib/models/readme.js +++ b/lib/models/readme.js @@ -28,10 +28,12 @@ Readme.prototype.getDescription = function() { @return {Readme} */ Readme.create = function(file, def) { + def = def || {}; + return new Readme({ file: file, - title: def.title, - description: def.description + title: def.title || '', + description: def.description || '' }); }; diff --git a/lib/models/summary.js b/lib/models/summary.js index 3b46941..5baebef 100644 --- a/lib/models/summary.js +++ b/lib/models/summary.js @@ -151,6 +151,21 @@ Summary.prototype.toText = function(parser) { }; /** + Return all articles as a list + + @return {List<Article>} +*/ +Summary.prototype.getArticlesAsList = function() { + var accu = []; + + this.getArticle(function(article) { + accu.push(article); + }); + + return Immutable.List(accu); +}; + +/** Create a new summary for a list of parts @param {Lust|Array} parts diff --git a/lib/output/helper/writeFile.js b/lib/output/helper/writeFile.js index 3e2261d..a6d4645 100644 --- a/lib/output/helper/writeFile.js +++ b/lib/output/helper/writeFile.js @@ -13,7 +13,7 @@ function writeFile(output, filePath, content) { var rootFolder = output.getRoot(); filePath = path.join(rootFolder, filePath); - return fs.ensure(filePath) + return fs.ensureFile(filePath) .then(function() { return fs.writeFile(filePath, content); }) diff --git a/lib/output/website/onAsset.js b/lib/output/website/onAsset.js index 1311c95..17b6ba7 100644 --- a/lib/output/website/onAsset.js +++ b/lib/output/website/onAsset.js @@ -17,7 +17,7 @@ function onAsset(output, asset) { var filePath = path.resolve(rootFolder, asset); var outputPath = path.resolve(outputFolder, asset); - return fs.ensure(outputPath) + return fs.ensureFile(outputPath) .then(function() { return fs.copy(filePath, outputPath); }) diff --git a/lib/utils/fs.js b/lib/utils/fs.js index 008dd40..be3ec0e 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -130,6 +130,6 @@ module.exports = { tmpDir: genTmpDir, download: download, uniqueFilename: uniqueFilename, - ensure: ensureFile, + ensureFile: ensureFile, rmDir: rmDir }; |