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

fix: all prefixes excluded from subject placeholder #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions buildCommit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const _ = require('lodash');
const wrap = require('word-wrap');
const defaults = require('./defaults');

const defaultSubjectSeparator = ': ';
const defaultMaxLineWidth = 100;
const defaultBreaklineChar = '|';

const addTicketNumber = (ticketNumber, config) => {
if (!ticketNumber) {
Expand All @@ -16,7 +15,7 @@ const addTicketNumber = (ticketNumber, config) => {
};

const addScope = (scope, config) => {
const separator = _.get(config, 'subjectSeparator', defaultSubjectSeparator);
const separator = _.get(config, 'subjectSeparator', defaults.subjectSeparator);

if (!scope) return separator; // it could be type === WIP. So there is no scope

Expand All @@ -32,7 +31,7 @@ const addType = (type, config) => {
return _.trim(`${prefix}${type}${suffix}`);
};

const addBreaklinesIfNeeded = (value, breaklineChar = defaultBreaklineChar) =>
const addBreaklinesIfNeeded = (value, breaklineChar = defaults.breaklineChar) =>
value
.split(breaklineChar)
.join('\n')
Expand Down
4 changes: 4 additions & 0 deletions defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
subjectSeparator: ': ',
breaklineChar: '|',
};
18 changes: 15 additions & 3 deletions questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const _ = require('lodash');
const buildCommit = require('./buildCommit');
const log = require('./logger');
const defaults = require('./defaults');

const isNotWip = answers => answers.type.toLowerCase() !== 'wip';

Expand All @@ -19,14 +20,24 @@ const isValidateTicketNo = (value, config) => {
return true;
};

const getPreparedCommit = context => {
const getPreparedCommit = (context, config) => {
let message = null;

if (fs.existsSync('./.git/COMMIT_EDITMSG')) {
let preparedCommit = fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8');
const separator = _.get(config, 'subjectSeparator', defaults.subjectSeparator);
const { ticketNumberPrefix, ticketNumberRegExp } = config;

const subjectSeparatorRegex = new RegExp(`^.+${separator}`);
const ticketRegex = new RegExp(`${ticketNumberPrefix}${ticketNumberRegExp}`);

preparedCommit = preparedCommit
.replace(/^#.*/gm, '')
.replace(/^\s*[\r\n]/gm, '')
.replace(/[\r\n]$/, '')
.replace(subjectSeparatorRegex, '')
.replace(ticketRegex, '')
.trimLeft()
.split(/\r\n|\r|\n/);

if (preparedCommit.length && preparedCommit[0]) {
Expand All @@ -37,6 +48,7 @@ const getPreparedCommit = context => {
}
}
}

return message;
};

Expand Down Expand Up @@ -132,7 +144,7 @@ module.exports = {
type: 'input',
name: 'subject',
message: messages.subject,
default: getPreparedCommit('subject'),
default: getPreparedCommit('subject', config),
validate(value) {
const limit = config.subjectLimit || 100;
if (value.length > limit) {
Expand All @@ -150,7 +162,7 @@ module.exports = {
type: 'input',
name: 'body',
message: messages.body,
default: getPreparedCommit('body'),
default: getPreparedCommit('body', config),
},
{
type: 'input',
Expand Down
7 changes: 5 additions & 2 deletions spec/questionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ describe('cz-customizable', () => {
let readFileSync;

beforeEach(() => {
config = {};
config = {
ticketNumberPrefix: 'TICKET-',
ticketNumberRegExp: '\\d{1,5}',
};
existsSync = spyOn(fs, 'existsSync');
readFileSync = spyOn(fs, 'readFileSync');
});
Expand All @@ -290,7 +293,7 @@ describe('cz-customizable', () => {

it('should take a single line commit as the subject', () => {
existsSync.andReturn(true);
readFileSync.andReturn('my commit');
readFileSync.andReturn('type(scope): TICKET-123 my commit');
expect(getQuestion(5).default).toEqual('my commit');
expect(getQuestion(6).default).toEqual(null);
});
Expand Down