var fs = require('fs');
var path = require('path');
var findup = require('findup');
var minimist = require('minimist');
var prettyjson = require('prettyjson');
var _ = require('lodash');
var utils = require('./server');

var CONFIG_FILENAME = 'styleguide.config.js';
var DEFAULT_CONFIG = {
	rootDir: null,
	components: null,
	skipComponentsWithoutExample: false,
	title: 'Style guide',
	styleguideDir: 'styleguide',
	template: path.join(__dirname, '../templates/index.html'),
	serverHost: 'localhost',
	serverPort: 3000,
	highlightTheme: 'base16-light',
	verbose: false,
	getExampleFilename: function(componentpath) {
		return path.join(path.dirname(componentpath), 'Readme.md');
	},
	updateWebpackConfig: null
};

function readConfig() {
	var argv = minimist(process.argv.slice(2));
	var configFilepath = findConfig(argv);

	var options = require(configFilepath);

	validateConfig(options);

	var configDir = path.dirname(configFilepath);
	var rootDir = path.resolve(configDir, options.rootDir);

	if (rootDir === configDir) {
		throw Error('Styleguidist: "rootDir" should not point to a folder with the Styleguidist config and node_modules folder');
	}
	if (!utils.isDirectoryExists(rootDir)) {
		throw Error('Styleguidist: "rootDir" directory not found: ' + rootDir);
	}

	options = _.merge({}, DEFAULT_CONFIG, options);
	options = _.merge({}, options, {
		verbose: !!argv.verbose,
		rootDir: rootDir,
		styleguideDir: path.resolve(configDir, options.styleguideDir)
	});

	if (options.verbose) {
		console.log();
		console.log('Using config file:', configFilepath);
		console.log(prettyjson.render(options));
		console.log();
	}

	return options;
}

function findConfig(argv) {
	if (argv.config) {
		// Custom config location

		var configFilepath = path.join(process.cwd(), argv.config);
		if (!fs.existsSync(configFilepath)) {
			throw Error('Styleguidist config not found: ' + configFilepath + '.');
		}

		return configFilepath;
	}
	else {
		// Search config file in all parent directories

		var configDir;
		try {
			configDir = findup.sync(__dirname, CONFIG_FILENAME);
		}
		catch (e) {
			throw Error('Styleguidist config not found: ' + CONFIG_FILENAME + '.');
		}

		return path.join(configDir, CONFIG_FILENAME);
	}
}

function validateConfig(options) {
	if (!options.rootDir) {
		throw Error('Styleguidist: "rootDir" options is required.');
	}
	if (!options.components) {
		throw Error('Styleguidist: "components" options is required.');
	}
	if (options.getExampleFilename && typeof options.getExampleFilename !== 'function') {
		throw Error('Styleguidist: "getExampleFilename" options must be a function.');
	}
	if (options.updateWebpackConfig && typeof options.updateWebpackConfig !== 'function') {
		throw Error('Styleguidist: "updateWebpackConfig" options must be a function.');
	}
}

module.exports = readConfig();

@@ -2321,33 +2321,32 @@
 %22rootDir%22 option
-s
  is required.');
@@ -2338,32 +2338,32 @@
 is required.');%0A
+
 %09%7D%0A%09if (!options
@@ -2425,17 +2425,16 @@
 %22 option
-s
  is requ
@@ -2580,33 +2580,32 @@
 Filename%22 option
-s
  must be a funct
@@ -2696,32 +2696,32 @@
 = 'function') %7B%0A
+
 %09%09throw Error('S
@@ -2761,17 +2761,16 @@
 %22 option
-s
  must be

Fix typos.