diff --git a/index.js b/index.js index 323e2d0a..a89625da 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ 'use strict'; -const wrap = require('word-wrap'); const inquirer = require('inquirer'); +const wrap = require('word-wrap'); + inquirer.registerPrompt('limitedInput', require('./prompt/LimitedInput')); const MAX_SUBJECT_LENGTH = 50; @@ -10,104 +11,112 @@ 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) => { - inquirer.prompt([ + prompter (cz, commit) { + return inquirer.prompt([ { - type: 'list', - name: 'type', - message: 'Select the type of change that you\'re committing:', - choices: [ - { - name: 'feat: A new feature', - value: 'feat' - }, - { - name: 'fix: A bug fix', - value: 'fix' - }, - { - name: 'docs: Documentation only changes', - value: 'docs' - }, - { - name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)', - value: 'style' - }, - { - name: 'refactor: A code change that neither fixes a bug or adds a feature', - value: 'refactor' - }, - { - name: 'perf: A code change that improves performance', - value: 'perf' - }, - { - name: 'test: Adding missing tests', - value: 'test' - }, - { - name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation', - value: 'chore' - } - ] + choices: [ + { + name: 'feat: A new feature', + value: 'feat' + }, + { + name: 'fix: A bug fix', + value: 'fix' + }, + { + name: 'docs: Documentation only changes', + value: 'docs' + }, + { + name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)', + value: 'style' + }, + { + name: 'refactor: A code change that neither fixes a bug or adds a feature', + value: 'refactor' + }, + { + name: 'perf: A code change that improves performance', + value: 'perf' + }, + { + name: 'test: Adding missing tests', + value: 'test' + }, + { + name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation', + value: 'chore' + } + ], + message: 'Select the type of change that you\'re committing:', + name: 'type', + type: 'list' }, { - 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; + let subject; + + subject = input.trim(); + while (subject.endsWith('.')) { + subject = subject.substr(0, subject.length - 1).trim(); + } + + return 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:' + maxLength: MAX_SUBJECT_LENGTH, + message: 'Write a short, imperative mood description of the change:', + name: 'subject', + type: 'limitedInput', + validate: (input) => input.length >= MIN_SUBJECT_LENGTH || MIN_SUBJECT_LENGTH_ERROR_MESSAGE }, { - type: 'input', + message: 'Provide a longer description of the change:\n', name: 'body', - message: 'Provide a longer description of the change:\n' + type: 'input' }, { - type: 'input', + message: 'List any breaking changes:\n BREAKING CHANGE:', name: 'breaking', - message: 'List any breaking changes:\n BREAKING CHANGE:' + type: 'input' }, { - type: 'input', + message: 'Reference any task that this commit closes:\n Issues:', name: 'footer', - message: 'Reference any task that this commit closes:\n Issues:' + type: 'input' } ]) - .then((answers) => { - const wrapOptions = { - trim: true, - indent: '', - width: MAX_LINE_WIDTH - }; + .then((answers) => { + const wrapOptions = { + indent: '', + trim: true, + width: MAX_LINE_WIDTH + }; - const head = answers.type + ': ' + answers.subject; + const head = answers.type + ': ' + answers.subject; - // Wrap these lines at MAX_LINE_WIDTH characters - const body = wrap(answers.body, wrapOptions); - const breaking = wrap(answers.breaking, wrapOptions); - const footer = wrap(answers.footer, wrapOptions); + // Wrap these lines at MAX_LINE_WIDTH characters + const body = wrap(answers.body, wrapOptions); + const breaking = wrap(answers.breaking, wrapOptions); + const footer = wrap(answers.footer, wrapOptions); - let msg = head; + let msg; - if (body) { - msg += '\n\n' + body; - } + msg = head; - if (breaking) { - msg += '\n\n' + 'BREAKING CHANGE: ' + breaking; - } + if (body) { + msg += '\n\n' + body; + } - if (footer) { - msg += '\n\n' + 'Issues: ' + footer; - } + if (breaking) { + msg += '\n\nBREAKING CHANGE: ' + breaking; + } + + if (footer) { + msg += '\n\nIssues: ' + footer; + } - commit(msg); - }); + return commit(msg); + }); } -} +}; diff --git a/prompt/LimitedInput.js b/prompt/LimitedInput.js index 139ad276..cadf9ba2 100644 --- a/prompt/LimitedInput.js +++ b/prompt/LimitedInput.js @@ -1,13 +1,13 @@ -const inquirer = require('inquirer'); const util = require('util'); +const inquirer = require('inquirer'); -function LimitedInput() { - inquirer.prompt.prompts.input.apply(this, arguments); +const LimitedInput = function (...args) { + inquirer.prompt.prompts.input.apply(this, args); if (!this.opt.maxLength) { this.throwParamError('maxLength'); } - this.opt._message = this.opt.message; + this.originalMeassage = this.opt.message; this.spacer = new Array(this.opt.maxLength).fill('-').join(''); if (this.opt.leadingLabel) { @@ -22,22 +22,22 @@ function LimitedInput() { this.leadingLength = this.leadingLabel.length; this.updateMessage(); -} +}; util.inherits(LimitedInput, inquirer.prompt.prompts.input); -LimitedInput.prototype.updateMessage = function(e) { - this.opt.message = `${this.opt._message} +LimitedInput.prototype.updateMessage = function () { + this.opt.message = `${this.originalMeassage} [${this.spacer}] ${this.remainingChar()} remaining chars ${this.leadingLabel}`; }; -LimitedInput.prototype.remainingChar = function(e) { - return (this.opt.maxLength - this.leadingLength) - this.rl.line.length; +LimitedInput.prototype.remainingChar = function () { + return this.opt.maxLength - this.leadingLength - this.rl.line.length; }; -LimitedInput.prototype.onKeypress = function(e) { - if(this.rl.line.length > (this.opt.maxLength - this.leadingLength)) { +LimitedInput.prototype.onKeypress = function () { + if (this.rl.line.length > this.opt.maxLength - this.leadingLength) { this.rl.line = this.rl.line.slice(0, this.opt.maxLength - this.leadingLength); this.rl.cursor--; }