Refactor config parsing to pass tests

This commit is contained in:
Rhys Arkins 2017-01-15 16:12:56 +01:00
parent 3133513aa8
commit 5fc82077b7
2 changed files with 55 additions and 42 deletions

View file

@ -1,60 +1,69 @@
const logger = require('winston');
const program = require('commander');
logger.debug('Generating config');
let config = null;
// Get configs
const defaultConfig = require('./default');
const fileConfig = require('./file');
const cliConfig = require('./cli');
const envConfig = require('./env');
function parseConfigs() {
logger.debug('Parsing configs');
// Get global config
const config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
// Get configs
/* 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
logger.level = config.logLevel;
// Get global config
config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
// Check for token
if (typeof config.token === 'undefined') {
// Set log level
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');
program.outputHelp();
process.exit(1);
}
// We need at least one repository defined
if (!config.repositories || config.repositories.length === 0) {
}
// We need at least one repository defined
if (!config.repositories || config.repositories.length === 0) {
logger.error('At least one repository must be configured');
program.outputHelp();
process.exit(1);
}
// Convert any repository strings to objects
config.repositories.forEach((repo, index) => {
}
// Convert any repository strings to objects
config.repositories.forEach((repo, index) => {
if (typeof repo === 'string') {
config.repositories[index] = { repository: repo };
}
});
// Add 'package.json' if missing
config.repositories.forEach((repo, index) => {
});
// Add 'package.json' if missing
config.repositories.forEach((repo, index) => {
if (!repo.packageFiles || !repo.packageFiles.length) {
config.repositories[index].packageFiles = ['package.json'];
}
});
// Expand packageFile format
config.repositories.forEach((repo, index) => {
});
// 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=${JSON.stringify(config)}`);
});
// Print config
logger.verbose(`config=${JSON.stringify(config)}`);
}
function getCascadedConfig(repo, packageFile) {
const cascadedConfig = Object.assign({}, config, repo, packageFile);
// 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
delete cascadedConfig.repositories;
delete cascadedConfig.repository;
@ -69,4 +78,5 @@ function getGlobalConfig() {
module.exports = {
getCascadedConfig,
getGlobalConfig,
parseConfigs,
};

View file

@ -9,6 +9,9 @@ module.exports = {
};
function start() {
// Parse config
config.parseConfigs();
// Initialize our promise chain
let p = Promise.resolve();