mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-11 14:36:25 +00:00
Refactor config parsing to pass tests
This commit is contained in:
parent
3133513aa8
commit
5fc82077b7
2 changed files with 55 additions and 42 deletions
|
@ -1,60 +1,69 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const program = require('commander');
|
const program = require('commander');
|
||||||
|
|
||||||
logger.debug('Generating config');
|
let config = null;
|
||||||
|
|
||||||
// Get configs
|
function parseConfigs() {
|
||||||
const defaultConfig = require('./default');
|
logger.debug('Parsing configs');
|
||||||
const fileConfig = require('./file');
|
|
||||||
const cliConfig = require('./cli');
|
|
||||||
const envConfig = require('./env');
|
|
||||||
|
|
||||||
// Get global config
|
// Get configs
|
||||||
const config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
|
/* eslint-disable global-require */
|
||||||
|
const defaultConfig = require('./default');
|
||||||
|
const fileConfig = require('./file');
|
||||||
|
const cliConfig = require('./cli');
|
||||||
|
const envConfig = require('./env');
|
||||||
|
/* eslint-enable global-require */
|
||||||
|
|
||||||
// Set log level
|
// Get global config
|
||||||
logger.level = config.logLevel;
|
config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
|
||||||
|
|
||||||
// Check for token
|
// Set log level
|
||||||
if (typeof config.token === 'undefined') {
|
logger.level = config.logLevel;
|
||||||
|
|
||||||
|
// Save default templates
|
||||||
|
config.defaultTemplates = defaultConfig.templates;
|
||||||
|
|
||||||
|
// Check for token
|
||||||
|
if (typeof config.token === 'undefined') {
|
||||||
logger.error('A GitHub token must be configured');
|
logger.error('A GitHub token must be configured');
|
||||||
program.outputHelp();
|
program.outputHelp();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
// We need at least one repository defined
|
// We need at least one repository defined
|
||||||
if (!config.repositories || config.repositories.length === 0) {
|
if (!config.repositories || config.repositories.length === 0) {
|
||||||
logger.error('At least one repository must be configured');
|
logger.error('At least one repository must be configured');
|
||||||
program.outputHelp();
|
program.outputHelp();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
// Convert any repository strings to objects
|
// Convert any repository strings to objects
|
||||||
config.repositories.forEach((repo, index) => {
|
config.repositories.forEach((repo, index) => {
|
||||||
if (typeof repo === 'string') {
|
if (typeof repo === 'string') {
|
||||||
config.repositories[index] = { repository: repo };
|
config.repositories[index] = { repository: repo };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Add 'package.json' if missing
|
// Add 'package.json' if missing
|
||||||
config.repositories.forEach((repo, index) => {
|
config.repositories.forEach((repo, index) => {
|
||||||
if (!repo.packageFiles || !repo.packageFiles.length) {
|
if (!repo.packageFiles || !repo.packageFiles.length) {
|
||||||
config.repositories[index].packageFiles = ['package.json'];
|
config.repositories[index].packageFiles = ['package.json'];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Expand packageFile format
|
// Expand packageFile format
|
||||||
config.repositories.forEach((repo, index) => {
|
config.repositories.forEach((repo, index) => {
|
||||||
config.repositories[index].packageFiles = repo.packageFiles.map((packageFile) => {
|
config.repositories[index].packageFiles = repo.packageFiles.map((packageFile) => {
|
||||||
if (typeof packageFile === 'string') {
|
if (typeof packageFile === 'string') {
|
||||||
return { fileName: packageFile };
|
return { fileName: packageFile };
|
||||||
}
|
}
|
||||||
return packageFile;
|
return packageFile;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// Print config
|
// Print config
|
||||||
logger.verbose(`config=${JSON.stringify(config)}`);
|
logger.verbose(`config=${JSON.stringify(config)}`);
|
||||||
|
}
|
||||||
|
|
||||||
function getCascadedConfig(repo, packageFile) {
|
function getCascadedConfig(repo, packageFile) {
|
||||||
const cascadedConfig = Object.assign({}, config, repo, packageFile);
|
const cascadedConfig = Object.assign({}, config, repo, packageFile);
|
||||||
// Fill in any missing templates with defaults
|
// Fill in any missing templates with defaults
|
||||||
cascadedConfig.templates = Object.assign({}, defaultConfig.templates, cascadedConfig.templates);
|
cascadedConfig.templates = Object.assign({}, config.defaultTemplates, cascadedConfig.templates);
|
||||||
// Remove unnecessary fields
|
// Remove unnecessary fields
|
||||||
delete cascadedConfig.repositories;
|
delete cascadedConfig.repositories;
|
||||||
delete cascadedConfig.repository;
|
delete cascadedConfig.repository;
|
||||||
|
@ -69,4 +78,5 @@ function getGlobalConfig() {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getCascadedConfig,
|
getCascadedConfig,
|
||||||
getGlobalConfig,
|
getGlobalConfig,
|
||||||
|
parseConfigs,
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,6 +9,9 @@ module.exports = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
|
// Parse config
|
||||||
|
config.parseConfigs();
|
||||||
|
|
||||||
// Initialize our promise chain
|
// Initialize our promise chain
|
||||||
let p = Promise.resolve();
|
let p = Promise.resolve();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue