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: only use prepared commit message if run as hook #141

Open
wants to merge 1 commit 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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ const readConfigFile = () => {
};

module.exports = {
prompter(cz, commit) {
prompter(cz, commit, isStandalone = false) {
const config = readConfigFile();
config.subjectLimit = config.subjectLimit || 100;
log.info('All lines except first will be wrapped after 100 characters.');

const questions = require('./questions').getQuestions(config, cz);
const isHook = process.argv.includes('--hook');

const questions = require('./questions').getQuestions(config, cz, isStandalone || isHook);

cz.prompt(questions).then(answers => {
if (answers.confirmCommit === 'edit') {
Expand Down
6 changes: 3 additions & 3 deletions questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const getPreparedCommit = context => {
};

module.exports = {
getQuestions(config, cz) {
getQuestions(config, cz, isStandaloneOrHook = false) {
// normalize config optional options
const scopeOverrides = config.scopeOverrides || {};
const messages = config.messages || {};
Expand Down Expand Up @@ -132,7 +132,7 @@ module.exports = {
type: 'input',
name: 'subject',
message: messages.subject,
default: getPreparedCommit('subject'),
default: isStandaloneOrHook ? getPreparedCommit('subject') : null,
validate(value) {
const limit = config.subjectLimit || 100;
if (value.length > limit) {
Expand All @@ -150,7 +150,7 @@ module.exports = {
type: 'input',
name: 'body',
message: messages.body,
default: getPreparedCommit('body'),
default: isStandaloneOrHook ? getPreparedCommit('body') : null,
},
{
type: 'input',
Expand Down
19 changes: 10 additions & 9 deletions spec/questionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('cz-customizable', () => {
Separator: jasmine.createSpy(),
};

const getQuestion = number => questions.getQuestions(config, mockedCz)[number - 1];
const getQuestion = (number, isStandaloneOrHook = false) =>
questions.getQuestions(config, mockedCz, isStandaloneOrHook)[number - 1];

it('should array of questions be returned', () => {
config = {
Expand Down Expand Up @@ -277,29 +278,29 @@ describe('cz-customizable', () => {

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);
expect(getQuestion(5, true).default).toEqual(null);
expect(getQuestion(6, true).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);
expect(getQuestion(5, true).default).toEqual(null);
expect(getQuestion(6, true).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);
expect(getQuestion(5, true).default).toEqual('my commit');
expect(getQuestion(6, true).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`);
expect(getQuestion(5, true).default).toEqual('my commit');
expect(getQuestion(6, true).default).toEqual(`message|is on several lines`);
});
});
});
2 changes: 1 addition & 1 deletion standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ const commit = commitMessage => {
}
};

app.prompter(inquirer, commit);
app.prompter(inquirer, commit, true);