Refactor config files

This commit is contained in:
Rhys Arkins 2017-01-14 14:39:26 +01:00
parent 7bcfc46879
commit 18788fed39
7 changed files with 167 additions and 147 deletions

54
app/config/cli.js Normal file
View file

@ -0,0 +1,54 @@
const logger = require('winston');
const program = require('commander');
const config = {};
program
.arguments('[repository] [fileName]')
.option('--dep-types <types>', 'List of dependency types')
.option('--force', 'Force creation of PRs')
.option('--ignore-deps <list>', 'List of dependencies to ignore')
.option('--labels <labels>', 'List of labels to apply')
.option('--log-level <level>', 'Log Level')
.option('--token <token>', 'GitHub Auth Token')
.on('--help', () => {
/* eslint-disable no-console */
console.log(' Examples:');
console.log('');
console.log(' $ renovate --token sp2jb5h7nsfjsg9s60v23b singapore/lint-condo');
console.log(' $ renovate --token sp2jb5h7nsfjsg9s60v23b singapore/lint-condo custom/location/package.json');
console.log('');
/* eslint-enable no-console */
})
.action((repository, fileName) => {
config.repositories = [
{
repository,
packageFiles: [fileName || 'package.json'],
},
];
})
.parse(process.argv);
if (program.depTypes) {
config.depTypes = program.depTypes.split(',');
}
if (program.force) {
config.force = true;
}
if (program.ignoreDeps) {
config.ignoreDeps = program.ignoreDeps.split(',');
}
if (program.labels) {
config.labels = program.labels.split(',');
}
if (program.logLevel) {
config.logLevel = program.logLevel;
}
if (program.token) {
config.token = program.token;
}
logger.debug(`CLI config: ${JSON.stringify(config)}`);
module.exports = config;

View file

