From 9f41dbf16038b06cea6f79f4299e8e881390a29b Mon Sep 17 00:00:00 2001 From: Iheb KHEMISSI Date: Thu, 27 Oct 2016 16:30:18 +0100 Subject: [PATCH] feat: Check commit subject for minimum length Do not allow commit subjects having less than 3 characters. This check do not include the type (e.g. 'feat', 'chore', ...) --- README.md | 2 +- index.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4f8bce65..a1dbfaf9 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Issues: MOL-1234 The **header** is the only mandatory part of the commit message. The first line (type + subject) is limited to 50 characters **[enforced]** + Any other line should be limited to 72 character **[automatic wrapping]** This allows the message to be easier to read on GitHub as well as in various git tools. @@ -87,7 +88,6 @@ Must be one of the following: The subject contains succinct description of the change: * Use the imperative, present tense: "change" not "changed" nor "changes" -* Lowercase only **[automatic fix]** * No dot (.) at the end. ### Body diff --git a/index.js b/index.js index d6700eee..323e2d0a 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ inquirer.registerPrompt('limitedInput', require('./prompt/LimitedInput')); const MAX_SUBJECT_LENGTH = 50; const MAX_LINE_WIDTH = 72; +const MIN_SUBJECT_LENGTH = 3; +const MIN_SUBJECT_LENGTH_ERROR_MESSAGE = `The subject must have at least ${MIN_SUBJECT_LENGTH} characters`; module.exports = { prompter: (cz, commit) => { @@ -53,6 +55,11 @@ module.exports = { type: 'limitedInput', name: 'subject', maxLength: MAX_SUBJECT_LENGTH, + filter: (input) => { + const subject = input.trim(); + return subject.endsWith('.') ? subject.substr(0, subject.length - 1).trim() : subject; + }, + validate: (input) => input.length >= MIN_SUBJECT_LENGTH || MIN_SUBJECT_LENGTH_ERROR_MESSAGE, leadingLabel: (answers) => `${answers.type}:`, message: 'Write a short, imperative mood description of the change:' }, @@ -64,12 +71,12 @@ module.exports = { { type: 'input', name: 'breaking', - message: 'List any breaking changes:\nBREAKING CHANGE: ' + message: 'List any breaking changes:\n BREAKING CHANGE:' }, { type: 'input', name: 'footer', - message: 'Reference any task that this commit closes:\nIssues: ' + message: 'Reference any task that this commit closes:\n Issues:' } ]) .then((answers) => { @@ -79,7 +86,7 @@ module.exports = { width: MAX_LINE_WIDTH }; - const head = (answers.type + ': ' + answers.subject.trim()).toLowerCase(); + const head = answers.type + ': ' + answers.subject; // Wrap these lines at MAX_LINE_WIDTH characters const body = wrap(answers.body, wrapOptions);