fix: cli options should override package.json options

The first priority from CLI(from user) settings then package.json.

Fixed #845
This commit is contained in:
Anton 2017-08-04 21:29:05 +03:00 committed by Tom MacWright
parent dccb15160d
commit ecf16bdaa0
2 changed files with 33 additions and 17 deletions

View File

@ -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',
@ -21,7 +44,7 @@ test('nc(mergeConfig)', function(done) {
'project-version'
]);
Promise.all(
return Promise.all(
[
[
{ config: path.join(__dirname, '../config_fixture/config.json') },
@ -74,7 +97,5 @@ test('nc(mergeConfig)', function(done) {
expect(res).toEqual(pair[1]);
})
)
).then(res => {
done();
});
);
});

View File

@ -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');
@ -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> {
@ -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)
);