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,20 +1,28 @@
const logger = require('winston'); const logger = require('winston');
const program = require('commander'); const program = require('commander');
logger.debug('Generating config'); let config = null;
function parseConfigs() {
logger.debug('Parsing configs');
// Get configs // Get configs
/* eslint-disable global-require */
const defaultConfig = require('./default'); const defaultConfig = require('./default');
const fileConfig = require('./file'); const fileConfig = require('./file');
const cliConfig = require('./cli'); const cliConfig = require('./cli');
const envConfig = require('./env'); const envConfig = require('./env');
/* eslint-enable global-require */
// Get global config // Get global config
const config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig); config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
// Set log level // Set log level
logger.level = config.logLevel; logger.level = config.logLevel;
// Save default templates
config.defaultTemplates = defaultConfig.templates;
// Check for token // Check for token
if (typeof config.token === 'undefined') { if (typeof config.token === 'undefined') {
logger.error('A GitHub token must be configured'); logger.error('A GitHub token must be configured');
@ -50,11 +58,12 @@ config.repositories.forEach((repo, index) => {
}); });
// 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,
}; };

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