summaryrefslogtreecommitdiffstats
path: root/lib/init.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-28 14:47:34 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-28 14:47:34 +0100
commit4aa66338fce505566a2fe6ba6aee79ddf3f656a2 (patch)
treeffce4b558735f6a41228542f8c36e3a6ba1e562d /lib/init.js
parent8cda844ef12cf87525024348c104413def02ed7f (diff)
downloadgitbook-4aa66338fce505566a2fe6ba6aee79ddf3f656a2.zip
gitbook-4aa66338fce505566a2fe6ba6aee79ddf3f656a2.tar.gz
gitbook-4aa66338fce505566a2fe6ba6aee79ddf3f656a2.tar.bz2
Add init command
Diffstat (limited to 'lib/init.js')
-rw-r--r--lib/init.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/init.js b/lib/init.js
new file mode 100644
index 0000000..ea75a77
--- /dev/null
+++ b/lib/init.js
@@ -0,0 +1,68 @@
+var _ = require('lodash');
+var path = require('path');
+
+var fs = require('./utils/fs');
+var Promise = require('./utils/promise');
+
+// Initialize folder structure for a book
+// Read SUMMARY to created the right chapter
+function initBook(book) {
+ var extensionToUse = '.md';
+
+ 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();
+
+ // Use extension of summary
+ extensionToUse = path.extname(summary);
+
+ // Readme doesn't have a path
+ if (!articles[0].path) {
+ articles[0].path = 'README' + extensionToUse;
+ }
+
+ // Summary doesn't exists? create one
+ if (!book.summary.path) {
+ articles.push({
+ title: 'Summary',
+ path: 'SUMMARY'+extensionToUse
+ });
+ }
+
+ // Create files that don't exist
+ return Promise.serie(articles, function(article) {
+ if (!article.path) return;
+
+ var absolutePath = book.resolve(article.path);
+
+ 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);
+ }
+
+ return fs.mkdirp(path.dirname(absolutePath))
+ .then(function() {
+ return fs.writeFile(absolutePath, '# '+article.title+'\n\n');
+ });
+ });
+ });
+ })
+ .then(function() {
+ book.log.info.ln('initialization is finished');
+ });
+}
+
+module.exports = initBook;