53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const promise_1 = __importDefault(require("../utils/promise"));
|
|
const validateConfig_1 = __importDefault(require("./validateConfig"));
|
|
const configFiles_1 = __importDefault(require("../constants/configFiles"));
|
|
/**
|
|
Parse configuration from "book.json" or "book.js"
|
|
|
|
@param {Book} book
|
|
@return {Promise<Book>}
|
|
*/
|
|
function parseConfig(book) {
|
|
const fs = book.getFS();
|
|
let config = book.getConfig();
|
|
return promise_1.default.some(configFiles_1.default, (filename) => {
|
|
// Is this file ignored?
|
|
if (book.isFileIgnored(filename)) {
|
|
return;
|
|
}
|
|
// Try loading it
|
|
return fs
|
|
.loadAsObject(filename)
|
|
.then((cfg) => {
|
|
return fs.statFile(filename).then((file) => {
|
|
return {
|
|
file: file,
|
|
values: cfg
|
|
};
|
|
});
|
|
})
|
|
.fail((err) => {
|
|
if (err.code != "MODULE_NOT_FOUND")
|
|
throw err;
|
|
else
|
|
return (0, promise_1.default)(false);
|
|
});
|
|
}).then((result) => {
|
|
let values = result ? result.values : {};
|
|
values = (0, validateConfig_1.default)(values);
|
|
// Set the file
|
|
if (result && result.file) {
|
|
config = config.setFile(result.file);
|
|
}
|
|
// Merge with old values
|
|
config = config.mergeValues(values);
|
|
return book.setConfig(config);
|
|
});
|
|
}
|
|
exports.default = parseConfig;
|