UNPKG

cz-conventional-changelog-zh-cn

Version:

A fork of cz-conventional-changelog, and has been sensibly localized into Simplified Chinese.

221 lines (206 loc) 7.26 kB
'format cjs'; var wrap = require('word-wrap'); var map = require('lodash.map'); var longest = require('longest'); var chalk = require('chalk'); var filter = function(array) { return array.filter(function(x) { return x; }); }; var headerLength = function(answers) { return ( answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0) ); }; var maxSummaryLength = function(options, answers) { return options.maxHeaderWidth - headerLength(answers); }; var filterSubject = function(subject, disableSubjectLowerCase) { subject = subject.trim(); if (!disableSubjectLowerCase && subject.charAt(0).toLowerCase() !== subject.charAt(0)) { subject = subject.charAt(0).toLowerCase() + subject.slice(1, subject.length); } while (subject.endsWith('.')) { subject = subject.slice(0, subject.length - 1); } return subject; }; // This can be any kind of SystemJS compatible module. // We use Commonjs here, but ES6 or AMD would do just // fine. module.exports = function(options) { var types = options.types; var length = longest(Object.keys(types)).length + 1; var choices = map(types, function(type, key) { return { name: (key + ':').padEnd(length) + ' ' + type.description, value: key }; }); return { // When a user runs `git cz`, prompter will // be executed. We pass you cz, which currently // is just an instance of inquirer.js. Using // this you can ask questions and get answers. // // The commit callback should be executed when // you're ready to send back a commit template // to git. // // By default, we'll de-indent your commit // template and will keep empty lines. prompter: function(cz, commit) { // Let's ask some questions of the user // so that we can populate our commit // template. // // 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: "选择您正在提交的变更类型:", choices: choices, default: options.defaultType }, { type: 'input', name: 'scope', message: '本次变更的范围(例如一个组件或文件名)是什么:(按回车跳过)', default: options.defaultScope, filter: function(value) { return options.disableScopeLowerCase ? value.trim() : value.trim().toLowerCase(); } }, { type: 'input', name: 'subject', message: function(answers) { return ( '编写简要的变更描述(最多 ' + maxSummaryLength(options, answers) + ' 字符):\n' ); }, default: options.defaultSubject, validate: function(subject, answers) { var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase); return filteredSubject.length == 0 ? '必须填写简要描述' : filteredSubject.length <= maxSummaryLength(options, answers) ? true : '简要描述的长度必须小于等于 ' + maxSummaryLength(options, answers) + ' 字符,当前长度为 ' + filteredSubject.length + ' 字符。'; }, 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: '可提供更详细的变更描述正文:(按回车跳过)\n', default: options.defaultBody }, { type: 'confirm', name: 'isBreaking', message: '是否存在破坏性变更(BREAKING CHANGE)?', default: false }, { type: 'input', name: 'breakingBody', default: '-', message: '破坏性变更(BREAKING CHANGE)的提交需要提供更详细的描述正文:\n', when: function(answers) { return answers.isBreaking && !answers.body; }, validate: function(breakingBody, answers) { return ( breakingBody.trim().length > 0 || '必须填写破坏性变更(BREAKING CHANGE)的描述正文' ); } }, { type: 'input', name: 'breaking', message: '请描述破坏性变更的内容:\n', when: function(answers) { return answers.isBreaking; } }, { type: 'confirm', name: 'isIssueAffected', message: '本次变更是否影响到未关闭的问题(issue)?', default: options.defaultIssues ? true : false }, { type: 'input', name: 'issuesBody', default: '-', message: '如果问题(issue)已关闭,则本次提交需要提供更详细的描述正文:\n', when: function(answers) { return ( answers.isIssueAffected && !answers.body && !answers.breakingBody ); } }, { type: 'input', name: 'issues', message: '添加对问题(issue)的引用(例如“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 = answers.scope ? '(' + answers.scope + ')' : ''; // 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; // 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; commit(filter([head, body, breaking, issues]).join('\n\n')); }); } }; };