diff --git a/lib/cli.js b/lib/cli.js index 221088a3..b8b4747c 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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); diff --git a/lib/createPrompter.js b/lib/createPrompter.js index 2343c9ea..587e40f6 100644 --- a/lib/createPrompter.js +++ b/lib/createPrompter.js @@ -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 = { diff --git a/lib/defaults.js b/lib/defaults.js index 39393a70..ed49470c 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -21,7 +21,7 @@ const types = { }, fix: { description: 'A bug fix', - emoji: '🐜', + emoji: '🐛', value: 'fix' }, perf: { @@ -84,7 +84,7 @@ const questions = [ module.exports = { list, - maxMessageLength: 50, + maxMessageLength: 64, minMessageLength: 3, questions, scopes, diff --git a/lib/formatCommitMessage.js b/lib/formatCommitMessage.js new file mode 100644 index 00000000..f2d648db --- /dev/null +++ b/lib/formatCommitMessage.js @@ -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;