Skip to content

Commit

Permalink
WIP: predefined scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsMissEm committed Jul 29, 2020
1 parent 10fffab commit 21480b0
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 215 deletions.
357 changes: 176 additions & 181 deletions engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,202 +101,197 @@ module.exports = function(options, inquirer) {
// See inquirer.js docs for specifics.
// You can also opt to use another input
// collection library if you prefer.
inquirer
.prompt([
{
type: 'list',
name: 'type',
message: "Select the type of change that you're committing:",
choices: typeChoices,
default: options.defaultType
},
{
type: 'list',
name: 'scopeChoices',
message:
'What is the scope of this change' +
(defaultScopeChoice > 0 ? '' : ' (press enter to skip)') +
':',
choices: scopeChoices,
default: function() {
return (
scopeChoices[defaultScopeChoice] &&
scopeChoices[defaultScopeChoice].value
);
},
filter: function(value) {
return options.disableScopeLowerCase
? value.trim()
: value.trim().toLowerCase();
},
when: function() {
return hasPredefinedScopes;
}
},
{
type: 'input',
name: 'scope',
message:
'What is the scope of this change (e.g. component or file name): ' +
chalk.grey('(press enter to skip)') +
'\n> ',
default: options.defaultScope,
filter: function(value) {
return options.disableScopeLowerCase
? value.trim()
: value.trim().toLowerCase();
},
when: function(answers) {
return (
(answers.scopeChoices &&
answers.scopeChoices === otherScopeChoice) ||
!hasPredefinedScopes
);
}
},
{
type: 'input',
name: 'subject',
message: function(answers) {
return (
'Write a short, imperative tense description of the change (max ' +
maxSummaryLength(options, answers) +
' chars):\n'
);
},
default: options.defaultSubject,
validate: function(subject, answers) {
var filteredSubject = filterSubject(subject);
return filteredSubject.length == 0
? 'subject is required'
: filteredSubject.length <= maxSummaryLength(options, answers)
? true
: 'Subject length must be less than or equal to ' +
maxSummaryLength(options, answers) +
' characters. Current length is ' +
filteredSubject.length +
' characters.';
},
transformer: function(subject, answers) {
var filteredSubject = filterSubject(subject);
var color =
filteredSubject.length <= maxSummaryLength(options, answers)
? chalk.green
: chalk.red;
return color('(' + filteredSubject.length + ') ' + subject);
},
filter: function(subject) {
return filterSubject(subject);
}

// FIXME: Avoid changing the entire file because of the longer variable of inquirer vs cz
var cz = inquirer;

cz.prompt([
{
type: 'list',
name: 'type',
message: "Select the type of change that you're committing:",
choices: typeChoices,
default: options.defaultType
},
{
type: 'list',
name: 'scopeChoices',
message:
'What is the scope of this change' +
(defaultScopeChoice > 0 ? '' : ' (press enter to skip)') +
':',
choices: scopeChoices,
default: function() {
return (
scopeChoices[defaultScopeChoice] &&
scopeChoices[defaultScopeChoice].value
);
},
{
type: 'input',
name: 'body',
message:
'Provide a longer description of the change: (press enter to skip)\n',
default: options.defaultBody
filter: function(value) {
return options.disableScopeLowerCase
? value.trim()
: value.trim().toLowerCase();
},
{
type: 'confirm',
name: 'isBreaking',
message: 'Are there any breaking changes?',
default: false
when: function() {
return hasPredefinedScopes;
}
},
{
type: 'input',
name: 'scope',
message:
'What is the scope of this change (e.g. component or file name): ' +
chalk.grey('(press enter to skip)') +
'\n> ',
default: options.defaultScope,
filter: function(value) {
return options.disableScopeLowerCase
? value.trim()
: value.trim().toLowerCase();
},
{
type: 'input',
name: 'breakingBody',
default: '-',
message:
'A BREAKING CHANGE commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return answers.isBreaking && !answers.body;
},
validate: function(breakingBody, answers) {
return (
breakingBody.trim().length > 0 ||
'Body is required for BREAKING CHANGE'
);
}
when: function(answers) {
return (
(answers.scopeChoices &&
answers.scopeChoices === otherScopeChoice) ||
!hasPredefinedScopes
);
}
},
{
type: 'input',
name: 'subject',
message: function(answers) {
return (
'Write a short, imperative tense description of the change (max ' +
maxSummaryLength(options, answers) +
' chars):\n'
);
},
{
type: 'input',
name: 'breaking',
message: 'Describe the breaking changes:\n',
when: function(answers) {
return answers.isBreaking;
}
default: options.defaultSubject,
validate: function(subject, answers) {
var filteredSubject = filterSubject(subject);
return filteredSubject.length == 0
? 'subject is required'
: filteredSubject.length <= maxSummaryLength(options, answers)
? true
: 'Subject length must be less than or equal to ' +
maxSummaryLength(options, answers) +
' characters. Current length is ' +
filteredSubject.length +
' characters.';
},

{
type: 'confirm',
name: 'isIssueAffected',
message: 'Does this change affect any open issues?',
default: options.defaultIssues ? true : false
transformer: function(subject, answers) {
var filteredSubject = filterSubject(subject);
var color =
filteredSubject.length <= maxSummaryLength(options, answers)
? chalk.green
: chalk.red;
return color('(' + filteredSubject.length + ') ' + subject);
},
{
type: 'input',
name: 'issuesBody',
default: '-',
message:
'If issues are closed, the commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return (
answers.isIssueAffected &&
!answers.body &&
!answers.breakingBody
);
}
filter: function(subject) {
return filterSubject(subject);
}
},
{
type: 'input',
name: 'body',
message:
'Provide a longer description of the change: (press enter to skip)\n',
default: options.defaultBody
},
{
type: 'confirm',
name: 'isBreaking',
message: 'Are there any breaking changes?',
default: false
},
{
type: 'input',
name: 'breakingBody',
default: '-',
message:
'A BREAKING CHANGE commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return answers.isBreaking && !answers.body;
},
{
type: 'input',
name: 'issues',
message: 'Add issue references (e.g. "fix #123", "re #123".):\n',
when: function(answers) {
return answers.isIssueAffected;
},
default: options.defaultIssues ? options.defaultIssues : undefined
validate: function(breakingBody, answers) {
return (
breakingBody.trim().length > 0 ||
'Body is required for BREAKING CHANGE'
);
}
},
{
type: 'input',
name: 'breaking',
message: 'Describe the breaking changes:\n',
when: function(answers) {
return answers.isBreaking;
}
])
.then(function(answers) {
var wrapOptions = {
trim: true,
cut: false,
newline: '\n',
indent: '',
width: options.maxLineWidth
};
},

// parentheses are only needed when a scope is present
var scope;
if (
answers.scopeChoices &&
answers.scopeChoices !== otherScopeChoice
) {
scope = '(' + answers.scopeChoices + ')';
} else {
scope = answers.scope ? '(' + answers.scope + ')' : '';
{
type: 'confirm',
name: 'isIssueAffected',
message: 'Does this change affect any open issues?',
default: options.defaultIssues ? true : false
},
{
type: 'input',
name: 'issuesBody',
default: '-',
message:
'If issues are closed, the commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return (
answers.isIssueAffected && !answers.body && !answers.breakingBody
);
}
},
{
type: 'input',
name: 'issues',
message: 'Add issue references (e.g. "fix #123", "re #123".):\n',
when: function(answers) {
return answers.isIssueAffected;
},
default: options.defaultIssues ? options.defaultIssues : undefined
}
]).then(function(answers) {
var wrapOptions = {
trim: true,
cut: false,
newline: '\n',
indent: '',
width: options.maxLineWidth
};

// parentheses are only needed when a scope is present
var scope;
if (answers.scopeChoices && answers.scopeChoices !== otherScopeChoice) {
scope = '(' + answers.scopeChoices + ')';
} else {
scope = answers.scope ? '(' + answers.scope + ')' : '';
}

// Hard limit this line in the validate
var head = answers.type + scope + ': ' + answers.subject;
// Hard limit this line in the validate
var head = answers.type + scope + ': ' + answers.subject;

// Wrap these lines at options.maxLineWidth characters
var body = answers.body ? wrap(answers.body, wrapOptions) : false;
// Wrap these lines at options.maxLineWidth characters
var body = answers.body ? wrap(answers.body, wrapOptions) : false;

// Apply breaking change prefix, removing it if already present
var breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking
? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '')
: '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;
// Apply breaking change prefix, removing it if already present
var breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking
? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '')
: '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;

var issues = answers.issues
? wrap(answers.issues, wrapOptions)
: false;
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;

var message = filter([head, body, breaking, issues]).join('\n\n');
commit(message);
});
var message = filter([head, body, breaking, issues]).join('\n\n');
commit(message);
});
}
};
};
Loading

0 comments on commit 21480b0

Please sign in to comment.