Skip to content

Commit

Permalink
Fix bug where output in config files was not honoured
Browse files Browse the repository at this point in the history
...and make debugging where `settings`, `output`, and `plugins` come
from easier.
  • Loading branch information
wooorm committed Jun 12, 2016
1 parent 2da3a5f commit cf1b4d4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 29 deletions.
102 changes: 74 additions & 28 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,60 @@ var concat = Array.prototype.concat;
* @param {Object} target - Configuration to merge into.
* @param {Object} configuration - Configuration to merge
* from.
* @param {boolean} [recursive] - Used internally no ensure
* the plug-in key is only escaped at root-level.
* @param {string} [source] - Used internally no map
* changes to source files, and to ensure the plug-in
* key is only escaped at root-level.
* @return {Object} - `target`.
*/
function merge(target, configuration, recursive) {
function merge(target, configuration, source) {
var key;
var value;
var index;
var length;
var result;
var plugin;
var subvalue;

/**
* Log a message about setting a value.
*
* @param {string} message - Thing done.
* @param {string} key - Affected key.
* @param {*} [value] - Set value, if any.
*/
function log(message, key, value) {
if (!source) {
return;
}

if (value) {
debug(message + ' `%s` to `%j` (from `%s`)', key, value, source);
} else {
debug(message + ' `%s` (from `%s`)', key, source);
}
}

/**
* Check if `value` is an object with keys.
*
* @param {*} value - Value to check.
* @return {boolean} - Whether `value` is an object with keys.
*/
function filled(value) {
return value && typeof value === 'object' && Object.keys(value).length;
}

/*
* Walk `configuration`.
*/

for (key in configuration) {
value = configuration[key];
result = target[key];

if (key === PLUGIN_KEY && !recursive) {
if (key === PLUGIN_KEY && source) {
if (!result) {
target[key] = result = {};
target[PLUGIN_KEY] = result = {};
}

if ('length' in value) {
Expand All @@ -77,30 +112,44 @@ function merge(target, configuration, recursive) {
plugin = value[index];

if (!(plugin in result)) {
log('Configuring plug-in', plugin, {});
result[plugin] = {};
}
}
} else {
for (plugin in value) {
if (value[plugin] === false) {
subvalue = value[plugin];

if (subvalue === false) {
result[plugin] = false
log('Turning off plug-in', key);
} else if (!filled(result[plugin])) {
result[plugin] = merge({}, subvalue || {});
log('Configuring plug-in', plugin, result[plugin]);
} else if (!filled(subvalue)) {
log('Not reconfiguring plug-in', plugin);
} else {
result[plugin] = merge(
result[plugin] || {},
value[plugin] || {},
true
);
result[plugin] = merge(result[plugin], subvalue);
log('Reconfiguring plug-in', plugin, result[plugin]);
}
}
}
} else if (typeof value === 'object' && value !== null) {
} else if (value && typeof value === 'object') {
if ('length' in value) {
target[key] = concat.apply(value);
} else {
target[key] = merge(result || {}, value, true);
log('Setting', key, target[key]);
} else if (filled(value)) {
if (result) {
target[key] = merge(result, value);
log('Merging', key, target[key]);
} else {
target[key] = value;
log('Setting', key, value);
}
}
} else if (value !== undefined) {
target[key] = value;
log('Setting', key, target[key]);
}
}

Expand Down Expand Up @@ -137,10 +186,11 @@ function load(filePath) {
* Get personal configuration object from `~`.
* Loads `rcName` and `rcName.js`.
*
* @param {Object} config - Config to load into.
* @param {string?} rcName - Name of configuration file.
* @return {Object} - Parsed JSON.
*/
function getUserConfiguration(rcName) {
function loadUserConfiguration(config, rcName) {
var configuration = {};

/**
Expand All @@ -152,7 +202,7 @@ function getUserConfiguration(rcName) {
/* istanbul ignore next - not really testable
* as this loads files outside this project. */
if (exists(filePath)) {
merge(configuration, load(filePath));
merge(configuration, load(filePath), filePath);
}
}

Expand Down Expand Up @@ -228,13 +278,13 @@ function getLocalConfiguration(context, directory, callback) {

debug('Using ' + file.filePath());

merge(configuration, local);
merge(configuration, local, file.filePath());
}

if (!found) {
debug('Using personal configuration');

merge(configuration, getUserConfiguration(rcName));
loadUserConfiguration(configuration, rcName);
}

callback(err, configuration);
Expand Down Expand Up @@ -299,23 +349,20 @@ Configuration.prototype.getConfiguration = function (filePath, callback) {
*/
function handleLocalConfiguration(err, localConfiguration) {
var current = self.cache[directory];
var config = localConfiguration || {};

if (localConfiguration) {
merge(configuration, localConfiguration);
}

merge(configuration, self.rcFile);
merge(config, self.rcFile, self.settings.rcPath);

merge(configuration, {
merge(config, {
'settings': self.settings.settings,
'plugins': self.settings.plugins,
'output': self.settings.output
});
}, 'settings');

self.cache[directory] = configuration;
self.cache[directory] = config;

current.forEach(function (callback) {
callback(err, configuration);
callback(err, config);
});
}

Expand All @@ -334,7 +381,6 @@ Configuration.prototype.getConfiguration = function (filePath, callback) {
}

self.cache[directory] = [callback];
configuration = {};
getLocalConfiguration(self, directory, handleLocalConfiguration);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function run(options, callback) {

settings.streamOut = options.streamOut || process.stdout;
settings.streamError = options.streamError || process.stderr;
settings.output = options.output || false;
settings.output = options.output || undefined;
settings.out = options.out;

if (settings.output && settings.out) {
Expand Down

0 comments on commit cf1b4d4

Please sign in to comment.