From 31d98c229a2f0fd23332705d2fcaca8da94b3c0a Mon Sep 17 00:00:00 2001 From: dengyongchi Date: Fri, 8 Mar 2024 15:42:50 +0800 Subject: [PATCH] feat: add skipQuestions config to skip some questions fix #218 --- engine.js | 243 +++++++++++++++++++++++++++--------------------------- index.js | 3 +- 2 files changed, 124 insertions(+), 122 deletions(-) diff --git a/engine.js b/engine.js index 24a42b7c..dc558ebf 100644 --- a/engine.js +++ b/engine.js @@ -46,7 +46,127 @@ module.exports = function(options) { value: key }; }); + const initQuestions = [ + { + type: 'list', + name: 'type', + message: "Select the type of change that you're committing:", + choices: choices, + default: options.defaultType + }, + { + type: 'input', + name: 'scope', + message: + 'What is the scope of this change (e.g. component or file name): (press enter to skip)', + default: options.defaultScope, + filter: function(value) { + return options.disableScopeLowerCase + ? value.trim() + : value.trim().toLowerCase(); + } + }, + { + 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, options.disableSubjectLowerCase); + 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, options.disableSubjectLowerCase); + var color = + filteredSubject.length <= maxSummaryLength(options, answers) + ? chalk.green + : chalk.red; + return color('(' + filteredSubject.length + ') ' + subject); + }, + filter: function(subject) { + return filterSubject(subject, options.disableSubjectLowerCase); + } + }, + { + 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; + }, + 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; + } + }, + { + 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 + } + ]; return { // When a user runs `git cz`, prompter will // be executed. We pass you cz, which currently @@ -67,127 +187,8 @@ module.exports = function(options) { // See inquirer.js docs for specifics. // You can also opt to use another input // collection library if you prefer. - cz.prompt([ - { - type: 'list', - name: 'type', - message: "Select the type of change that you're committing:", - choices: choices, - default: options.defaultType - }, - { - type: 'input', - name: 'scope', - message: - 'What is the scope of this change (e.g. component or file name): (press enter to skip)', - default: options.defaultScope, - filter: function(value) { - return options.disableScopeLowerCase - ? value.trim() - : value.trim().toLowerCase(); - } - }, - { - 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, options.disableSubjectLowerCase); - 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, options.disableSubjectLowerCase); - var color = - filteredSubject.length <= maxSummaryLength(options, answers) - ? chalk.green - : chalk.red; - return color('(' + filteredSubject.length + ') ' + subject); - }, - filter: function(subject) { - return filterSubject(subject, options.disableSubjectLowerCase); - } - }, - { - 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; - }, - 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; - } - }, - - { - 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) { + const finalQuestions = options.skipQuestions ? initQuestions.filter(item => !options.skipQuestions.includes(item.name)) : initQuestions; + cz.prompt(finalQuestions).then(function(answers) { var wrapOptions = { trim: true, cut: false, diff --git a/index.js b/index.js index 7a4586d2..efb67a22 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,8 @@ var options = { (process.env.CZ_MAX_LINE_WIDTH && parseInt(process.env.CZ_MAX_LINE_WIDTH)) || config.maxLineWidth || - 100 + 100, + skipQuestions: process.env.CZ_SKIP_QUESTIONS || config.skipQuestions }; (function(options) {