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: support prepared commit message #122

Merged
merged 5 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 24 additions & 0 deletions questions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const _ = require('lodash');
const buildCommit = require('./buildCommit');
const log = require('./logger');
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -127,6 +150,7 @@ module.exports = {
type: 'input',
name: 'body',
message: messages.body,
default: getPreparedCommit('body'),
},
{
type: 'input',
Expand Down
42 changes: 42 additions & 0 deletions spec/questionsSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable nada/path-case */
const fs = require('fs');
const questions = require('../questions.js');

describe('cz-customizable', () => {
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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`);
});
});
});