summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-23 15:05:32 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-23 15:05:32 +0100
commitf052aacee1140f211ebb9454157fa7a3604005cf (patch)
treef784d8b9fd70bd94b754e13d26e6f6dd548b9908
parent13e2ff8035d9a93e4ee8eb79ec71ac92e6f2bf7e (diff)
downloadgitbook-f052aacee1140f211ebb9454157fa7a3604005cf.zip
gitbook-f052aacee1140f211ebb9454157fa7a3604005cf.tar.gz
gitbook-f052aacee1140f211ebb9454157fa7a3604005cf.tar.bz2
Add .gitbook to locate book inside repository
-rw-r--r--.gitbook1
-rw-r--r--lib/book.js26
-rw-r--r--lib/cli/helper.js13
-rw-r--r--lib/cli/index.js23
-rw-r--r--test/all.js1
-rw-r--r--test/locate.js28
-rw-r--r--test/mock.js48
7 files changed, 99 insertions, 41 deletions
diff --git a/.gitbook b/.gitbook
new file mode 100644
index 0000000..ffcee85
--- /dev/null
+++ b/.gitbook
@@ -0,0 +1 @@
+./docs \ No newline at end of file
diff --git a/lib/book.js b/lib/book.js
index c81d255..86b1d67 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -328,4 +328,30 @@ Book.prototype.isInLanguageBook = function(filename) {
});
};
+// Locate a book in a folder
+// - Read the ".gitbook" is exists
+// - Try the folder itself
+// - Try a "docs" folder
+Book.locate = function(fs, root) {
+ return fs.readAsString(path.join(root, '.gitbook'))
+ .then(function(content) {
+ return path.join(root, content);
+ }, function() {
+ // .gitbook doesn't exists, fall back to the root folder
+ return Promise(root);
+ });
+};
+
+// Locate and setup a book
+Book.setup = function(fs, root, opts) {
+ return Book.locate(fs, root)
+ .then(function(_root) {
+ return new Book(_.extend(opts || {}, {
+ root: _root,
+ fs: fs
+ }));
+ });
+};
+
+
module.exports = Book;
diff --git a/lib/cli/helper.js b/lib/cli/helper.js
index 9510b49..e62c8d9 100644
--- a/lib/cli/helper.js
+++ b/lib/cli/helper.js
@@ -9,6 +9,8 @@ var JSONOutput = require('../output/json');
var WebsiteOutput = require('../output/website');
var EBookOutput = require('../output/ebook');
+var nodeFS = new NodeFS();
+
var LOG_OPTION = {
name: 'log',
description: 'Minimum log level to display',
@@ -39,14 +41,12 @@ var FORMATS = {
function bookCmd(fn) {
return function(args, kwargs) {
var input = path.resolve(args[0] || process.cwd());
- var book = new Book({
- fs: new NodeFS(),
- root: input,
-
+ return Book.setup(nodeFS, input, {
logLevel: kwargs.log
+ })
+ .then(function(book) {
+ return fn(book, args.slice(1), kwargs);
});
-
- return fn(book, args.slice(1), kwargs);
};
}
@@ -94,6 +94,7 @@ function ebookCmd(format) {
}
module.exports = {
+ nodeFS: nodeFS,
bookCmd: bookCmd,
outputCmd: outputCmd,
ebookCmd: ebookCmd,
diff --git a/lib/cli/index.js b/lib/cli/index.js
index 78c5286..0b2366a 100644
--- a/lib/cli/index.js
+++ b/lib/cli/index.js
@@ -114,22 +114,21 @@ module.exports = {
// Generate the book
.then(function() {
- var book = new Book({
- fs: new NodeFS(),
- root: input,
+ return Book.setup(helper.nodeFS, input, {
'config': {
'defaultsPlugins': ['livereload']
},
'logLevel': kwargs.log
- });
-
- return book.parse()
- .then(function() {
- var Out = helper.FORMATS[kwargs.format];
- var output = new Out(book);
-
- return output.generate()
- .thenResolve(output);
+ })
+ .then(function(book) {
+ return book.parse()
+ .then(function() {
+ var Out = helper.FORMATS[kwargs.format];
+ var output = new Out(book);
+
+ return output.generate()
+ .thenResolve(output);
+ });
});
})
diff --git a/test/all.js b/test/all.js
index 5a96e38..9c82c91 100644
--- a/test/all.js
+++ b/test/all.js
@@ -4,6 +4,7 @@ require('./location');
require('./paths');
// Parsing
+require('./locate');
require('./config');
require('./readme');
require('./summary');
diff --git a/test/locate.js b/test/locate.js
new file mode 100644
index 0000000..609f2da
--- /dev/null
+++ b/test/locate.js
@@ -0,0 +1,28 @@
+var path = require('path');
+
+var Book = require('../').Book;
+var mock = require('./mock');
+
+describe('Locate', function() {
+ it('should use root folder if no .gitbook', function() {
+ return mock.setupFS({
+ 'README.md': '# Hello'
+ })
+ .then(function(root) {
+ return Book.locate(mock.fs, root)
+ .should.be.fulfilledWith(root);
+ });
+ });
+
+ it('should use resolve using .gitbook', function() {
+ return mock.setupFS({
+ 'README.md': '# Hello',
+ '.gitbook': './docs'
+ })
+ .then(function(root) {
+ return Book.locate(mock.fs, root)
+ .should.be.fulfilledWith(path.resolve(root, 'docs'));
+ });
+ });
+
+});
diff --git a/test/mock.js b/test/mock.js
index b075f2c..1bb92e2 100644
--- a/test/mock.js
+++ b/test/mock.js
@@ -14,27 +14,30 @@ require('./assertions');
// Create filesystem instance for testing
var nodeFS = new NodeFS();
-function setupFS(_fs, rootFolder, files) {
- return _.chain(_.pairs(files))
- .sortBy(0)
- .reduce(function(prev, pair) {
- return prev.then(function() {
- var filename = path.resolve(rootFolder, pair[0]);
- var buf = pair[1];
-
- if (_.isObject(buf)) buf = JSON.stringify(buf);
- if (_.isString(buf)) buf = new Buffer(buf, 'utf-8');
-
- return fs.mkdirp(path.dirname(filename))
- .then(function() {
- return fs.writeFile(filename, buf);
+function setupFS(files) {
+ return Q.nfcall(tmp.dir.bind(tmp)).get(0)
+ .then(function(rootFolder) {
+ return _.chain(_.pairs(files))
+ .sortBy(0)
+ .reduce(function(prev, pair) {
+ return prev.then(function() {
+ var filename = path.resolve(rootFolder, pair[0]);
+ var buf = pair[1];
+
+ if (_.isObject(buf)) buf = JSON.stringify(buf);
+ if (_.isString(buf)) buf = new Buffer(buf, 'utf-8');
+
+ return fs.mkdirp(path.dirname(filename))
+ .then(function() {
+ return fs.writeFile(filename, buf);
+ });
});
+ }, Q())
+ .value()
+ .then(function() {
+ return rootFolder;
});
- }, Q())
- .value()
- .then(function() {
- return _fs;
- });
+ });
}
// Setup a mock book for testing using a map of files
@@ -42,14 +45,11 @@ function setupBook(files, opts) {
opts = opts || {};
opts.log = function() { };
- return Q.nfcall(tmp.dir.bind(tmp)).get(0)
+ return setupFS(files)
.then(function(folder) {
opts.fs = nodeFS;
opts.root = folder;
- return setupFS(nodeFS, folder, files);
- })
- .then(function() {
return new Book(opts);
});
}
@@ -90,6 +90,8 @@ function logError(err) {
}
module.exports = {
+ fs: nodeFS,
+ setupFS: setupFS,
setupBook: setupBook,
setupDefaultBook: setupDefaultBook,
outputDefaultBook: outputDefaultBook,