diff --git a/questions.js b/questions.js index 1d5d830..b0f0fb6 100644 --- a/questions.js +++ b/questions.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const _ = require('lodash'); const buildCommit = require('./buildCommit'); const log = require('./logger'); @@ -18,6 +19,27 @@ const isValidateTicketNo = (value, config) => { return true; }; +const getPreparedCommit = context => { + let message = null; + if (fs.existsSync('./.git/COMMIT_EDITMSG')) { + let preparedCommit = fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8'); + preparedCommit = preparedCommit + .replace(/^#.*/gm, '') + .replace(/^\s*[\r\n]/gm, '') + .replace(/[\r\n]$/, '') + .split(/\r\n|\r|\n/); + + if (preparedCommit.length && preparedCommit[0]) { + if (context === 'subject') [message] = preparedCommit; + else if (context === 'body' && preparedCommit.length > 1) { + preparedCommit.shift(); + message = preparedCommit.join('|'); + } + } + } + return message; +}; + module.exports = { getQuestions(config, cz) { // normalize config optional options @@ -110,6 +132,7 @@ module.exports = { type: 'input', name: 'subject', message: messages.subject, + default: getPreparedCommit('subject'), validate(value) { const limit = config.subjectLimit || 100; if (value.length > limit) { @@ -127,6 +150,7 @@ module.exports = { type: 'input', name: 'body', message: messages.body, + default: getPreparedCommit('body'), }, { type: 'input', diff --git a/spec/questionsSpec.js b/spec/questionsSpec.js index 02377d8..2b1b5b9 100644 --- a/spec/questionsSpec.js +++ b/spec/questionsSpec.js @@ -1,3 +1,5 @@ +/* eslint-disable nada/path-case */ +const fs = require('fs'); const questions = require('../questions.js'); describe('cz-customizable', () => { @@ -62,6 +64,7 @@ describe('cz-customizable', () => { // question 5 - SUBJECT expect(getQuestion(5).name).toEqual('subject'); expect(getQuestion(5).type).toEqual('input'); + expect(getQuestion(5).default).toEqual(null); expect(getQuestion(5).message).toMatch(/IMPERATIVE tense description/); expect(getQuestion(5).filter('Subject')).toEqual('subject'); expect(getQuestion(5).validate('bad subject that exceed limit for 6 characters')).toEqual('Exceed limit: 40'); @@ -70,6 +73,7 @@ describe('cz-customizable', () => { // question 6 - BODY expect(getQuestion(6).name).toEqual('body'); expect(getQuestion(6).type).toEqual('input'); + expect(getQuestion(6).default).toEqual(null); // question 7 - BREAKING CHANGE expect(getQuestion(7).name).toEqual('breaking'); @@ -260,4 +264,42 @@ describe('cz-customizable', () => { }); }); }); + + describe('commit already prepared', () => { + let existsSync; + let readFileSync; + + beforeEach(() => { + config = {}; + existsSync = spyOn(fs, 'existsSync'); + readFileSync = spyOn(fs, 'readFileSync'); + }); + + it('should ignore if there is no prepared commit file', () => { + existsSync.andReturn(false); + expect(getQuestion(5).default).toEqual(null); + expect(getQuestion(6).default).toEqual(null); + }); + + it('should ignore an empty prepared commit', () => { + existsSync.andReturn(true); + readFileSync.andReturn(''); + expect(getQuestion(5).default).toEqual(null); + expect(getQuestion(6).default).toEqual(null); + }); + + it('should take a single line commit as the subject', () => { + existsSync.andReturn(true); + readFileSync.andReturn('my commit'); + expect(getQuestion(5).default).toEqual('my commit'); + expect(getQuestion(6).default).toEqual(null); + }); + + it('should split multi line commit between the subject and the body', () => { + existsSync.andReturn(true); + readFileSync.andReturn('my commit\nmessage\n\nis on several lines'); + expect(getQuestion(5).default).toEqual('my commit'); + expect(getQuestion(6).default).toEqual(`message|is on several lines`); + }); + }); });