|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +(function () { |
| 4 | + 'use strict'; |
| 5 | + |
| 6 | + var childProcess = require('child_process'), |
| 7 | + inquirer = require('inquirer'), |
| 8 | + configParser = require('turbo-git-config').parser, |
| 9 | + utils = require('turbo-git-config').utils, |
| 10 | + Promise = require('promise'); |
| 11 | + |
| 12 | + require('colors'); |
| 13 | + |
| 14 | + init(); |
| 15 | + |
| 16 | + function init() { |
| 17 | + inquirer |
| 18 | + .prompt([{ |
| 19 | + type: 'list', |
| 20 | + name: 'tag', |
| 21 | + message: configParser.getCommitPromptText('tag'), |
| 22 | + choices: configParser.getTagsFormat() |
| 23 | + }]) |
| 24 | + .then(function (answers) { |
| 25 | + var tagTitle = answers.tag; |
| 26 | + |
| 27 | + showPrompts(tagTitle); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + function showPrompts(tagTitle) { |
| 32 | + var commitText = '', |
| 33 | + tagTitleLength = tagTitle.length, |
| 34 | + fieldTexts = { |
| 35 | + 'title': configParser.getCommitPromptText('title'), |
| 36 | + 'component': configParser.getCommitPromptText('component'), |
| 37 | + 'desc': configParser.getCommitPromptText('desc') |
| 38 | + }; |
| 39 | + |
| 40 | + commitText += tagTitle; |
| 41 | + |
| 42 | + onComponentPrompt('component', fieldTexts.component).then(function (componentInput) { |
| 43 | + if (componentInput){ |
| 44 | + commitText += '('+componentInput+'): '; |
| 45 | + } |
| 46 | + nextStepTitle(); |
| 47 | + }); |
| 48 | + |
| 49 | + function nextStepTitle() { |
| 50 | + onTitlePrompt('title', fieldTexts.title).then(function (titleInput) { |
| 51 | + commitText += titleInput; |
| 52 | + nextStepDesc(); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + function nextStepDesc() { |
| 57 | + onDescPrompt('desc', fieldTexts.desc, tagTitleLength).then(function (descInput) { |
| 58 | + commitText += descInput; |
| 59 | + execCommit(commitText); |
| 60 | + }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function onComponentPrompt(field, desc) { |
| 65 | + if (!desc) { return Promise.resolve(''); } |
| 66 | + return onAskFor(field, desc).then(function (component) { |
| 67 | + if (!component) { |
| 68 | + return ''; |
| 69 | + } |
| 70 | + return component; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + function onTitlePrompt(field, desc) { |
| 75 | + if (!desc) { return Promise.resolve(''); } |
| 76 | + return onAskFor(field, desc).then(function (title) { |
| 77 | + if (!title) { |
| 78 | + utils.showError('Title is mandatory'); |
| 79 | + return onTitlePrompt(field, desc); |
| 80 | + } |
| 81 | + return Promise.resolve(' ' + title); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + function onDescPrompt(field, desc, tagTitleLength) { |
| 87 | + var parseDesc; |
| 88 | + |
| 89 | + if (!desc) { return Promise.resolve(''); } |
| 90 | + return onAskFor(field, desc).then(function (commitDesc) { |
| 91 | + if (commitDesc) { |
| 92 | + parseDesc = '\n\n'; |
| 93 | + parseDesc += new Array(tagTitleLength).join(' '); |
| 94 | + parseDesc += ' - ' + commitDesc; |
| 95 | + return parseDesc; |
| 96 | + } |
| 97 | + return ''; |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + function onAskFor(field, text) { |
| 103 | + return inquirer |
| 104 | + .prompt([{ |
| 105 | + type: 'input', |
| 106 | + name: field, |
| 107 | + message: text |
| 108 | + }]) |
| 109 | + .then(function (answers) { |
| 110 | + return answers[field]; |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + function execCommit(commitMessage) { |
| 115 | + childProcess.exec('git commit -m "' + commitMessage + '"', function (error) { |
| 116 | + if (error) { |
| 117 | + utils.showError('Nothing to commit, you need to do a `git add` before commit'); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | +})(); |
0 commit comments