|
| 1 | +"format cjs"; |
| 2 | + |
| 3 | +var wrap = require('word-wrap'); |
| 4 | +var objectAssign = require('object-assign'); |
| 5 | +var objectValues = require('object-values'); |
| 6 | +var forEach = require('lodash.foreach'); |
| 7 | +var mapKeys = require('lodash.mapkeys'); |
| 8 | +var mapValues = require('lodash.mapvalues'); |
| 9 | +var rightPadKeys = require('right-pad-keys'); |
| 10 | +var conventionalCommitTypes = require('conventional-commit-types'); |
| 11 | + |
| 12 | +// This can be any kind of SystemJS compatible module. |
| 13 | +// We use Commonjs here, but ES6 or AMD would do just |
| 14 | +// fine. |
| 15 | +module.exports = function (options) { |
| 16 | + |
| 17 | + options = options || {}; |
| 18 | + |
| 19 | + var types = objectAssign({}, options.types || conventionalCommitTypes.types); |
| 20 | + forEach(types, function (type, key) { |
| 21 | + type.key = key; |
| 22 | + }); |
| 23 | + types = mapKeys(types, function (type, key) { |
| 24 | + return key + ':'; |
| 25 | + }); |
| 26 | + types = rightPadKeys(types); |
| 27 | + |
| 28 | + var choices = mapValues(types, function (type, key) { |
| 29 | + return { |
| 30 | + name: key + ' ' + type.description, |
| 31 | + value: type.key |
| 32 | + }; |
| 33 | + }); |
| 34 | + choices = objectValues(choices); |
| 35 | + |
| 36 | + return { |
| 37 | + // When a user runs `git cz`, prompter will |
| 38 | + // be executed. We pass you cz, which currently |
| 39 | + // is just an instance of inquirer.js. Using |
| 40 | + // this you can ask questions and get answers. |
| 41 | + // |
| 42 | + // The commit callback should be executed when |
| 43 | + // you're ready to send back a commit template |
| 44 | + // to git. |
| 45 | + // |
| 46 | + // By default, we'll de-indent your commit |
| 47 | + // template and will keep empty lines. |
| 48 | + prompter: function(cz, commit) { |
| 49 | + console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n'); |
| 50 | + |
| 51 | + // Let's ask some questions of the user |
| 52 | + // so that we can populate our commit |
| 53 | + // template. |
| 54 | + // |
| 55 | + // See inquirer.js docs for specifics. |
| 56 | + // You can also opt to use another input |
| 57 | + // collection library if you prefer. |
| 58 | + cz.prompt([ |
| 59 | + { |
| 60 | + type: 'list', |
| 61 | + name: 'type', |
| 62 | + message: 'Select the type of change that you\'re committing:', |
| 63 | + choices: choices |
| 64 | + }, { |
| 65 | + type: 'input', |
| 66 | + name: 'scope', |
| 67 | + message: 'Denote the scope of this change ($location, $browser, $compile, etc.):\n' |
| 68 | + }, { |
| 69 | + type: 'input', |
| 70 | + name: 'subject', |
| 71 | + message: 'Write a short, imperative tense description of the change:\n' |
| 72 | + }, { |
| 73 | + type: 'input', |
| 74 | + name: 'body', |
| 75 | + message: 'Provide a longer description of the change:\n' |
| 76 | + }, { |
| 77 | + type: 'input', |
| 78 | + name: 'footer', |
| 79 | + message: 'List any breaking changes or issues closed by this change:\n' |
| 80 | + } |
| 81 | + ]).then(function(answers) { |
| 82 | + |
| 83 | + var maxLineWidth = 100; |
| 84 | + |
| 85 | + var wrapOptions = { |
| 86 | + trim: true, |
| 87 | + newline: '\n', |
| 88 | + indent:'', |
| 89 | + width: maxLineWidth |
| 90 | + }; |
| 91 | + |
| 92 | + // parentheses are only needed when a scope is present |
| 93 | + var scope = answers.scope.trim(); |
| 94 | + scope = scope ? '(' + answers.scope.trim() + ')' : ''; |
| 95 | + |
| 96 | + // Hard limit this line |
| 97 | + var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth); |
| 98 | + |
| 99 | + // Wrap these lines at 100 characters |
| 100 | + var body = wrap(answers.body, wrapOptions); |
| 101 | + var footer = wrap(answers.footer, wrapOptions); |
| 102 | + |
| 103 | + commit(head + '\n\n' + body + '\n\n' + footer); |
| 104 | + }); |
| 105 | + } |
| 106 | + }; |
| 107 | +}; |
0 commit comments