Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
feat: create formatMessage() function
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 5, 2018
1 parent f942494 commit d1c6755
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
5 changes: 3 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const signale = require('signale');
const createState = require('./createState');
const runInteractiveQuestions = require('./runInteractiveQuestions');
const formatCommitMessage = require('./formatCommitMessage');

const main = async () => {
try {
const state = createState();
const answers = await runInteractiveQuestions(state);
const message = formatCommitMessage(state);

console.log(state);

console.log(message);
console.log(answers);
} catch (error) {
signale.fatal(error);
Expand Down
15 changes: 0 additions & 15 deletions lib/createPrompter.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
const fs = require('fs');
const wrap = require('word-wrap');
const runInteractiveQuestions = require('./runInteractiveQuestions');

const MAX_LINE_WIDTH = 72;


/*
const makeAffectsLine = function (answers) {
const selectedPackages = answers.packages;
if (selectedPackages && selectedPackages.length) {
return `\naffects: ${selectedPackages.join(', ')}`;
}
return '';
};
*/

module.exports = (state) => {
const prompter = {
Expand Down
4 changes: 2 additions & 2 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const types = {
},
fix: {
description: 'A bug fix',
emoji: '🐜',
emoji: '🐛',
value: 'fix'
},
perf: {
Expand Down Expand Up @@ -84,7 +84,7 @@ const questions = [

module.exports = {
list,
maxMessageLength: 50,
maxMessageLength: 64,
minMessageLength: 3,
questions,
scopes,
Expand Down
57 changes: 57 additions & 0 deletions lib/formatCommitMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const wrap = require('word-wrap');

const MAX_LINE_WIDTH = 72;

const makeAffectsLine = function (answers) {
const selectedPackages = answers.packages;

if (selectedPackages && selectedPackages.length) {
return `\naffects: ${selectedPackages.join(', ')}`;
}

return '';
};

const formatCommitMessage = (state) => {
const {config, answers} = state;
const wrapOptions = {
indent: '',
trim: true,
width: MAX_LINE_WIDTH
};

const emoji = config.types[answers.type].emoji;
const emojiPrefix = emoji ? emoji + ' ' : '';

let scope = '';

if (answers.scope && (answers.scope !== 'none')) {
scope = `(${answers.scope})`;
}

const head = answers.type + scope + ': ' + emojiPrefix + answers.subject;
const affectsLine = makeAffectsLine(answers);

// Wrap these lines at MAX_LINE_WIDTH character
const body = wrap(answers.body + affectsLine, wrapOptions);
const breaking = wrap(answers.breaking, wrapOptions);
const issues = wrap(answers.issues, wrapOptions);

let msg = head;

if (body) {
msg += '\n\n' + body;
}

if (breaking) {
msg += '\n\nBREAKING CHANGE: ' + breaking;
}

if (issues) {
msg += '\n\nIssues: ' + issues;
}

return msg;
};

module.exports = formatCommitMessage;

0 comments on commit d1c6755

Please sign in to comment.