Skip to content

Commit bd1d98b

Browse files
authored
Merge pull request #14 from JPeer264/feat/rules
Feat/rules
2 parents a3a6eb4 + 161a972 commit bd1d98b

File tree

11 files changed

+189
-31
lines changed

11 files changed

+189
-31
lines changed

.sgcrc_default

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@
4545
"type": "Test:",
4646
"description": "Adding missing tests or correcting existing tests"
4747
}
48-
]
48+
],
49+
"rules": {
50+
"max-char": 72,
51+
"min-char": 10,
52+
"end-with-dot": false
53+
}
4954
}

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { choices, questions } from './promptConfig';
1111

1212
const configuration = getConfig();
1313
const choicesList = choices(configuration);
14-
const questionsList = questions(choicesList);
14+
const questionsList = questions(choicesList, configuration);
1515
const argv = yargs
1616
.usage('Usage: $0')
1717
.alias('v', 'version')

lib/promptConfig.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chalk from 'chalk';
2+
import ruleWarningMessages from './rules/ruleWarningMessages';
23

34
const choices = (configuration) => {
45
const choicesList = [];
@@ -29,7 +30,13 @@ const questions = (choicesList) => {
2930
type: 'input',
3031
name: 'description',
3132
message: 'Enter your commit message:',
32-
validate: input => (input ? true : 'A commit message is mandatory!'),
33+
validate: (input) => {
34+
const warnings = ruleWarningMessages(input);
35+
36+
if (warnings) return warnings;
37+
38+
return true;
39+
},
3340
},
3441
{
3542
type: 'confirm',

lib/rules/availableRules.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import getConfig from '../getConfig';
2+
3+
const configuration = getConfig();
4+
5+
const rules = {
6+
endWithDot: {
7+
message: 'The commit message can not end with a dot',
8+
check: (input) => {
9+
if (input[input.length - 1] === '.') return false;
10+
return true;
11+
},
12+
},
13+
maxChar: {
14+
message: `The commit message is not allowed to be longer as ${configuration.rules['max-char']}. Consider writing a body.`,
15+
check: (input) => {
16+
if (input.length > configuration.rules['max-char']) return false;
17+
return true;
18+
},
19+
},
20+
minChar: {
21+
message: `The commit message has to be at least ${configuration.rules['min-char']} character.`,
22+
check: (input) => {
23+
if (input.length < configuration.rules['min-char']) return false;
24+
return true;
25+
},
26+
},
27+
};
28+
29+
export default rules;

lib/rules/ruleWarningMessages.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import entries from 'object.entries';
2+
import getConfig from '../getConfig';
3+
import rules from './availableRules';
4+
5+
const configuration = getConfig();
6+
7+
const ruleWarningMessages = (input) => {
8+
let warningMessage = '';
9+
const configRuleEntries = entries(configuration.rules);
10+
11+
configRuleEntries.forEach((rule) => {
12+
const camelCaseRuleName = (rule[0]).replace(/-([a-z])/g, g => (g[1].toUpperCase()));
13+
const ruleIs = rules[camelCaseRuleName].check(input);
14+
15+
if (!ruleIs) warningMessage += `${rules[camelCaseRuleName].message}\n`;
16+
});
17+
18+
return warningMessage;
19+
};
20+
21+
export default ruleWarningMessages;

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
"prepush": "npm test"
1818
},
1919
"ava": {
20-
"require": "babel-register",
20+
"require": [
21+
"babel-register",
22+
"babel-polyfill"
23+
],
2124
"babel": "inherit"
2225
},
2326
"nyc": {
@@ -47,6 +50,7 @@
4750
"devDependencies": {
4851
"ava": "^0.18.2",
4952
"babel-cli": "^6.24.0",
53+
"babel-polyfill": "^6.23.0",
5054
"babel-preset-env": "^1.2.1",
5155
"coveralls": "^2.12.0",
5256
"eslint": "^3.17.1",
@@ -61,6 +65,7 @@
6165
"execa": "^0.6.1",
6266
"inquirer": "^3.0.6",
6367
"json-extra": "^0.5.0",
68+
"object.entries": "^1.0.4",
6469
"update-notifier": "^2.1.0",
6570
"yargs": "^7.0.2"
6671
}

test/fixtures/.sgcrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
"type": "Add:",
66
"description": "Files added"
77
}
8-
]
8+
],
9+
"rules": {
10+
"max-char": 72,
11+
"min-char": 10,
12+
"end-with-dot": false
13+
}
914
}

