UNPKG

2.54 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _fs = _interopRequireDefault(require("fs"));
7
8var _path = _interopRequireDefault(require("path"));
9
10var _findup = _interopRequireDefault(require("findup"));
11
12var _isString = _interopRequireDefault(require("lodash/isString"));
13
14var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
15
16var _config = _interopRequireDefault(require("./schemas/config"));
17
18var _error = _interopRequireDefault(require("./utils/error"));
19
20var _sanitizeConfig = _interopRequireDefault(require("./utils/sanitizeConfig"));
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24const CONFIG_FILENAME = 'styleguide.config.js';
25/**
26 * Try to find config file up the file tree.
27 *
28 * @return {string|boolean} Config absolute file path.
29 */
30
31function findConfigFile() {
32 let configDir;
33
34 try {
35 configDir = _findup.default.sync(process.cwd(), CONFIG_FILENAME);
36 } catch (exception) {
37 return false;
38 }
39
40 return _path.default.join(configDir, CONFIG_FILENAME);
41}
42/**
43 * Read, parse and validate config file or passed config.
44 *
45 * @param {object|string} [config] All config options or config file name or nothing.
46 * @param {function} [update] Change config object before running validation on it.
47 * @returns {object}
48 */
49
50
51function getConfig(config, update) {
52 let configFilepath = false;
53
54 if ((0, _isString.default)(config)) {
55 // Load config from a given file
56 configFilepath = _path.default.resolve(process.cwd(), config);
57
58 if (!_fs.default.existsSync(configFilepath)) {
59 throw new _error.default('Styleguidist config not found: ' + configFilepath + '.');
60 }
61
62 config = {};
63 } else if (!(0, _isPlainObject.default)(config)) {
64 // Try to read config options from a file
65 configFilepath = findConfigFile();
66 config = {};
67 }
68
69 if (configFilepath) {
70 config = require(configFilepath);
71 }
72
73 if (!config || (0, _isString.default)(config)) {
74 return {};
75 }
76
77 if (update) {
78 config = update(config);
79 }
80
81 const configDir = configFilepath ? _path.default.dirname(configFilepath) : process.cwd();
82
83 try {
84 return (0, _sanitizeConfig.default)(config, _config.default, configDir);
85 } catch (exception) {
86 if (exception instanceof _error.default) {
87 throw new _error.default(`Something is wrong with your style guide config\n\n${exception.message}`, exception.extra);
88 } else {
89 throw exception;
90 }
91 }
92}
93
94var _default = getConfig;
95exports.default = _default;
\No newline at end of file