Skip to content

Commit c8452d1

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

File tree

3 files changed

+104
-101
lines changed

3 files changed

+104
-101
lines changed

engine.js

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

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"author": "Jim Cummins <[email protected]>",
1717
"license": "MIT",
1818
"dependencies": {
19+
"conventional-commit-types": "^2.0.0",
20+
"lodash.map": "^4.5.1",
21+
"longest": "^1.0.1",
22+
"pad-right": "^0.2.2",
23+
"right-pad": "^1.0.1",
1924
"word-wrap": "^1.0.3"
2025
},
2126
"devDependencies": {

0 commit comments

Comments
 (0)