Skip to content

Commit 602c769

Browse files
committed
Feat: add inherit mode (fixes #26)
1 parent 944d286 commit 602c769

File tree

8 files changed

+111
-10
lines changed

8 files changed

+111
-10
lines changed

.babelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"presets": ["env"]
2+
"presets": ["env"],
3+
"plugins": [
4+
["transform-object-rest-spread", { "useBuiltIns": true }]
5+
]
36
}

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ You can even create a global config. Just go to your users home and create a `.s
4545
- [emoji](#emoji)
4646
- [types](#types)
4747
- [rules](#rules)
48+
- [inherit](#inherit)
4849

4950
### questions
5051

@@ -180,3 +181,28 @@ Example:
180181
}
181182
}
182183
```
184+
185+
### inherit
186+
187+
**Type:** `boolean | array`
188+
189+
**Default:** `false`
190+
191+
This will inherit every object entry which is given in the array. If this is set to true everything is inherited. The own configuration won't get overwritten.
192+
193+
Example:
194+
```js
195+
// following set "emoji" to true, and will inherit everything except "emoji" from the defaults.
196+
{
197+
"emoji": true,
198+
"inherit": true
199+
}
200+
201+
// following has "emoji" to true and just types are "types" from the defaults.
202+
{
203+
"emoji": true,
204+
"inherit": [
205+
"types"
206+
]
207+
}
208+
```

lib/getConfig.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,48 @@ const getConfig = (altPath) => {
1414
const sgcrcDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc'));
1515
const sgcrcTestDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));
1616

17+
const sgcrcDefault = sgcrcDefaultConfig || sgcrcTestDefaultConfig;
18+
1719
// priority order (1. highest priority):
1820
// 1. local config
1921
// - 1. .sgcrc
2022
// - 2. (package.json).sgc
2123
// 2. global config
22-
// 3. default config from ../.sgcrc
23-
// 4. In the test case ../.sgcrc is renamed to ../.sgcrc_default
24-
let config = configObject
25-
|| packageConfig
26-
|| globalConfig
27-
|| sgcrcDefaultConfig
28-
|| sgcrcTestDefaultConfig;
24+
// 3. default config
25+
// - 1. from ../.sgcrc
26+
// - 2. test case ../.sgcrc is renamed to ../.sgcrc_default
27+
let config = configObject || packageConfig || globalConfig || sgcrcDefault;
28+
29+
config.inherit = config.inherit === undefined ? {} : config.inherit;
2930

3031
// set defaults
31-
const configDefaults = {
32+
let configDefaults = {
3233
questions: {
3334
scope: false,
3435
moreInfo: true,
3536
},
3637
};
3738

39+
// inherit everything
40+
if (config.inherit === true) {
41+
configDefaults = sgcrcDefault;
42+
}
43+
44+
if (Object.prototype.toString.call(config.inherit) === '[object Array]') {
45+
config.inherit.forEach((element) => {
46+
if (!config[element] && sgcrcDefault[element] !== undefined) {
47+
config[element] = sgcrcDefault[element];
48+
}
49+
});
50+
}
51+
3852
config = merge({}, configDefaults, config);
3953

40-
return config;
54+
// next will remove "inherit" from the config
55+
// eslint-disable-next-line
56+
const { inherit, ...copiedConfig } = config;
57+
58+
return copiedConfig;
4159
};
4260

4361
export default getConfig;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"devDependencies": {
5252
"ava": "^0.18.2",
5353
"babel-cli": "^6.24.0",
54+
"babel-plugin-transform-object-rest-spread": "^6.23.0",
5455
"babel-polyfill": "^6.23.0",
5556
"babel-preset-env": "^1.2.1",
5657
"coveralls": "^2.12.0",

test/fixtures/.sgcrc_inherit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"emoji": true,
3+
"inherit": [
4+
"emoji",
5+
"rules"
6+
]
7+
}

test/fixtures/.sgcrc_inherit_all

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"emoji": true,
3+
"inherit": true
4+
}

test/questions.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import os from 'os';
22
import test from 'ava';
33
import path from 'path';
44
import fs from 'fs-extra';
5+
import json from 'json-extra';
6+
import merge from 'lodash.merge';
57

68
import getConfig from '../lib/getConfig';
79
import questions, { choices } from '../lib/questions';
@@ -18,16 +20,24 @@ let globalExist = false;
1820

1921
// rename global .sgcrc
2022
test.before(() => {
23+
// rename global config
2124
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
2225
globalExist = true;
2326
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`));
2427
}
28+
29+
// rename local sgcrc
30+
fs.renameSync(path.join(cwd, '.sgcrc'), path.join(cwd, '.sgcrc_default'));
2531
});
2632

2733
test.after.always(() => {
34+
// rename global config
2835
if (globalExist) {
2936
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`), path.join(homedir, '.sgcrc'));
3037
}
38+
39+
// rename local sgcrc
40+
fs.renameSync(path.join(cwd, '.sgcrc_default'), path.join(cwd, '.sgcrc'));
3141
});
3242

3343
test('choices are rendered without emojis', (t) => {
@@ -37,6 +47,27 @@ test('choices are rendered without emojis', (t) => {
3747
t.deepEqual(choicesList, withoutEmoji);
3848
});
3949

50+
test('choices with inherit mode | all', (t) => {
51+
const sgc = getConfig(path.join(fixtures, '.sgcrc_inherit_all'));
52+
const sgcrc = json.readToObjSync(path.join(cwd, '.sgcrc_default'));
53+
54+
t.deepEqual(sgc, merge({}, sgcrc, sgc));
55+
});
56+
57+
test('choices with inherit mode | some', (t) => {
58+
const sgc = getConfig(path.join(fixtures, '.sgcrc_inherit'));
59+
const sgcrc = json.readToObjSync(path.join(cwd, '.sgcrc_default'));
60+
61+
const ownConfig = {
62+
emoji: true,
63+
};
64+
65+
ownConfig.rules = sgcrc.rules;
66+
ownConfig.questions = sgcrc.questions;
67+
68+
t.deepEqual(sgc, ownConfig);
69+
});
70+
4071
test('choices are rendered with emojis (default)', (t) => {
4172
const sgc = getConfig(path.join(fixtures, '.sgcrc'));
4273

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0:
501501
version "6.13.0"
502502
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"
503503

504+
babel-plugin-syntax-object-rest-spread@^6.8.0:
505+
version "6.13.0"
506+
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"
507+
504508
babel-plugin-syntax-trailing-function-commas@^6.13.0, babel-plugin-syntax-trailing-function-commas@^6.20.0:
505509
version "6.22.0"
506510
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.22.0.tgz#ba0360937f8d06e40180a43fe0d5616fff532cf3"
@@ -689,6 +693,13 @@ babel-plugin-transform-exponentiation-operator@^6.8.0:
689693
babel-plugin-syntax-exponentiation-operator "^6.8.0"
690694
babel-runtime "^6.22.0"
691695

696+
babel-plugin-transform-object-rest-spread@^6.23.0:
697+
version "6.23.0"
698+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.23.0.tgz#875d6bc9be761c58a2ae3feee5dc4895d8c7f921"
699+
dependencies:
700+
babel-plugin-syntax-object-rest-spread "^6.8.0"
701+
babel-runtime "^6.22.0"
702+
692703
babel-plugin-transform-regenerator@^6.6.0:
693704
version "6.22.0"
694705
resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.22.0.tgz#65740593a319c44522157538d690b84094617ea6"

0 commit comments

Comments
 (0)