Skip to content

Commit 7b938c0

Browse files
authored
Merge pull request #27 from JPeer264/feature/scope
Breaking Changes
2 parents 781bba3 + c3baf46 commit 7b938c0

22 files changed

+428
-213
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
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ coverage
88

99
# macOS
1010
.DS_STORE
11+
12+
# IDE / Editors
13+
.vscode

.sgcrc_default renamed to .sgcrc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"scope": false,
3+
"body": true,
4+
"emoji": false,
25
"types": [
36
{
47
"emoji": ":wrench:",
@@ -44,11 +47,6 @@
4447
"emoji": ":white_check_mark:",
4548
"type": "Test:",
4649
"description": "Adding missing tests or correcting existing tests"
47-
},
48-
{
49-
"emoji": ":tada:",
50-
"type": "Init:",
51-
"description": "The very first commit"
5250
}
5351
],
5452
"rules": {

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ node_js:
55
- "4"
66
- "6"
77
install:
8-
- npm i -g npm
9-
- npm install
8+
- yarn
109
script: npm test
1110
notifications:
1211
email:
1312
on_failure: change
13+
on_success: change
1414
after_success:
1515
- npm run coveralls

README.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,61 @@ $ sgc
4141
You can even create a global config. Just go to your users home and create a `.sgcrc`. The global config will be triggered if no project configurations are present.
4242

4343
**Options:**
44-
- [emojies](#emojies)
44+
- [body](#body)
45+
- [scope](#scope)
46+
- [emoji](#emoji)
4547
- [types](#types)
4648
- [rules](#rules)
4749

48-
### emojies
50+
### body
4951

5052
**Type:** `boolean`
5153

52-
**Default:** `true`
54+
**Default**: `true`
55+
56+
Asks if more info (body) should be added. This will open your default editor.
57+
58+
Example:
59+
```json
60+
{
61+
"body": false
62+
}
63+
```
64+
65+
### scope
66+
67+
**Type:** `boolean`
68+
69+
**Default:** `false`
5370

54-
A boolean to enable emojies at the beginning of a commit message
71+
Asks for the scope in parentheses of the commit.
5572

5673
Example:
5774
```json
5875
{
59-
"emojies": true
76+
"scope": true
77+
}
78+
```
79+
80+
### emoji
81+
82+
**Type:** `boolean`
83+
84+
**Default:** `false`
85+
86+
A boolean to enable emoji at the beginning of a commit message
87+
88+
Example:
89+
```json
90+
{
91+
"emoji": true
6092
}
6193
```
6294

6395
### types
6496

65-
> Types will define your git commits.
97+
> Types will define your git commits. If `types` is not set in your own `.sgcrc`, the `types` of the global [.sgcrc](.sgcrc)
98+
6699

67100
**Keys**
68101

@@ -114,15 +147,15 @@ Available rules:
114147

115148
**Type:** `number`
116149

117-
**Default:** `undefined`
150+
**Default:** `72`
118151

119-
If a number is set, it will not allow to commit messages **more than** the given number
152+
If a number is set, it will not allow to commit messages **more than** the given number. If it is set to `-1` the rule is deactivated
120153

121154
Example:
122155
```json
123156
{
124157
"rules": {
125-
"max-char": 10
158+
"max-char": -1
126159
}
127160
}
128161
```
@@ -131,15 +164,15 @@ Example:
131164

132165
**Type:** `number`
133166

134-
**Default:** `undefined`
167+
**Default:** `10`
135168

136-
If a number is set, it will not allow to commit messages **less than** the given number
169+
If a number is set, it will not allow to commit messages **less than** the given number. If it is set to `-1` the rule is deactivated
137170

138171
Example:
139172
```json
140173
{
141174
"rules": {
142-
"min-char": 10
175+
"min-char": -1
143176
}
144177
}
145178
```

lib/cli.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
import chalk from 'chalk';
34
import execa from 'execa';
45
import yargs from 'yargs';
@@ -9,11 +10,11 @@ import updateNotifier from 'update-notifier';
910

1011
import pkg from '../package.json';
1112
import getConfig from './getConfig';
12-
import { choices, questions } from './promptConfig';
13+
import questions from './questions';
14+
import combineTypeScope from './helpers/combineTypeScope';
1315

14-
const configuration = getConfig();
15-
const choicesList = choices(configuration);
16-
const questionsList = questions(choicesList, configuration);
16+
const config = getConfig();
17+
const questionsList = questions(config);
1718
const argv = yargs
1819
.usage('Usage: $0')
1920
.alias('v', 'version')
@@ -32,7 +33,8 @@ if (argv.v) {
3233
console.error(chalk.red('Please', chalk.bold('git add'), 'some files first before you commit.'));
3334
} else {
3435
inquirer.prompt(questionsList).then((answers) => {
35-
const message = answers.moreInfo ? `${answers.editor}` : `${answers.type} ${answers.description}`;
36+
const typeScope = combineTypeScope(answers.type, answers.scope);
37+
const message = answers.body ? `${answers.editor}` : `${typeScope} ${answers.description}`;
3638

3739
return execa('git', ['commit', '-m', message], { stdio: 'inherit' })
3840
.catch(() => {
@@ -41,4 +43,3 @@ if (argv.v) {
4143
});
4244
});
4345
}
44-

lib/getConfig.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os from 'os';
22
import path from 'path';
33
import json from 'json-extra';
4+
import merge from 'lodash.merge';
45

56
const cwd = process.cwd();
67
const homedir = os.homedir();
@@ -10,17 +11,35 @@ const getConfig = (altPath) => {
1011
const configObject = json.readToObjSync(pathString);
1112
const globalConfig = json.readToObjSync(path.join(homedir, '.sgcrc'));
1213
const packageConfig = json.readToObjSync(path.join(cwd, 'package.json')).sgc;
13-
const sgcrcDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));
14+
const sgcrcDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc'));
15+
const sgcrcTestDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));
16+
17+
const sgcrcDefault = sgcrcDefaultConfig || sgcrcTestDefaultConfig;
1418

1519
// priority order (1. highest priority):
1620
// 1. local config
1721
// - 1. .sgcrc
1822
// - 2. (package.json).sgc
1923
// 2. global config
20-
// 3. default config from ../.sgcrc_default
21-
const config = configObject || packageConfig || globalConfig || sgcrcDefaultConfig;
24+
// 3. default config
25+
// - 1. from ../.sgcrc
26+
// - 2. test case ../.sgcrc is renamed to ../.sgcrc_default
27+
const config = configObject || packageConfig || globalConfig || sgcrcDefault;
28+
29+
// set defaults which are necessary
30+
const tempConfig = merge({}, sgcrcDefault, config);
31+
32+
// do not merge types
33+
// so return them to their set default
34+
if (config.types) {
35+
tempConfig.types = config.types;
36+
}
37+
38+
// next will remove "inherit" from the config
39+
// eslint-disable-next-line
40+
const { inherit, ...copiedConfig } = tempConfig;
2241

23-
return config;
42+
return copiedConfig;
2443
};
2544

2645
export default getConfig;

lib/helpers/combineTypeScope.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const combineTypeScope = (type, scope) => {
2+
let thisType = type;
3+
4+
const thisScope = scope || '';
5+
6+
// add scope correctly if ':' is at the end
7+
if (thisScope.length > 0) {
8+
if (thisType.charAt(thisType.length - 1) === ':') {
9+
thisType = thisType.slice(0, thisType.length - 1);
10+
thisType = `${thisType}${thisScope}:`;
11+
} else {
12+
thisType += thisScope;
13+
}
14+
}
15+
16+
return thisType;
17+
};
18+
19+
export default combineTypeScope;

lib/promptConfig.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

lib/questions.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import chalk from 'chalk';
2+
import ruleWarningMessages from './rules/ruleWarningMessages';
3+
import combineTypeScope from './helpers/combineTypeScope';
4+
5+
const choices = (config) => {
6+
const choicesList = [];
7+
8+
config.types.forEach((type) => {
9+
const emoji = config.emoji && type.emoji ? `${type.emoji} ` : '';
10+
const configType = type.type;
11+
const description = type.description || '';
12+
13+
choicesList.push({
14+
value: emoji + configType,
15+
name: `${chalk.bold(configType)} ${description}`,
16+
});
17+
});
18+
19+
return choicesList;
20+
};
21+
22+
const questions = (config) => {
23+
const choicesList = choices(config);
24+
const questionsList = [
25+
{
26+
type: 'list',
27+
name: 'type',
28+
message: 'Select the type of your commit:',
29+
choices: choicesList,
30+
},
31+
{
32+
type: 'input',
33+
name: 'scope',
34+
message: 'Enter your scope (no whitespaces allowed):',
35+
when: () => config.scope,
36+
validate: input => (input.match(/\s/) !== null ? 'No whitespaces allowed' : true),
37+
filter: input => (input ? `(${input})` : input),
38+
},
39+
{
40+
type: 'input',
41+
name: 'description',
42+
message: 'Enter your commit message:',
43+
validate: (input) => {
44+
const warnings = ruleWarningMessages(input, config);
45+
46+
return warnings || true;
47+
},
48+
},
49+
{
50+
type: 'confirm',
51+
name: 'body',
52+
message: 'Do you want to add a body?',
53+
when: () => config.body,
54+
default: false,
55+
},
56+
{
57+
type: 'editor',
58+
name: 'editor',
59+
message: 'This will let you add more information',
60+
when: answers => answers.body,
61+
default: (answers) => {
62+
const type = combineTypeScope(answers.type, answers.scope);
63+
64+
return `${type} ${answers.description}\n\n\n`;
65+
},
66+
},
67+
];
68+
69+
return questionsList;
70+
};
71+
72+
export default questions;
73+
export { choices };

0 commit comments

Comments
 (0)