test/prompConfig.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,40 @@ test('get configuration file equals .sgcrc_default', (t) => {
1111
t.deepEqual(getConfig(), json.readToObjSync(path.join(cwd, '.sgcrc_default')));
1212
});
1313

14-
test('choices are the same as choices generated from .sgcrc_default', (t) => {
15-
const sgc = getConfig();
16-
const configuration = getConfig();
17-
const choicesList = choices(configuration);
14+
test('choices are the same as choices generated from .sgcrc_default', async (t) => {
15+
const sgc = await getConfig();
16+
const choicesList = await choices(sgc);
1817
const choicesArray = [];
1918

20-
Promise.resolve(() => {
21-
sgc.types.forEach((type) => {
22-
const emoji = `${type.emoji} ` || '';
23-
const configType = type.type;
24-
const description = type.description || '';
25-
26-
choicesArray.push({
27-
value: emoji + configType,
28-
name: `${chalk.bold(configType)} ${description}`,
29-
});
30-
}).then(() => {
31-
t.deepEqual(choicesList, choicesArray);
19+
sgc.types.forEach((type) => {
20+
const emoji = `${type.emoji} ` || '';
21+
const configType = type.type;
22+
const description = type.description || '';
23+
24+
choicesArray.push({
25+
value: emoji + configType,
26+
name: `${chalk.bold(configType)} ${description}`,
3227
});
3328
});
29+
30+
t.deepEqual(choicesList, await choicesArray);
3431
});
3532

36-
test('check the values of the question object', (t) => {
37-
const configuration = getConfig();
38-
const choicesList = choices(configuration);
39-
const questionsList = questions(choicesList);
33+
test('check the values of the question object', async (t) => {
34+
const configuration = await getConfig();
35+
const choicesList = await choices(configuration);
36+
const questionsList = await questions(choicesList, configuration);
4037

4138
t.deepEqual(typeof questionsList, 'object');
4239
});
4340

44-
test('validate functions in questions', (t) => {
45-
const configuration = getConfig();
46-
const choicesList = choices(configuration);
47-
const questionsList = questions(choicesList);
41+
test('validate functions in questions', async (t) => {
42+
const configuration = await getConfig();
43+
const choicesList = await choices(configuration);
44+
const questionsList = await questions(choicesList, configuration);
4845

4946
t.deepEqual(questionsList[1].validate('input text'), true);
50-
t.deepEqual(questionsList[1].validate(), 'A commit message is mandatory!');
47+
t.deepEqual(questionsList[1].validate('This message has over 72 characters. So this test will definitely fail. I can guarantee that I am telling the truth'), 'The commit message is not allowed to be longer as 72. Consider writing a body.\n');
5148
});
5249

5350
test('when and default functions in questions', (t) => {

test/rules/availableRules.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import test from 'ava';
2+
import rules from '../../lib/rules/availableRules';
3+
4+
test('rules endWithDot', (t) => {
5+
const endWithDot = rules.endWithDot.check('input with dot.');
6+
const endWithoutDot = rules.endWithDot.check('input with dot');
7+
t.deepEqual(endWithDot, false);
8+
t.deepEqual(endWithoutDot, true);
9+
});
10+
11+
test('rules minChar', (t) => {
12+
const notMinChar = rules.minChar.check('less');
13+
const minChar = rules.minChar.check('this are more than 10 characters');
14+
t.deepEqual(notMinChar, false);
15+
t.deepEqual(minChar, true);
16+
});
17+
18+
test('rules mxChar', (t) => {
19+
const moreThanMaxChar = rules.maxChar.check('this are more than 72 characters, believe me or not but the value moreThanMaxChar will be false ;-P');
20+
const lessThanMaxChar = rules.maxChar.check('this are less than 72 characters');
21+
t.deepEqual(moreThanMaxChar, false);
22+
t.deepEqual(lessThanMaxChar, true);
23+
});

test/rules/ruleWarningMessages.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test from 'ava';
2+
import ruleWaringMessages from '../../lib/rules/ruleWarningMessages';
3+
4+
test('ruleWarningMessages', (t) => {
5+
const messages = ruleWaringMessages('input.');
6+
t.deepEqual(messages, 'The commit message has to be at least 10 character.\nThe commit message can not end with a dot\n');
7+
});

0 commit comments

Comments
 (0)