roc
Version:
Build modern web applications easily
98 lines (79 loc) • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getApplicationConfig = getApplicationConfig;
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _helpers = require('../helpers');
var _style = require('../helpers/style');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* Make sure that we only print some feedback once */
let 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 relative paths to. By default will use the
* current working directory.
* @param {boolean} [verbose=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) {
let directory = arguments.length <= 1 || arguments[1] === undefined ? process.cwd() : arguments[1];
let verbose = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];
if (applicationConfigPath === false) {
return {};
}
const configPath = (0, _helpers.getAbsolutePath)(process.env.ROC_CONFIG_PATH || applicationConfigPath, directory);
if (onceApp && applicationConfigPath && process.env.ROC_CONFIG_PATH) {
onceApp = false;
console.log((0, _style.feedbackMessage)((0, _style.warningLabel)('Warning', 'Configuration'), '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));
}
try {
if (configPath) {
const stats = _fs2.default.statSync(configPath);
if (!stats.isFile()) {
throw new Error('Not a file.');
}
}
} catch (err) {
manageUnaccessibleFile(configPath);
}
// Return correct project configuration with fallback to empty object
const appConfigPath = configPath || (0, _helpers.getAbsolutePath)('roc.config.js', directory);
try {
const config = require(appConfigPath);
if (Object.keys(config).length === 0) {
console.log((0, _style.feedbackMessage)((0, _style.warningLabel)('Warning', 'Configuration'), 'The configuration file at ' + _chalk2.default.bold(appConfigPath) + ' was empty.'));
}
return config;
} catch (err) {
if (err.constructor === SyntaxError) {
console.log((0, _style.feedbackMessage)((0, _style.warningLabel)('Warning', 'Configuration'), 'Something is wrong with the configuration file at ' + _chalk2.default.bold(appConfigPath) + ' and it will be ignored. Received: ' + _chalk2.default.underline(err.message)));
} else if (verbose) {
console.log((0, _style.feedbackMessage)((0, _style.warningLabel)('Warning', 'Configuration'), `Could not find the configuration file at ${ _chalk2.default.bold(appConfigPath) }.`));
}
return {};
}
}
function manageUnaccessibleFile(configPath) {
console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Configuration'), `Configuration path points to unaccessible file: ${ configPath }`));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
//# sourceMappingURL=helpers.js.map