Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: --non-limit allow the subject to have any length #498

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/LimitWarningInputPrompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const LimitedInputPrompt = require('./LimitedInputPrompt');

class LimitWarningInputPrompt extends LimitedInputPrompt {
// eslint-disable-next-line class-methods-use-this
generateSpacer () {
return 'No limit mode';
}

onKeypress () {
this.render();
}

getCharsLeftPlainText () {
return `${this.leadingLength + this.rl.line.length}/${this.opt.maxLength}`;
}
}

module.exports = LimitWarningInputPrompt;
18 changes: 14 additions & 4 deletions lib/LimitedInputPrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LimitedInputPrompt extends InputPrompt {
this.throwParamError('maxLength');
}
this.originalMessage = this.opt.message;
this.spacer = new Array(this.opt.maxLength).fill('-').join('');
this.spacer = this.generateSpacer();

if (this.opt.leadingLabel) {
if (typeof this.opt.leadingLabel === 'function') {
Expand All @@ -24,6 +24,10 @@ class LimitedInputPrompt extends InputPrompt {
this.leadingLength = this.leadingLabel.length;
}

generateSpacer () {
return new Array(this.opt.maxLength).fill('-').join('');
}

remainingChar () {
return this.opt.maxLength - this.leadingLength - this.rl.line.length;
}
Expand All @@ -39,16 +43,22 @@ class LimitedInputPrompt extends InputPrompt {

getCharsLeftText () {
const chars = this.remainingChar();
const text = this.getCharsLeftPlainText(chars);

if (chars > 15) {
return chalk.green(`${chars} chars left`);
return chalk.green(text);
} else if (chars > 5) {
return chalk.yellow(`${chars} chars left`);
return chalk.yellow(text);
} else {
return chalk.red(`${chars} chars left`);
return chalk.red(text);
}
}

// eslint-disable-next-line class-methods-use-this
getCharsLeftPlainText (chars) {
return `${chars} chars left`;
}

render (error) {
let bottomContent = '';
let message = this.getQuestion();
Expand Down
12 changes: 7 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const runInteractiveQuestions = require('./runInteractiveQuestions');
const runNonInteractiveMode = require('./runNonInteractiveMode');
const formatCommitMessage = require('./formatCommitMessage');
const getGitDir = require('./util/getGitDir');
const ObjectBuilder = require('./util/ObjectBuilder');

// eslint-disable-next-line no-process-env
const executeCommand = (command, env = process.env) => {
Expand All @@ -31,11 +32,12 @@ const main = async () => {

let state = null;

if (cliOptions.disableEmoji) {
state = createState({disableEmoji: cliOptions.disableEmoji});
} else {
state = createState();
}
const configFromFlags = new ObjectBuilder()
.if(cliOptions.disableEmoji).add({disableEmoji: true})
.if(cliOptions.nonLimit).add({withoutLimit: true})
.build();

state = createState(configFromFlags);

if (cliOptions.dryRun) {
// eslint-disable-next-line no-console
Expand Down
6 changes: 5 additions & 1 deletion lib/parseArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const helpScreen = `
--disable-emoji don't add emoji to commit title
--format custom formatting options for subject
--non-interactive run git-cz in non-interactive mode
--non-limit allow the subject to have any length

non-interactive mode options:
--type type of the commit, defaults to "chore"
Expand All @@ -39,6 +40,7 @@ const parseArgs = () => {
breaking,
issues,
lerna,
'non-limit': nonLimit,
scope,
subject,
type,
Expand All @@ -58,7 +60,8 @@ const parseArgs = () => {
'disable-emoji',
'non-interactive',
'hook',
'dry-run'
'dry-run',
'non-limit'
],
string: [
'format',
Expand Down Expand Up @@ -89,6 +92,7 @@ const parseArgs = () => {
help,
hook,
nonInteractive,
nonLimit,
version
};

Expand Down
2 changes: 1 addition & 1 deletion lib/questions/subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports.createQuestion = (state) => {
maxLength: config.maxMessageLength - 3,
message: 'Write a short, imperative mood description of the change:',
name: 'subject',
type: 'limitedInput',
type: config.withoutLimit ? 'limitWarningInput' : 'limitedInput',
validate: (input) => input.length >= config.minMessageLength || minTitleLengthErrorMessage
};

Expand Down
2 changes: 2 additions & 0 deletions lib/runInteractiveQuestions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const inquirer = require('inquirer');
const AutocompletePrompt = require('inquirer-list-search-prompt');
const LimitedInputPrompt = require('./LimitedInputPrompt');
const LimitWarningInputPrompt = require('./LimitWarningInputPrompt');
const createQuestions = require('./createQuestions');

inquirer.registerPrompt('limitedInput', LimitedInputPrompt);
inquirer.registerPrompt('limitWarningInput', LimitWarningInputPrompt);
inquirer.registerPrompt('autocomplete', AutocompletePrompt);

// if (IS_LERNA_PROJECT) {
Expand Down
40 changes: 40 additions & 0 deletions lib/util/ObjectBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class ObjectBuilder {
constructor (initial) {
this.currentObject = initial || {};
}

/**
* @param {boolean} condition
* @returns {{ add: ConditionalObjectBuilder["add"] }}
*/
if (condition) {
return {
add: (mappedValues) => {
if (condition) {
return this.add(mappedValues);
}

return this;
}
};
}

/**
* @param {{ [key: string]: value }} mappedValues
* @returns {ConditionalObjectBuilder}
*/
add (mappedValues) {
Object.keys(mappedValues)
.forEach((key) => {
this.currentObject[key] = mappedValues[key];
});

return this;
}

build () {
return this.currentObject;
}
}

module.exports = ObjectBuilder;