Skip to content

Commit beeb191

Browse files
committed
feat: refactor to engine, default to conventional commit types
Closes commitizen#29
1 parent 39182b8 commit beeb191

File tree

3 files changed

+116
-101
lines changed

3 files changed

+116
-101
lines changed

engine.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"format cjs";
2+
3+
var wrap = require('word-wrap');
4+
var objectAssign = require('object-assign');
5+
var objectValues = require('object-values');
6+
var forEach = require('lodash.foreach');
7+
var mapKeys = require('lodash.mapkeys');
8+
var mapValues = require('lodash.mapvalues');
9+
var rightPadKeys = require('right-pad-keys');
10+
var conventionalCommitTypes = require('conventional-commit-types');
11+
12+
// This can be any kind of SystemJS compatible module.
13+
// We use Commonjs here, but ES6 or AMD would do just
14+
// fine.
15+
module.exports = function (options) {
16+
17+
options = options || {};
18+
19+
var types = objectAssign({}, options.types || conventionalCommitTypes.types);
20+
forEach(types, function (type, key) {
21+
type.key = key;
22+
});
23+
types = mapKeys(types, function (type, key) {
24+
return key + ':';
25+
});
26+
types = rightPadKeys(types);
27+
28+
var choices = mapValues(types, function (type, key) {
29+
return {
30+
name: key + ' ' + type.description,
31+
value: type.key
32+
};
33+
});
34+
choices = objectValues(choices);
35+
36+
return {
37+
// When a user runs `git cz`, prompter will
38+
// be executed. We pass you cz, which currently
39+
// is just an instance of inquirer.js. Using
40+
// this you can ask questions and get answers.
41+
//
42+
// The commit callback should be executed when
43+
// you're ready to send back a commit template
44+
// to git.
45+
//
46+
// By default, we'll de-indent your commit
47+
// template and will keep empty lines.
48+
prompter: function(cz, commit) {
49+
console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');
50+
51+
// Let's ask some questions of the user
52+
// so that we can populate our commit
53+
// template.
54+
//
55+
// See inquirer.js docs for specifics.
56+
// You can also opt to use another input
57+
// collection library if you prefer.
58+
cz.prompt([
59+
{
60+
type: 'list',
61+
name: 'type',
62+
message: 'Select the type of change that you\'re committing:',
63+
choices: choices
64+
}, {
65+
type: 'input',
66+
name: 'scope',
67+
message: 'Denote the scope of this change ($location, $browser, $compile, etc.):\n'
68+
}, {
69+
type: 'input',
70+
name: 'subject',
71+
message: 'Write a short, imperative tense description of the change:\n'
72+
}, {
73+
type: 'input',
74+
name: 'body',
75+
message: 'Provide a longer description of the change:\n'
76+
}, {
77+
type: 'input',
78+
name: 'footer',
79+
message: 'List any breaking changes or issues closed by this change:\n'
80+
}
81+
]).then(function(answers) {
82+
83+
var maxLineWidth = 100;
84+
85+
var wrapOptions = {
86+
trim: true,
87+
newline: '\n',
88+
indent:'',
89+
width: maxLineWidth
90+
};
91+
92+
// parentheses are only needed when a scope is present
93+
var scope = answers.scope.trim();
94+
scope = scope ? '(' + answers.scope.trim() + ')' : '';
95+
96+
// Hard limit this line
97+
var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth);
98+
99+
// Wrap these lines at 100 characters
100+
var body = wrap(answers.body, wrapOptions);
101+
var footer = wrap(answers.footer, wrapOptions);
102+
103+
commit(head + '\n\n' + body + '\n\n' + footer);
104+
});
105+
}
106+
};
107+
};

index.js

+2-101
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,5 @@
11
"format cjs";
22

3-
var wrap = require('word-wrap');
3+
var engine = require('./engine');
44

5-
// This can be any kind of SystemJS compatible module.
6-
// We use Commonjs here, but ES6 or AMD would do just
7-
// fine.
8-
module.exports = {
9-
10-
// When a user runs `git cz`, prompter will
11-
// be executed. We pass you cz, which currently
12-
// is just an instance of inquirer.js. Using
13-
// this you can ask questions and get answers.
14-
//
15-
// The commit callback should be executed when
16-
// you're ready to send back a commit template
17-
// to git.
18-
//
19-
// By default, we'll de-indent your commit
20-
// template and will keep empty lines.
21-
prompter: function(cz, commit) {
22-
console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');
23-
24-
// Let's ask some questions of the user
25-
// so that we can populate our commit
26-
// template.
27-
//
28-
// See inquirer.js docs for specifics.
29-
// You can also opt to use another input
30-
// collection library if you prefer.
31-
cz.prompt([
32-
{
33-
type: 'list',
34-
name: 'type',
35-
message: 'Select the type of change that you\'re committing:',
36-
choices: [
37-
{
38-
name: 'feat: A new feature',
39-
value: 'feat'
40-
}, {
41-
name: 'fix: A bug fix',
42-
value: 'fix'
43-
}, {
44-
name: 'docs: Documentation only changes',
45-
value: 'docs'
46-
}, {
47-
name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc.)',
48-
value: 'style'
49-
}, {
50-
name: 'refactor: A code change that neither fixes a bug or adds a feature',
51-
value: 'refactor'
52-
}, {
53-
name: 'perf: A code change that improves performance',
54-
value: 'perf'
55-
}, {
56-
name: 'test: Adding missing tests',
57-
value: 'test'
58-
}, {
59-
name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation',
60-
value: 'chore'
61-
}]
62-
}, {
63-
type: 'input',
64-
name: 'scope',
65-
message: 'Denote the scope of this change ($location, $browser, $compile, etc.):\n'
66-
}, {
67-
type: 'input',
68-
name: 'subject',
69-
message: 'Write a short, imperative tense description of the change:\n'
70-
}, {
71-
type: 'input',
72-
name: 'body',
73-
message: 'Provide a longer description of the change:\n'
74-
}, {
75-
type: 'input',
76-
name: 'footer',
77-
message: 'List any breaking changes or issues closed by this change:\n'
78-
}
79-
]).then(function(answers) {
80-
81-
var maxLineWidth = 100;
82-
83-
var wrapOptions = {
84-
trim: true,
85-
newline: '\n',
86-
indent:'',
87-
width: maxLineWidth
88-
};
89-
90-
// parentheses are only needed when a scope is present
91-
var scope = answers.scope.trim();
92-
scope = scope ? '(' + answers.scope.trim() + ')' : '';
93-
94-
// Hard limit this line
95-
var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth);
96-
97-
// Wrap these lines at 100 characters
98-
var body = wrap(answers.body, wrapOptions);
99-
var footer = wrap(answers.footer, wrapOptions);
100-
101-
commit(head + '\n\n' + body + '\n\n' + footer);
102-
});
103-
}
104-
}
5+
module.exports = engine();

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
"author": "Jim Cummins <[email protected]>",
1717
"license": "MIT",
1818
"dependencies": {
19+
"conventional-commit-types": "^2.0.0",
20+
"lodash.foreach": "^4.4.1",
21+
"lodash.mapkeys": "^4.5.1",
22+
"lodash.mapvalues": "^4.5.1",
23+
"object-assign": "^4.1.0",
24+
"object-values": "^1.0.0",
25+
"right-pad-keys": "^0.1.2",
1926
"word-wrap": "^1.0.3"
2027
},
2128
"devDependencies": {

0 commit comments

Comments
 (0)