UNPKG

roc

Version:

Build modern web applications easily

89 lines (70 loc) 3.39 kB
'use strict'; exports.__esModule = true; exports.getApplicationConfig = getApplicationConfig; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } require('source-map-support/register'); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _helpers = require('../helpers'); var _helpersStyle = require('../helpers/style'); /* Make sure that we only print some feedback once */ var onceApp = true; /** * Gets the application configuration by reading a file. * * Will give a warning if ROC_CONFIG_PATH has been set since that will then be used as the path to get the configuration * file, even if one is provided to the function. * * Reads configuration files in this manner: * 1. Environment variable ROC_CONFIG_PATH. * 2. Path given as applicationConfigPath. * 3. Default by trying to read "roc.config.js" in the current working directory. * 4. Return a empty object along with a warning. * * @param {string} applicationConfigPath - Path to application configuration file. Can be either relative or absolute. * @param {string} [directory=process.cwd()] - The directory to resolve realative paths to. By default will use the * current working directory. * @param {boolean} [debug=false] - If extra information should be printed. * * @returns {object} - The application configuration object. * @throws {Error} - When an invalid path override is specified. */ function getApplicationConfig(applicationConfigPath) { var directory = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1]; var debug = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; var configPath = _helpers.getAbsolutePath(process.env.ROC_CONFIG_PATH || applicationConfigPath, directory); if (onceApp && applicationConfigPath && process.env.ROC_CONFIG_PATH) { onceApp = false; console.log(_helpersStyle.error('You have configured a location for the application configuration file but the ' + 'environment variable ' + _chalk2['default'].bold('ROC_CONFIG_PATH') + ' is set and that will be used instead. The ' + 'path that will be used is ' + configPath), '\n'); } try { if (configPath) { var stats = _fs2['default'].statSync(configPath); if (!stats.isFile()) { throw new Error('Not a file.'); } } } catch (err) { throwUnaccessableFile(configPath); } // Return correct project configuration with fallback to empty object var appConfigPath = configPath || _helpers.getAbsolutePath('roc.config.js', directory); try { var config = require(appConfigPath); if (Object.keys(config).length === 0) { console.log(_helpersStyle.warning('The configuration file at ' + _chalk2['default'].bold(appConfigPath) + ' was empty.')); } return config; } catch (error) { if (debug) { console.log(_helpersStyle.warning('Could not read the configuration file at ' + _chalk2['default'].bold(appConfigPath))); } return {}; } } function throwUnaccessableFile(configPath) { throw new Error('Configuration path points to unaccessable file: ' + configPath); } //# sourceMappingURL=helpers.js.map