refactor: move config globals inside functions (#1088)

This was necessary when attempting to use jest mock all
This commit is contained in:
Rhys Arkins 2017-11-03 07:43:26 +01:00 committed by GitHub
parent 9dd3fd968c
commit 1e5a5cab79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 15 deletions

View file

@ -1,12 +1,7 @@
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
const options = require('./definitions').getOptions(); const options = require('./definitions').getOptions();
const allowedStrings = []; let allowedStrings;
options.forEach(option => {
if (option.allowString) {
allowedStrings.push(option.name);
}
});
module.exports = { module.exports = {
massageConfig, massageConfig,
@ -14,6 +9,14 @@ module.exports = {
// Returns a massaged config // Returns a massaged config
function massageConfig(config) { function massageConfig(config) {
if (!allowedStrings) {
allowedStrings = [];
options.forEach(option => {
if (option.allowString) {
allowedStrings.push(option.name);
}
});
}
const massagedConfig = deepcopy(config); const massagedConfig = deepcopy(config);
for (const key of Object.keys(config)) { for (const key of Object.keys(config)) {
const val = config[key]; const val = config[key];

View file

@ -2,10 +2,7 @@ const later = require('later');
const deepcopy = require('deepcopy'); const deepcopy = require('deepcopy');
const options = require('./definitions').getOptions(); const options = require('./definitions').getOptions();
const optionTypes = {}; let optionTypes;
options.forEach(option => {
optionTypes[option.name] = option.type;
});
module.exports = { module.exports = {
migrateConfig, migrateConfig,
@ -27,6 +24,12 @@ const removedOptions = [
// Returns a migrated config // Returns a migrated config
function migrateConfig(config) { function migrateConfig(config) {
if (!optionTypes) {
optionTypes = {};
options.forEach(option => {
optionTypes[option.name] = option.type;
});
}
let isMigrated = false; let isMigrated = false;
const migratedConfig = deepcopy(config); const migratedConfig = deepcopy(config);
for (const key of Object.keys(config)) { for (const key of Object.keys(config)) {

View file

@ -161,7 +161,8 @@ async function getPreset(preset, logger) {
logger.warn(`Cannot find preset ${preset}`); logger.warn(`Cannot find preset ${preset}`);
return {}; return {};
} }
logger.debug({ presetConfig }, `Found preset ${preset}`); logger.debug(`Found preset ${preset}`);
logger.trace({ presetConfig });
if (params) { if (params) {
const argMapping = {}; const argMapping = {};
for (const [index, value] of params.entries()) { for (const [index, value] of params.entries()) {

View file

@ -1,16 +1,19 @@
const options = require('./definitions').getOptions(); const options = require('./definitions').getOptions();
const { hasValidSchedule } = require('../workers/branch/schedule'); const { hasValidSchedule } = require('../workers/branch/schedule');
const optionTypes = {}; let optionTypes;
options.forEach(option => {
optionTypes[option.name] = option.type;
});
module.exports = { module.exports = {
validateConfig, validateConfig,
}; };
function validateConfig(config) { function validateConfig(config) {
if (!optionTypes) {
optionTypes = {};
options.forEach(option => {
optionTypes[option.name] = option.type;
});
}
let errors = []; let errors = [];
let warnings = []; let warnings = [];