|
| 1 | +import chalk from 'chalk'; |
| 2 | +import execa from 'execa'; |
| 3 | +import inquirer from 'inquirer'; |
| 4 | +import getConfig from './getConfig'; |
| 5 | + |
| 6 | +const promptConfig = { |
| 7 | + config: () => getConfig(), |
| 8 | + |
| 9 | + choices: (configuration) => { |
| 10 | + const choicesList = []; |
| 11 | + |
| 12 | + configuration.types.forEach((type) => { |
| 13 | + const emoji = `${type.emoji} ` || ''; |
| 14 | + const configType = type.type; |
| 15 | + const description = type.description || ''; |
| 16 | + |
| 17 | + choicesList.push({ |
| 18 | + value: emoji + configType, |
| 19 | + name: `${chalk.bold(configType)} ${description}`, |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + return choicesList; |
| 24 | + }, |
| 25 | + |
| 26 | + answerTypes: (configuration) => { |
| 27 | + const configTypeList = []; |
| 28 | + configuration.types.forEach((type) => { |
| 29 | + const configType = type.type; |
| 30 | + |
| 31 | + configTypeList.push(configType); |
| 32 | + }); |
| 33 | + return configTypeList; |
| 34 | + }, |
| 35 | + |
| 36 | + questions: (choices) => { |
| 37 | + const questionsList = [ |
| 38 | + { |
| 39 | + type: 'list', |
| 40 | + name: 'type', |
| 41 | + message: 'Select the type of your commit:', |
| 42 | + choices, |
| 43 | + }, |
| 44 | + { |
| 45 | + type: 'input', |
| 46 | + name: 'description', |
| 47 | + message: 'Enter your commit message:', |
| 48 | + validate: /* istanbul ignore next */ input => (input ? true : 'A commit message is mandatory!'), |
| 49 | + }, |
| 50 | + { |
| 51 | + type: 'confirm', |
| 52 | + name: 'moreInfo', |
| 53 | + message: 'Do you want to add more information to your commit?', |
| 54 | + default: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + type: 'editor', |
| 58 | + name: 'editor', |
| 59 | + message: 'This will let you add more information', |
| 60 | + when: /* istanbul ignore next */ answers => answers.moreInfo, |
| 61 | + default: /* istanbul ignore next */ answers => `${answers.type} ${answers.description}\n\n\n`, |
| 62 | + }, |
| 63 | + ]; |
| 64 | + return questionsList; |
| 65 | + }, |
| 66 | + |
| 67 | + prompt: (questions) => { |
| 68 | + const config = promptConfig.config(); |
| 69 | + const types = promptConfig.answerTypes(config); |
| 70 | + const cli = inquirer.prompt(questions).then((answers) => { |
| 71 | + const message = answers.moreInfo ? `${answers.editor}` : `${answers.type} ${answers.description}`; |
| 72 | + /* istanbul ignore if */ |
| 73 | + if (types.includes(answers.type.split(' ')[1])) { |
| 74 | + return execa('git', ['commit', '-m', message]) |
| 75 | + .then(result => console.log(result.stdout)) |
| 76 | + .catch((err) => { |
| 77 | + console.error(chalk.red("Have you thought about 'git add' some files? Add files and try run following again:\n")); |
| 78 | + console.error(chalk.green(err.cmd)); |
| 79 | + }); |
| 80 | + } |
| 81 | + return answers.type; |
| 82 | + }); |
| 83 | + return cli; |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +export default promptConfig; |
0 commit comments