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

Commit

Permalink
style: align code to mailonline styleguide
Browse files Browse the repository at this point in the history
  • Loading branch information
brokenmass committed Oct 27, 2016
1 parent 2461888 commit bcac383
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 88 deletions.
163 changes: 86 additions & 77 deletions 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;
Expand All @@ -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);
});
}
}
};
22 changes: 11 additions & 11 deletions 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) {
Expand All @@ -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--;
}
Expand Down

0 comments on commit bcac383

Please sign in to comment.