renovate/lib/config/index.js

92 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-01-14 13:39:26 +00:00
const logger = require('winston');
const stringify = require('json-stringify-pretty-compact');
2017-01-14 13:39:26 +00:00
const defaultsParser = require('./defaults');
const fileParser = require('./file');
const cliParser = require('./cli');
const envParser = require('./env');
2017-01-15 15:12:56 +00:00
let config = null;
2017-01-14 13:39:26 +00:00
module.exports = {
parseConfigs,
getCascadedConfig,
getGlobalConfig,
};
function parseConfigs(env, argv) {
2017-01-15 15:12:56 +00:00
logger.debug('Parsing configs');
2017-01-14 13:39:26 +00:00
2017-01-15 15:12:56 +00:00
// Get configs
const defaultConfig = defaultsParser.getConfig();
const fileConfig = fileParser.getConfig(env);
const cliConfig = cliParser.getConfig(argv);
const envConfig = envParser.getConfig(env);
2017-01-14 13:39:26 +00:00
logger.debug(`Default config = ${redact(defaultConfig)}`);
logger.debug(`File config = ${redact(fileConfig)}`);
logger.debug(`CLI config: ${redact(cliConfig)}`);
logger.debug(`Env config: ${redact(envConfig)}`);
2017-01-15 15:12:56 +00:00
// Get global config
config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
logger.debug(`raw config=${redact(config)}`);
2017-01-14 13:39:26 +00:00
2017-01-15 15:12:56 +00:00
// Set log level
logger.level = config.logLevel;
// Check for token
if (config.token === null) {
throw new Error('A GitHub token must be configured');
2017-01-14 13:39:26 +00:00
}
2017-01-15 15:12:56 +00:00
// We need at least one repository defined
if (!config.repositories || config.repositories.length === 0) {
throw new Error('At least one repository must be configured');
2017-01-14 13:39:26 +00:00
}
2017-01-15 15:12:56 +00:00
// Convert any repository strings to objects
config.repositories.forEach((repo, index) => {
if (typeof repo === 'string') {
config.repositories[index] = { repository: repo };
2017-01-14 13:39:26 +00:00
}
});
// Set default packageFiles
2017-01-15 15:12:56 +00:00
config.repositories.forEach((repo, index) => {
if (!repo.packageFiles || !repo.packageFiles.length) {
config.repositories[index].packageFiles = config.packageFiles;
2017-01-15 15:12:56 +00:00
}
});
// Expand packageFile format
config.repositories.forEach((repo, index) => {
config.repositories[index].packageFiles = repo.packageFiles.map((packageFile) => {
if (typeof packageFile === 'string') {
return { fileName: packageFile };
}
return packageFile;
});
});
// Print config
logger.verbose(`config=${redact(config)}`);
2017-01-15 15:12:56 +00:00
}
2017-01-14 13:39:26 +00:00
function getCascadedConfig(repo, packageFile) {
const cascadedConfig = Object.assign({}, config, repo, packageFile);
// Remove unnecessary fields
delete cascadedConfig.repositories;
delete cascadedConfig.repository;
delete cascadedConfig.fileName;
return cascadedConfig;
}
function getGlobalConfig() {
return config;
}
function redact(inputConfig) {
const tokenConfig = {};
if (inputConfig.token) {
tokenConfig.token = `${inputConfig.token.substr(0, 4)}${new Array(inputConfig.token.length - 3).join('*')}`;
}
const redactedConfig = Object.assign({}, inputConfig, tokenConfig);
return stringify(redactedConfig);
}