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

Commit bcac383

Browse files
committed
style: align code to mailonline styleguide
1 parent 2461888 commit bcac383

File tree

2 files changed

+97
-88
lines changed

2 files changed

+97
-88
lines changed

index.js

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
const wrap = require('word-wrap');
43
const inquirer = require('inquirer');
4+
const wrap = require('word-wrap');
5+
56
inquirer.registerPrompt('limitedInput', require('./prompt/LimitedInput'));
67

78
const MAX_SUBJECT_LENGTH = 50;
@@ -10,104 +11,112 @@ const MIN_SUBJECT_LENGTH = 3;
1011
const MIN_SUBJECT_LENGTH_ERROR_MESSAGE = `The subject must have at least ${MIN_SUBJECT_LENGTH} characters`;
1112

1213
module.exports = {
13-
prompter: (cz, commit) => {
14-
inquirer.prompt([
14+
prompter (cz, commit) {
15+
return inquirer.prompt([
1516
{
16-
type: 'list',
17-
name: 'type',
18-
message: 'Select the type of change that you\'re committing:',
19-
choices: [
20-
{
21-
name: 'feat: A new feature',
22-
value: 'feat'
23-
},
24-
{
25-
name: 'fix: A bug fix',
26-
value: 'fix'
27-
},
28-
{
29-
name: 'docs: Documentation only changes',
30-
value: 'docs'
31-
},
32-
{
33-
name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)',
34-
value: 'style'
35-
},
36-
{
37-
name: 'refactor: A code change that neither fixes a bug or adds a feature',
38-
value: 'refactor'
39-
},
40-
{
41-
name: 'perf: A code change that improves performance',
42-
value: 'perf'
43-
},
44-
{
45-
name: 'test: Adding missing tests',
46-
value: 'test'
47-
},
48-
{
49-
name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation',
50-
value: 'chore'
51-
}
52-
]
17+
choices: [
18+
{
19+
name: 'feat: A new feature',
20+
value: 'feat'
21+
},
22+
{
23+
name: 'fix: A bug fix',
24+
value: 'fix'
25+
},
26+
{
27+
name: 'docs: Documentation only changes',
28+
value: 'docs'
29+
},
30+
{
31+
name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)',
32+
value: 'style'
33+
},
34+
{
35+
name: 'refactor: A code change that neither fixes a bug or adds a feature',
36+
value: 'refactor'
37+
},
38+
{
39+
name: 'perf: A code change that improves performance',
40+
value: 'perf'
41+
},
42+
{
43+
name: 'test: Adding missing tests',
44+
value: 'test'
45+
},
46+
{
47+
name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation',
48+
value: 'chore'
49+
}
50+
],
51+
message: 'Select the type of change that you\'re committing:',
52+
name: 'type',
53+
type: 'list'
5354
},
5455
{
55-
type: 'limitedInput',
56-
name: 'subject',
57-
maxLength: MAX_SUBJECT_LENGTH,
5856
filter: (input) => {
59-
const subject = input.trim();
60-
return subject.endsWith('.') ? subject.substr(0, subject.length - 1).trim() : subject;
57+
let subject;
58+
59+
subject = input.trim();
60+
while (subject.endsWith('.')) {
61+
subject = subject.substr(0, subject.length - 1).trim();
62+
}
63+
64+
return subject;
6165
},
62-
validate: (input) => input.length >= MIN_SUBJECT_LENGTH || MIN_SUBJECT_LENGTH_ERROR_MESSAGE,
6366
leadingLabel: (answers) => `${answers.type}:`,
64-
message: 'Write a short, imperative mood description of the change:'
67+
maxLength: MAX_SUBJECT_LENGTH,
68+
message: 'Write a short, imperative mood description of the change:',
69+
name: 'subject',
70+
type: 'limitedInput',
71+
validate: (input) => input.length >= MIN_SUBJECT_LENGTH || MIN_SUBJECT_LENGTH_ERROR_MESSAGE
6572
},
6673
{
67-
type: 'input',
74+
message: 'Provide a longer description of the change:\n',
6875
name: 'body',
69-
message: 'Provide a longer description of the change:\n'
76+
type: 'input'
7077
},
7178
{
72-
type: 'input',
79+
message: 'List any breaking changes:\n BREAKING CHANGE:',
7380
name: 'breaking',
74-
message: 'List any breaking changes:\n BREAKING CHANGE:'
81+
type: 'input'
7582
},
7683
{
77-
type: 'input',
84+
message: 'Reference any task that this commit closes:\n Issues:',
7885
name: 'footer',
79-
message: 'Reference any task that this commit closes:\n Issues:'
86+
type: 'input'
8087
}
8188
])
82-
.then((answers) => {
83-
const wrapOptions = {
84-
trim: true,
85-
indent: '',
86-
width: MAX_LINE_WIDTH
87-
};
89+
.then((answers) => {
90+
const wrapOptions = {
91+
indent: '',
92+
trim: true,
93+
width: MAX_LINE_WIDTH
94+
};
8895

89-
const head = answers.type + ': ' + answers.subject;
96+
const head = answers.type + ': ' + answers.subject;
9097

91-
// Wrap these lines at MAX_LINE_WIDTH characters
92-
const body = wrap(answers.body, wrapOptions);
93-
const breaking = wrap(answers.breaking, wrapOptions);
94-
const footer = wrap(answers.footer, wrapOptions);
98+
// Wrap these lines at MAX_LINE_WIDTH characters
99+
const body = wrap(answers.body, wrapOptions);
100+
const breaking = wrap(answers.breaking, wrapOptions);
101+
const footer = wrap(answers.footer, wrapOptions);
95102

96-
let msg = head;
103+
let msg;
97104

98-
if (body) {
99-
msg += '\n\n' + body;
100-
}
105+
msg = head;
101106

102-
if (breaking) {
103-
msg += '\n\n' + 'BREAKING CHANGE: ' + breaking;
104-
}
107+
if (body) {
108+
msg += '\n\n' + body;
109+
}
105110

106-
if (footer) {
107-
msg += '\n\n' + 'Issues: ' + footer;
108-
}
111+
if (breaking) {
112+
msg += '\n\nBREAKING CHANGE: ' + breaking;
113+
}
114+
115+
if (footer) {
116+
msg += '\n\nIssues: ' + footer;
117+
}
109118

110-
commit(msg);
111-
});
119+
return commit(msg);
120+
});
112121
}
113-
}
122+
};

