Skip to content

Commit

Permalink
feat(adapter): refactor to engine, default to conventional commit types
Browse files Browse the repository at this point in the history
  • Loading branch information
adjohnson916 committed Aug 13, 2016
1 parent 39182b8 commit c8452d1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 101 deletions.
94 changes: 94 additions & 0 deletions engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"format cjs";

var wrap = require('word-wrap');
var map = require('lodash.map');
var longest = require('longest');
var rightPad = require('right-pad');

// This can be any kind of SystemJS compatible module.
// We use Commonjs here, but ES6 or AMD would do just
// fine.
module.exports = function (options) {

var types = options.types;

var length = longest(Object.keys(types)).length + 1;
var choices = map(types, function (type, key) {
return {
name: rightPad(key + ':', length) + ' ' + type.description,
value: key
};
});

return {
// When a user runs `git cz`, prompter will
// be executed. We pass you cz, which currently
// is just an instance of inquirer.js. Using
// this you can ask questions and get answers.
//
// The commit callback should be executed when
// you're ready to send back a commit template
// to git.
//
// By default, we'll de-indent your commit
// template and will keep empty lines.
prompter: function(cz, commit) {
console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');

// Let's ask some questions of the user
// so that we can populate our commit
// template.
//
// See inquirer.js docs for specifics.
// You can also opt to use another input
// collection library if you prefer.
cz.prompt([
{
type: 'list',
name: 'type',
message: 'Select the type of change that you\'re committing:',
choices: choices
}, {
type: 'input',
name: 'scope',
message: 'Denote the scope of this change ($location, $browser, $compile, etc.):\n'
}, {
type: 'input',
name: 'subject',
message: 'Write a short, imperative tense description of the change:\n'
}, {
type: 'input',
name: 'body',
message: 'Provide a longer description of the change:\n'
}, {
type: 'input',
name: 'footer',
message: 'List any breaking changes or issues closed by this change:\n'
}
]).then(function(answers) {

var maxLineWidth = 100;

var wrapOptions = {
trim: true,
newline: '\n',
indent:'',
width: maxLineWidth
};

// parentheses are only needed when a scope is present
var scope = answers.scope.trim();
scope = scope ? '(' + answers.scope.trim() + ')' : '';

// Hard limit this line
var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth);

// Wrap these lines at 100 characters
var body = wrap(answers.body, wrapOptions);
var footer = wrap(answers.footer, wrapOptions);

commit(head + '\n\n' + body + '\n\n' + footer);
});
}
};
};
106 changes: 5 additions & 101 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,8 @@
"format cjs";

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

// This can be any kind of SystemJS compatible module.
// We use Commonjs here, but ES6 or AMD would do just
// fine.
module.exports = {

// When a user runs `git cz`, prompter will
// be executed. We pass you cz, which currently
// is just an instance of inquirer.js. Using
// this you can ask questions and get answers.
//
// The commit callback should be executed when
// you're ready to send back a commit template
// to git.
//
// By default, we'll de-indent your commit
// template and will keep empty lines.
prompter: function(cz, commit) {
console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');

// Let's ask some questions of the user
// so that we can populate our commit
// template.
//
// See inquirer.js docs for specifics.
// You can also opt to use another input
// collection library if you prefer.
cz.prompt([
{
type: 'list',
name: 'type',
message: 'Select the type of change that you\'re committing:',
choices: [
{
name: 'feat: A new feature',
value: 'feat'
}, {
name: 'fix: A bug fix',
value: 'fix'
}, {
name: 'docs: Documentation only changes',
value: 'docs'
}, {
name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc.)',
value: 'style'
}, {
name: 'refactor: A code change that neither fixes a bug or adds a feature',
value: 'refactor'
}, {
name: 'perf: A code change that improves performance',
value: 'perf'
}, {
name: 'test: Adding missing tests',
value: 'test'
}, {
name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation',
value: 'chore'
}]
}, {
type: 'input',
name: 'scope',
message: 'Denote the scope of this change ($location, $browser, $compile, etc.):\n'
}, {
type: 'input',
name: 'subject',
message: 'Write a short, imperative tense description of the change:\n'
}, {
type: 'input',
name: 'body',
message: 'Provide a longer description of the change:\n'
}, {
type: 'input',
name: 'footer',
message: 'List any breaking changes or issues closed by this change:\n'
}
]).then(function(answers) {

var maxLineWidth = 100;

var wrapOptions = {
trim: true,
newline: '\n',
indent:'',
width: maxLineWidth
};

// parentheses are only needed when a scope is present
var scope = answers.scope.trim();
scope = scope ? '(' + answers.scope.trim() + ')' : '';

// Hard limit this line
var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth);

// Wrap these lines at 100 characters
var body = wrap(answers.body, wrapOptions);
var footer = wrap(answers.footer, wrapOptions);

commit(head + '\n\n' + body + '\n\n' + footer);
});
}
}
module.exports = engine({
types: conventionalCommitTypes.types
});
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"author": "Jim Cummins <[email protected]>",
"license": "MIT",
"dependencies": {
"conventional-commit-types": "^2.0.0",
"lodash.map": "^4.5.1",
"longest": "^1.0.1",
"pad-right": "^0.2.2",
"right-pad": "^1.0.1",
"word-wrap": "^1.0.3"
},
"devDependencies": {
Expand Down

0 comments on commit c8452d1

Please sign in to comment.