Skip to content

Commit 56575d1

Browse files
committed
feat(adapter): refactor to engine, default to conventional commit types
Closes commitizen#29
1 parent 39182b8 commit 56575d1

File tree

3 files changed

+107
-101
lines changed

3 files changed

+107
-101
lines changed

engine.js

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

index.js

Lines changed: 5 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,8 @@
11
"format cjs";
22

3-
var wrap = require('word-wrap');
3+
var engine = require('./engine');
4+
var conventionalCommitTypes = require('conventional-commit-types');
45

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-
}
6+
module.exports = engine({
7+
types: conventionalCommitTypes.types
8+
});

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
"author": "Jim Cummins <[email protected]>",
1717
"license": "MIT",
1818
"dependencies": {
19+
"conventional-commit-types": "^2.0.0",
20+
"lodash.clonedeep": "^4.4.1",
21+
"lodash.map": "^4.5.1",
22+
"longest": "^1.0.1",
23+
"pad-right": "^0.2.2",
24+
"right-pad": "^1.0.1",
1925
"word-wrap": "^1.0.3"
2026
},
2127
"devDependencies": {

0 commit comments

Comments
 (0)