Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine.js): appends .gitmessage #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions engine.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ var wrap = require('word-wrap');
var map = require('lodash.map');
var longest = require('longest');
var chalk = require('chalk');
var fs = require('fs');

var filter = function(array) {
return array.filter(function(x) {
@@ -99,10 +100,14 @@ module.exports = function(options) {
},
default: options.defaultSubject,
validate: function(subject, answers) {
var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase);
var filteredSubject = filterSubject(
subject,
options.disableSubjectLowerCase
);
return filteredSubject.length == 0
? 'subject is required'
: filteredSubject.length <= maxSummaryLength(options, answers)
: filteredSubject.length <=
maxSummaryLength(options, answers)
? true
: 'Subject length must be less than or equal to ' +
maxSummaryLength(options, answers) +
@@ -111,15 +116,21 @@ module.exports = function(options) {
' characters.';
},
transformer: function(subject, answers) {
var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase);
var filteredSubject = filterSubject(
subject,
options.disableSubjectLowerCase
);
var color =
filteredSubject.length <= maxSummaryLength(options, answers)
? chalk.green
: chalk.red;
return color('(' + filteredSubject.length + ') ' + subject);
},
filter: function(subject) {
return filterSubject(subject, options.disableSubjectLowerCase);
return filterSubject(
subject,
options.disableSubjectLowerCase
);
}
},
{
@@ -159,7 +170,6 @@ module.exports = function(options) {
return answers.isBreaking;
}
},

{
type: 'confirm',
name: 'isIssueAffected',
@@ -174,27 +184,32 @@ module.exports = function(options) {
'If issues are closed, the commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return (
answers.isIssueAffected && !answers.body && !answers.breakingBody
answers.isIssueAffected &&
!answers.body &&
!answers.breakingBody
);
}
},
{
type: 'input',
name: 'issues',
message: 'Add issue references (e.g. "fix #123", "re #123".):\n',
message:
'Add issue references (e.g. "fix #123", "re #123".):\n',
when: function(answers) {
return answers.isIssueAffected;
},
default: options.defaultIssues ? options.defaultIssues : undefined
default: options.defaultIssues
? options.defaultIssues
: undefined
},
{
type: 'confirm',
name: 'includeGitMessage',
message: 'Do you want to append existing .gitmessage?',
default: false
}
]).then(function(answers) {
var wrapOptions = {
trim: true,
cut: false,
newline: '\n',
indent: '',
width: options.maxLineWidth
};
var wrapOptions = { trim: true, cut: false, newline: '\n', indent: '', width: options.maxLineWidth };

// parentheses are only needed when a scope is present
var scope = answers.scope ? '(' + answers.scope + ')' : '';
@@ -207,14 +222,14 @@ module.exports = function(options) {

// Apply breaking change prefix, removing it if already present
var breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking
? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '')
: '';
breaking = breaking ? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '') : '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;

var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;

commit(filter([head, body, breaking, issues]).join('\n\n'));
var gitMessage = answers.includeGitMessage ? fs.readFileSync('./.git/.gitmessage', 'utf8') : '';

commit(filter([head, body, breaking, issues, gitMessage]).join('\n\n'));
});
}
};
23 changes: 23 additions & 0 deletions engine.test.js
Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@ var longIssues =
'b b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b bb b';
var breakingChange = 'BREAKING CHANGE: ';
var breaking = 'asdhdfkjhbakjdhjkashd adhfajkhs asdhkjdsh ahshd';
var includeGitMessage = true;
var gitMessage = 'my lovely git message';
var longIssuesSplit =
longIssues.slice(0, defaultOptions.maxLineWidth).trim() +
'\n' +
@@ -47,6 +49,8 @@ var longIssuesSplit =
'\n' +
longIssues.slice(defaultOptions.maxLineWidth * 2, longIssues.length).trim();

const mockfs = require('mock-fs');

describe('commit message', function() {
it('only header w/ out scope', function() {
expect(
@@ -255,6 +259,22 @@ describe('commit message', function() {
`${type}(${scope}): ${subject}\n\n${longBodySplit}\n\n${breakingChange}${breaking}\n\n${longIssuesSplit}`
);
});

it('header, long body, breaking change (with prefix entered), long issues w/ scope and included git message', function() {
mockfs({ './.git': { '.gitmessage': gitMessage } });

expect(commitMessage({
type,
scope,
subject,
body: longBody,
breaking: `${breakingChange}${breaking}`,
issues: longIssues,
includeGitMessage
})).to.equal(`${type}(${scope}): ${subject}\n\n${longBodySplit}\n\n${breakingChange}${breaking}\n\n${longIssuesSplit}\n\n${gitMessage}`);

mockfs.restore();
});
});

describe('validation', function() {
@@ -337,6 +357,9 @@ describe('defaults', function() {
it('disableSubjectLowerCase default', function() {
expect(questionDefault('disableSubjectLowerCase')).to.be.undefined;
});
it('defaultIncludeGitMessage default', function() {
expect(questionDefault('includeGitMessage')).to.be.false;
});
});

describe('prompts', function() {
Loading