diff options
Diffstat (limited to 'lib/config/index.js')
-rw-r--r-- | lib/config/index.js | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/config/index.js b/lib/config/index.js index 267f650..7734392 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -1,9 +1,15 @@ -var Q = require('q'); var _ = require('lodash'); var semver = require('semver'); var gitbook = require('../gitbook'); var configDefault = require('./default'); +var Promise = require('../utils/promise'); + +// Config files to tested (sorted) +var CONFIG_FILES = [ + 'book.js', + 'book.json' +]; /* Config is an interface for the book's configuration stored in "book.json" (or "book.js") @@ -13,6 +19,7 @@ function Config(book, baseConfig) { this.book = book; this.fs = book.fs; this.log = book.log; + this.filename = null; this.replace(baseConfig || {}); } @@ -22,11 +29,17 @@ function Config(book, baseConfig) { Config.prototype.load = function() { var that = this; - this.log.debug.ln('loading configuration'); - return this.fs.loadAsObject(this.book.resolve('book')) - .fail(function(err) { - if (err.code != 'MODULE_NOT_FOUND') throw(err); - else return Q({}); + this.log.info.ln('loading configuration'); + + // Try all potential configuration file + return Promise.some(CONFIG_FILES, function(filename) { + that.log.debug.ln('try loading configuration from', filename); + + return that.fs.loadAsObject(that.book.resolve(filename)) + .fail(function(err) { + if (err.code != 'MODULE_NOT_FOUND') throw(err); + else return Promise(false); + }); }) .then(function(_config) { return that.replace(_config); @@ -86,6 +99,11 @@ Config.prototype.replace = function(options) { }); }; +// Return true if book has a configuration file +Config.prototype.exists = function() { + return Boolean(this.filename); +}; + // Return path to a structure file // Strip the extension by default Config.prototype.getStructure = function(name, dontStripExt) { |