1 0
import * as fs from 'fs'
2 0
import * as path from 'path'
3 0
import findup from 'findup'
4 0
import isString from 'lodash/isString'
5 0
import isPlainObject from 'lodash/isPlainObject'
6 0
import StyleguidistError from 'react-styleguidist/lib/scripts/utils/error'
7 0
import sanitizeConfig from 'react-styleguidist/lib/scripts/utils/sanitizeConfig'
8 0
import schema from './schemas/config'
9
import { StyleguidistConfig, SanitizedStyleguidistConfig } from '../types/StyleGuide'
10

11 0
const CONFIG_FILENAME = 'styleguide.config.js'
12

13
/**
14
 * Read, parse and validate config file or passed config.
15
 *
16
 * @param {object|string} [config] All config options or config file name or nothing.
17
 * @param {function} [update] Change config object before running validation on it.
18
 * @returns {object}
19
 */
20 0
export default function getConfig(
21
	configParam: string | StyleguidistConfig | { serverPort?: string | number },
22 0
	update?: (conf: SanitizedStyleguidistConfig | {}) => SanitizedStyleguidistConfig
23
): Promise<SanitizedStyleguidistConfig> | SanitizedStyleguidistConfig {
24
	let configFilepath
25
	let config:
26
		| StyleguidistConfig
27
		| { serverPort?: string | number }
28
		| (() => Promise<StyleguidistConfig>)
29 1
	if (isString(configParam)) {
30
		// Load config from a given file
31 0
		configFilepath = path.resolve(process.cwd(), configParam)
32 1
		if (!fs.existsSync(configFilepath)) {
33 0
			throw new StyleguidistError('Styleguidist config not found: ' + configFilepath + '.')
34
		}
35 0
		config = {}
36 1
	} else if (!isPlainObject(configParam)) {
37
		// Try to read config options from a file
38 0
		configFilepath = findConfigFile()
39 0
		config = {}
40
	} else {
41 0
		config = configParam
42
	}
43

44 1
	if (typeof configFilepath === 'string') {
45 0
		config = require(configFilepath)
46
	}
47

48
	const configDir =
49 1
		typeof configFilepath === 'string' ? path.dirname(configFilepath) : process.cwd()
50

51 1
	if (typeof config === 'function') {
52 0
		return config().then(conf => postTreatConfig(configDir, conf, update))
53
	}
54

55 0
	return postTreatConfig(configDir, config, update)
56
}
57

58
function postTreatConfig(
59
	configDir: string,
60
	config: StyleguidistConfig | { serverPort?: string | number },
61 0
	update?: (conf: SanitizedStyleguidistConfig | {}) => SanitizedStyleguidistConfig
62
): SanitizedStyleguidistConfig {
63 1
	if (update) {
64 0
		config = update(config)
65
	}
66

67 1
	if (config.serverPort && isString(config.serverPort)) {
68 0
		config.serverPort = parseInt(config.serverPort)
69
	}
70

71 0
	try {
72 0
		return sanitizeConfig(config as StyleguidistConfig, schema as any, configDir) as any
73
	} catch (exception) {
74
		/* eslint-disable */
75 0
		console.log(exception instanceof StyleguidistError, exception.constructor.name)
76 0
		throw exception.message
77
	}
78
}
79

80
/**
81
 * Try to find config file up the file tree.
82
 *
83
 * @return {string|boolean} Config absolute file path.
84
 */
85 0
function findConfigFile() {
86
	let configDir
87 0
	try {
88 0
		configDir = findup.sync(process.cwd(), CONFIG_FILENAME)
89
	} catch (exception) {
90 0
		return false
91
	}
92

93 0
	return path.join(configDir, CONFIG_FILENAME)
94
}

Read our documentation on viewing source code .

Loading