Add .gitbook to locate book inside repository

This commit is contained in:
Samy Pessé
2016-02-23 15:05:32 +01:00
parent 13e2ff8035
commit f052aacee1
7 changed files with 96 additions and 38 deletions
+1
View File
@@ -0,0 +1 @@
./docs
+26
View File
@@ -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;
+7 -6
View File
@@ -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,
+10 -11
View File
@@ -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
});
})
.then(function(book) {
return book.parse()
.then(function() {
var Out = helper.FORMATS[kwargs.format];
var output = new Out(book);
return book.parse()
.then(function() {
var Out = helper.FORMATS[kwargs.format];
var output = new Out(book);
return output.generate()
.thenResolve(output);
return output.generate()
.thenResolve(output);
});
});
})
+1
View File
@@ -4,6 +4,7 @@ require('./location');
require('./paths');
// Parsing
require('./locate');
require('./config');
require('./readme');
require('./summary');
+28
View File
@@ -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'));
});
});
});
+23 -21
View File
@@ -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];
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');
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);
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,