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 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;
logger.error('A GitHub token must be configured');
program.outputHelp(); // Save default templates
process.exit(1); config.defaultTemplates = defaultConfig.templates;
}
// We need at least one repository defined // Check for token
if (!config.repositories || config.repositories.length === 0) { if (typeof config.token === 'undefined') {
logger.error('At least one repository must be configured'); logger.error('A GitHub token must be configured');
program.outputHelp(); program.outputHelp();
process.exit(1); process.exit(1);
}
// Convert any repository strings to objects
config.repositories.forEach((repo, index) => {
if (typeof repo === 'string') {
config.repositories[index] = { repository: repo };
} }
}); // We need at least one repository defined
// Add 'package.json' if missing if (!config.repositories || config.repositories.length === 0) {
config.repositories.forEach((repo, index) => { logger.error('At least one repository must be configured');
if (!repo.packageFiles || !repo.packageFiles.length) { program.outputHelp();
config.repositories[index].packageFiles = ['package.json']; process.exit(1);
} }
}); // Convert any repository strings to objects
// Expand packageFile format config.repositories.forEach((repo, index) => {
config.repositories.forEach((repo, index) => { if (typeof repo === 'string') {
config.repositories[index].packageFiles = repo.packageFiles.map((packageFile) => { config.repositories[index] = { repository: repo };
if (typeof packageFile === 'string') {
return { fileName: packageFile };
} }
return packageFile;
}); });
}); // Add 'package.json' if missing
// Print config config.repositories.forEach((repo, index) => {
logger.verbose(`config=${JSON.stringify(config)}`); if (!repo.packageFiles || !repo.packageFiles.length) {
config.repositories[index].packageFiles = ['package.json'];
}
});
// 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)}`);
}
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,
}; };

View file

@ -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();