Skip to content

Commit ecf16bd

Browse files
anthony-redFoxtmcw
authored andcommitted
fix: cli options should override package.json options
The first priority from CLI(from user) settings then package.json. Fixed #845
1 parent dccb151 commit ecf16bd

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

__tests__/lib/merge_config.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,30 @@ test('bad config', async function() {
1010
}
1111
});
1212

13-
test('nc(mergeConfig)', function(done) {
13+
test('right merging package configuration', async function() {
14+
// Omit configuration from output, for simplicity
15+
var nc = _.curryRight(_.omit, 2)([
16+
'config',
17+
'no-package',
18+
'parseExtension',
19+
'project-homepage',
20+
'project-version'
21+
]);
22+
return mergeConfig({
23+
config: path.join(__dirname, '../config_fixture/config.json'),
24+
'no-package': true,
25+
'project-name': 'cool Documentation'
26+
})
27+
.then(nc)
28+
.then(res => {
29+
expect(res).toEqual({
30+
'project-name': 'cool Documentation',
31+
foo: 'bar'
32+
});
33+
});
34+
});
35+
36+
test('nc(mergeConfig)', async function() {
1437
// Omit configuration from output, for simplicity
1538
var nc = _.curryRight(_.omit, 2)([
1639
'config',
@@ -21,7 +44,7 @@ test('nc(mergeConfig)', function(done) {
2144
'project-version'
2245
]);
2346

24-
Promise.all(
47+
return Promise.all(
2548
[
2649
[
2750
{ config: path.join(__dirname, '../config_fixture/config.json') },
@@ -74,7 +97,5 @@ test('nc(mergeConfig)', function(done) {
7497
expect(res).toEqual(pair[1]);
7598
})
7699
)
77-
).then(res => {
78-
done();
79-
});
100+
);
80101
});

src/merge_config.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var yaml = require('js-yaml'),
44
fs = require('fs'),
55
pify = require('pify'),
6-
_ = require('lodash'),
76
readPkgUp = require('read-pkg-up'),
87
path = require('path'),
98
stripComments = require('strip-json-comments');
@@ -29,7 +28,7 @@ function processToc(config: DocumentationConfig, absFilePath: string) {
2928
* values of `name` and `version` config.
3029
*
3130
* @param {Object} config the user-provided config, usually via argv
32-
* @returns {Object} configuration with inferred parameters
31+
* @returns {Promise<Object>} configuration with inferred parameters
3332
* @throws {Error} if the file cannot be read.
3433
*/
3534
function mergePackage(config: Object): Promise<Object> {
@@ -38,16 +37,12 @@ function mergePackage(config: Object): Promise<Object> {
3837
}
3938
return (
4039
readPkgUp()
41-
.then(pkg =>
42-
_.defaults(
43-
{},
44-
_.mapKeys(
45-
_.pick(pkg.pkg, ['name', 'homepage', 'version']),
46-
(val, key) => `project-${key}`
47-
),
48-
config
49-
)
50-
)
40+
.then(pkg => {
41+
['name', 'homepage', 'version'].forEach(key => {
42+
config[`project-${key}`] = config[`project-${key}`] || pkg.pkg[key];
43+
});
44+
return config;
45+
})
5146
// Allow this to fail: this inference is not required.
5247
.catch(() => config)
5348
);

0 commit comments

Comments
 (0)