2017-01-14 13:39:26 +00:00
|
|
|
const logger = require('winston');
|
2017-01-18 13:14:07 +00:00
|
|
|
const stringify = require('json-stringify-pretty-compact');
|
2017-01-14 13:39:26 +00:00
|
|
|
|
2017-01-20 13:03:18 +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
|
|
|
|
2017-01-20 13:03:18 +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
|
2017-01-20 13:03:18 +00:00
|
|
|
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
|
|
|
|
2017-01-18 13:14:07 +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-18 00:48:17 +00:00
|
|
|
|
2017-01-15 15:12:56 +00:00
|
|
|
// Get global config
|
|
|
|
config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
|
2017-01-18 13:14:07 +00:00
|
|
|
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
|
2017-01-20 13:03:18 +00:00
|
|
|
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) {
|
2017-01-20 13:03:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2017-01-17 08:11:42 +00:00
|
|
|
// Set default packageFiles
|
2017-01-15 15:12:56 +00:00
|
|
|
config.repositories.forEach((repo, index) => {
|
|
|
|
if (!repo.packageFiles || !repo.packageFiles.length) {
|
2017-01-17 08:11:42 +00:00
|
|
|
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
|
2017-01-18 13:14:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-18 13:14:07 +00:00
|
|
|
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);
|
|
|
|
}
|