lib/config/config.js
function loadConfigFile(configPath) {
	logger.debug('Loading configuration file at:', configPath);

	function loadConfig() {
		return fs.readFileSync(configPath, { encoding: 'utf8' });
	}

	function parseConfig(content, extension) {
		switch (extension) {
		case '.js':
			return require(configPath);

		case '.yaml':
			return jsYaml.safeLoad(content, { filename: configPath });

		case '.json':
			try {
				return JSON.parse(content);
			} catch (error) {
				// Try to provide more specific syntax errors
				lintJson(content);

				// If no syntax errors are found, throw a generic error
				throw new Error('There was a problem loading a configuration file: ' + configPath);
			}

		case '.json5':
			return JSON5.parse(content);

		default:
			throw new Error(`Configuration file is an unsupported type [${supportedTypes.join(', ')}]: ` + configPath);
		}
	}

	var extension = path.extname(configPath);
	var content;

	try {
		content = loadConfig();
	} catch (error) {
		throw new ConfigurationError('Failed to read configuration file', configPath, error);
	}

	try {
		return parseConfig(content, extension);
	} catch (error) {
		throw new ConfigurationError('Failed to parse configuration file', configPath, error);
	}
}
function loadConfigFile(configPath) {
	logger.debug('Loading configuration file at:', configPath);

	function loadConfig() {
		return fs.readFileSync(configPath, { encoding: 'utf8' });
	}

	function parseConfig(content, extension) {
		switch (extension) {
		case '.js':
			return require(configPath);

		case '.yaml':
			return jsYaml.safeLoad(content, { filename: configPath });

		case '.json':
			try {
				return JSON.parse(content);
			} catch (error) {
				// Try to provide more specific syntax errors
				lintJson(content);

				// If no syntax errors are found, throw a generic error
				throw new Error('There was a problem loading a configuration file: ' + configPath);
			}

		case '.json5':
			return JSON5.parse(content);

		default:
			throw new Error(`Configuration file is an unsupported type [${supportedTypes.join(', ')}]: ` + configPath);
		}
	}

	var extension = path.extname(configPath);
	var content;

	try {
		content = loadConfig();
	} catch (error) {
		throw new ConfigurationError('Failed to read configuration file', configPath, error);
	}

	try {
		return parseConfig(content, extension);
	} catch (error) {
		throw new ConfigurationError('Failed to parse configuration file', configPath, error);
	}
}
function loadConfigFile ( configPath ) { logger . debug ( 'Loading configuration file at:' , configPath ) ; function loadConfig ( ) { return fs . readFileSync ( configPath , { encoding : 'utf8' } ) ; } function parseConfig ( content , extension ) { switch ( extension ) { case '.js' : return require ( configPath ) ; case '.yaml' : return jsYaml . safeLoad ( content , { filename : configPath } ) ; case '.json' : try { return JSON . parse ( content ) ; } catch ( error ) { // Try to provide more specific syntax errors lintJson ( content ) ; // If no syntax errors are found, throw a generic error throw new Error ( 'There was a problem loading a configuration file: ' + configPath ) ; } case '.json5' : return JSON5 . parse ( content ) ; default : throw new Error ( ` ${ supportedTypes . join ( ', ' ) } ` + configPath ) ; } } var extension = path . extname ( configPath ) ; var content ; try { content = loadConfig ( ) ; } catch ( error ) { throw new ConfigurationError ( 'Failed to read configuration file' , configPath , error ) ; } try { return parseConfig ( content , extension ) ; } catch ( error ) { throw new ConfigurationError ( 'Failed to parse configuration file' , configPath , error ) ; } }
Attempt to load a config file, and provide some helpful output if the file cannot be parsed.

@param  {String} configPath A filesystem path to the configuration file.
@return {Object}            The loaded configuration object.
Attempt to load a config file and provide some helpful output if the file cannot be parsed .