prompt/LimitedInput.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const inquirer = require('inquirer');
21
const util = require('util');
2+
const inquirer = require('inquirer');
33

4-
function LimitedInput() {
5-
inquirer.prompt.prompts.input.apply(this, arguments);
4+
const LimitedInput = function (...args) {
5+
inquirer.prompt.prompts.input.apply(this, args);
66

77
if (!this.opt.maxLength) {
88
this.throwParamError('maxLength');
99
}
10-
this.opt._message = this.opt.message;
10+
this.originalMeassage = this.opt.message;
1111
this.spacer = new Array(this.opt.maxLength).fill('-').join('');
1212

1313
if (this.opt.leadingLabel) {
@@ -22,22 +22,22 @@ function LimitedInput() {
2222

2323
this.leadingLength = this.leadingLabel.length;
2424
this.updateMessage();
25-
}
25+
};
2626

2727
util.inherits(LimitedInput, inquirer.prompt.prompts.input);
2828

29-
LimitedInput.prototype.updateMessage = function(e) {
30-
this.opt.message = `${this.opt._message}
29+
LimitedInput.prototype.updateMessage = function () {
30+
this.opt.message = `${this.originalMeassage}
3131
[${this.spacer}] ${this.remainingChar()} remaining chars
3232
${this.leadingLabel}`;
3333
};
3434

35-
LimitedInput.prototype.remainingChar = function(e) {
36-
return (this.opt.maxLength - this.leadingLength) - this.rl.line.length;
35+
LimitedInput.prototype.remainingChar = function () {
36+
return this.opt.maxLength - this.leadingLength - this.rl.line.length;
3737
};
3838

39-
LimitedInput.prototype.onKeypress = function(e) {
40-
if(this.rl.line.length > (this.opt.maxLength - this.leadingLength)) {
39+
LimitedInput.prototype.onKeypress = function () {
40+
if (this.rl.line.length > this.opt.maxLength - this.leadingLength) {
4141
this.rl.line = this.rl.line.slice(0, this.opt.maxLength - this.leadingLength);
4242
this.rl.cursor--;
4343
}

0 commit comments

Comments
 (0)