diff --git a/buildCommit.js b/buildCommit.js index d17d7ff..c8770c4 100644 --- a/buildCommit.js +++ b/buildCommit.js @@ -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) { @@ -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 @@ -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') diff --git a/defaults.js b/defaults.js new file mode 100644 index 0000000..d96fee9 --- /dev/null +++ b/defaults.js @@ -0,0 +1,4 @@ +module.exports = { + subjectSeparator: ': ', + breaklineChar: '|', +}; diff --git a/questions.js b/questions.js index b0f0fb6..fde3ba4 100644 --- a/questions.js +++ b/questions.js @@ -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'; @@ -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]) { @@ -37,6 +48,7 @@ const getPreparedCommit = context => { } } } + return message; }; @@ -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) { @@ -150,7 +162,7 @@ module.exports = { type: 'input', name: 'body', message: messages.body, - default: getPreparedCommit('body'), + default: getPreparedCommit('body', config), }, { type: 'input', diff --git a/spec/questionsSpec.js b/spec/questionsSpec.js index 2b1b5b9..fee45b5 100644 --- a/spec/questionsSpec.js +++ b/spec/questionsSpec.js @@ -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'); }); @@ -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); });