Skip to content

Commit

Permalink
fix: cli options should override package.json options
Browse files Browse the repository at this point in the history
The first priority from CLI(from user) settings then package.json.

Fixed #845
  • Loading branch information
anthony-redFox authored and tmcw committed Aug 4, 2017
1 parent dccb151 commit ecf16bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
31 changes: 26 additions & 5 deletions __tests__/lib/merge_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,30 @@ test('bad config', async function() {
}
});

test('nc(mergeConfig)', function(done) {
test('right merging package configuration', async function() {
// Omit configuration from output, for simplicity
var nc = _.curryRight(_.omit, 2)([
'config',
'no-package',
'parseExtension',
'project-homepage',
'project-version'
]);
return mergeConfig({
config: path.join(__dirname, '../config_fixture/config.json'),
'no-package': true,
'project-name': 'cool Documentation'
})
.then(nc)
.then(res => {
expect(res).toEqual({
'project-name': 'cool Documentation',
foo: 'bar'
});
});
});

test('nc(mergeConfig)', async function() {
// Omit configuration from output, for simplicity
var nc = _.curryRight(_.omit, 2)([
'config',
Expand All @@ -21,7 +44,7 @@ test('nc(mergeConfig)', function(done) {
'project-version'
]);

Promise.all(
return Promise.all(
[
[
{ config: path.join(__dirname, '../config_fixture/config.json') },
Expand Down Expand Up @@ -74,7 +97,5 @@ test('nc(mergeConfig)', function(done) {
expect(res).toEqual(pair[1]);
})
)
).then(res => {
done();
});
);
});
19 changes: 7 additions & 12 deletions src/merge_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var yaml = require('js-yaml'),
fs = require('fs'),
pify = require('pify'),
_ = require('lodash'),
readPkgUp = require('read-pkg-up'),
path = require('path'),
stripComments = require('strip-json-comments');
Expand All @@ -29,7 +28,7 @@ function processToc(config: DocumentationConfig, absFilePath: string) {
* values of `name` and `version` config.
*
* @param {Object} config the user-provided config, usually via argv
* @returns {Object} configuration with inferred parameters
* @returns {Promise<Object>} configuration with inferred parameters
* @throws {Error} if the file cannot be read.
*/
function mergePackage(config: Object): Promise<Object> {
Expand All @@ -38,16 +37,12 @@ function mergePackage(config: Object): Promise<Object> {
}
return (
readPkgUp()
.then(pkg =>
_.defaults(
{},
_.mapKeys(
_.pick(pkg.pkg, ['name', 'homepage', 'version']),
(val, key) => `project-${key}`
),
config
)
)
.then(pkg => {
['name', 'homepage', 'version'].forEach(key => {
config[`project-${key}`] = config[`project-${key}`] || pkg.pkg[key];
});
return config;
})
// Allow this to fail: this inference is not required.
.catch(() => config)
);
Expand Down

0 comments on commit ecf16bd

Please sign in to comment.