@ -1,4 +1,6 @@
module.exports = {
const logger = require('winston');
const config = {
logLevel: 'info',
depTypes: ['dependencies', 'devDependencies', 'optionalDependencies'],
labels: [],
@ -17,3 +19,7 @@ ${params.changelog}`,
prTitlePin: params => `Pin dependency ${params.depName} to version ${params.newVersion}`,
},
};
logger.debug(`Default config = ${JSON.stringify(config)}`);
module.exports = config;

14
app/config/env.js Normal file
View file

@ -0,0 +1,14 @@
const logger = require('winston');
const config = {};
if (process.env.LOG_LEVEL) {
config.logLevel = process.env.LOG_LEVEL;
}
if (process.env.RENOVATE_TOKEN) {
config.token = process.env.RENOVATE_TOKEN;
}
logger.debug(`Env config: ${JSON.stringify(config)}`);
module.exports = config;

14
app/config/file.js Normal file
View file

@ -0,0 +1,14 @@
const logger = require('winston');
let config = {};
try {
// eslint-disable-next-line import/no-unresolved,global-require
config = require('../../config');
} catch (err) {
// Do nothing
logger.verbose('No custom config found');
}
logger.debug(`File config = ${JSON.stringify(config)}`);
module.exports = config;

72
app/config/index.js Normal file
View file

@ -0,0 +1,72 @@
const logger = require('winston');
const program = require('commander');
logger.debug('Generating config');
// Get configs
const defaultConfig = require('./default');
const fileConfig = require('./file');
const envConfig = require('./env');
const cliConfig = require('./cli');
// Get global config
const config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
// Set log level
logger.level = config.logLevel;
// 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) {
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) => {
if (typeof repo === 'string') {
config.repositories[index] = { repository: repo };
}
});
// 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) => {
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) {
const cascadedConfig = Object.assign({}, config, repo, packageFile);
// Fill in any missing templates with defaults
cascadedConfig.templates = Object.assign({}, defaultConfig.templates, cascadedConfig.templates);
// Remove unnecessary fields
delete cascadedConfig.repositories;
delete cascadedConfig.repository;
delete cascadedConfig.fileName;
return cascadedConfig;
}
function getGlobalConfig() {
return config;
}
module.exports = {
getCascadedConfig,
getGlobalConfig,
};

View file

@ -1,139 +0,0 @@
const logger = require('winston');
const program = require('commander');
module.exports = {
getGlobalConfig,
getCascadedConfig,
};
let config = null;
// This function reads in all configs and combines them
function getGlobalConfig() {
// eslint-disable-next-line global-require
const defaultConfig = require('./defaults');
// save default templates for cascading later
defaultConfig.defaultTemplates = defaultConfig.templates;
// Custom config file options - optional
let customConfig = {};
try {
// eslint-disable-next-line import/no-unresolved,global-require
customConfig = require('../../config');
} catch (err) {
// Do nothing
logger.verbose('No custom config found');
}
// Environment variables
const envConfig = {};
if (process.env.LOG_LEVEL) {
envConfig.logLevel = process.env.LOG_LEVEL;
}
if (process.env.RENOVATE_TOKEN) {
envConfig.token = process.env.RENOVATE_TOKEN;
}
// Parse any CLI commands
const cliConfig = {};
program
.arguments('[repository] [fileName]')
.option('--dep-types <types>', 'List of dependency types')
.option('--force', 'Force creation of PRs')
.option('--ignore-deps <list>', 'List of dependencies to ignore')
.option('--labels <labels>', 'List of labels to apply')
.option('--log-level <level>', 'Log Level')
.option('--token <token>', 'GitHub Auth Token')
.on('--help', () => {
/* eslint-disable no-console */
console.log(' Examples:');
console.log('');
console.log(' $ renovate --token sp2jb5h7nsfjsg9s60v23b singapore/lint-condo');
console.log(' $ renovate --token sp2jb5h7nsfjsg9s60v23b singapore/lint-condo custom/location/package.json');
console.log('');
/* eslint-enable no-console */
})
.action((repository, fileName) => {
cliConfig.repositories = [
{
repository,
packageFiles: [fileName || 'package.json'],
},
];
})
.parse(process.argv);
if (program.depTypes) {
cliConfig.depTypes = program.depTypes.split(',');
}
if (program.force) {
cliConfig.force = true;
}
if (program.ignoreDeps) {
cliConfig.ignoreDeps = program.ignoreDeps.split(',');
}
if (program.labels) {
cliConfig.labels = program.labels.split(',');
}
if (program.logLevel) {
cliConfig.logLevel = program.logLevel;
}
if (program.token) {
cliConfig.token = program.token;
}
// Get global config
config = Object.assign({}, defaultConfig, customConfig, envConfig, cliConfig);
// Set log level
logger.level = config.logLevel;
// token must be defined
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) {
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) => {
if (typeof repo === 'string') {
config.repositories[index] = { repository: repo };
}
});
// 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) => {
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)}`);
return config;
}
function getCascadedConfig(repo, packageFile) {
const cascadedConfig = Object.assign({}, config, repo, packageFile);
// Fill in any missing templates with defaults
cascadedConfig.templates = Object.assign({}, config.defaultTemplates, cascadedConfig.templates);
// Remove unnecessary fields
delete cascadedConfig.repositories;
delete cascadedConfig.repository;
delete cascadedConfig.fileName;
return cascadedConfig;
}

View file

@ -1,22 +1,21 @@
#!/usr/bin/env node
const logger = require('./logger');
const config = require('./config');
// Initialize config
const configParser = require('./config/parser');
// Require main source
const renovate = require('./renovate');
// Get global config
const config = configParser.getGlobalConfig();
// Initialize our promise chain
let p = Promise.resolve();
// Get global config
const globalConfig = config.getGlobalConfig();
// Queue up each repo/package combination
config.repositories.forEach((repo) => {
globalConfig.repositories.forEach((repo) => {
repo.packageFiles.forEach((packageFile) => {
const cascadedConfig = configParser.getCascadedConfig(repo, packageFile);
const cascadedConfig = config.getCascadedConfig(repo, packageFile);
p = p.then(() => renovate(repo.repository, packageFile.fileName, cascadedConfig));
});
});