|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const promisify = require('es6-promisify'); |
| 5 | +const inquirer = require('inquirer'); |
| 6 | +const axios = require('axios'); |
| 7 | +const chalk = require('chalk'); |
| 8 | +const pathExists = require('path-exists'); |
| 9 | +const fileExists = require('file-exists'); |
| 10 | +const { map, path, test, join } = require('ramda'); |
| 11 | + |
| 12 | +const writeFile = promisify(fs.writeFile); |
| 13 | +const readFile = promisify(fs.readFile); |
| 14 | +const chmod = promisify(fs.chmod); |
| 15 | + |
| 16 | +const gitmojiUrl = 'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json'; |
| 17 | +const prepareCommitMsgFileName = 'prepare-commit-msg'; |
| 18 | + |
| 19 | +const gitmojiCommitHookComand = `#!/bin/sh |
| 20 | +exec < /dev/tty |
| 21 | +gitmoji-commit-hook $1 |
| 22 | +`; |
| 23 | + |
| 24 | +const errorMessage = { |
| 25 | + notGit: 'The directory is not a git repository.', |
| 26 | + commitHookExist: `A prepare-commit hook already exists, please remove the hook (rm .git/hooks/${prepareCommitMsgFileName}) or install gitmoji-commit-hook manually by adding the following content info .git/hooks/\n\n${prepareCommitMsgFileName}:${gitmojiCommitHookComand}`, |
| 27 | + gitmojiParse: 'Could not find gitmojis at url' |
| 28 | +}; |
| 29 | + |
| 30 | +function errorHandler(error) { |
| 31 | + console.error(chalk.red(`🚨 ERROR: ${error}`)); |
| 32 | + process.exit(1); |
| 33 | +} |
| 34 | + |
| 35 | +function rejectIf(errorMsg) { |
| 36 | + return val => val ? Promise.reject(new Error(errorMsg)) : val; |
| 37 | +} |
| 38 | + |
| 39 | +function rejectIfNot(errorMsg) { |
| 40 | + return val => val ? val : Promise.reject(new Error(errorMsg)); |
| 41 | +} |
| 42 | + |
| 43 | +function getGitmojiList() { |
| 44 | + return axios.get(gitmojiUrl) |
| 45 | + .then(path(['data', 'gitmojis'])) |
| 46 | + .then(rejectIfNot(errorMessage.gitmojiParse)); |
| 47 | +} |
| 48 | + |
| 49 | +function assertGitRepository() { |
| 50 | + return pathExists('.git') |
| 51 | + .then(rejectIfNot(errorMessage.notGit)); |
| 52 | +} |
| 53 | + |
| 54 | +function assertNoPrepareCommitHook(gitHookPath) { |
| 55 | + return () => fileExists(`${gitHookPath}/${prepareCommitMsgFileName}`) |
| 56 | + .then(rejectIf(errorMessage.commitHookExist)); |
| 57 | +} |
| 58 | + |
| 59 | +function initProject(gitHookPath) { |
| 60 | + return assertGitRepository() |
| 61 | + .then(assertNoPrepareCommitHook(gitHookPath)) |
| 62 | + .then(() => writeFile(`${gitHookPath}/${prepareCommitMsgFileName}`, gitmojiCommitHookComand)) |
| 63 | + .then(() => chmod(`${gitHookPath}/${prepareCommitMsgFileName}`, '755')); |
| 64 | +} |
| 65 | + |
| 66 | +function prependMessage(getMessage, putMessage) { |
| 67 | + return filepath => message => getMessage(filepath) |
| 68 | + .then(fileContent => `${message} ${fileContent}`) |
| 69 | + .then(fileContent => putMessage(filepath, fileContent)); |
| 70 | +} |
| 71 | + |
| 72 | +const prependMessageToFile = prependMessage(readFile, writeFile); |
| 73 | + |
| 74 | +function getGitmojiBlacklist() { |
| 75 | + return fileExists(`${process.env.PWD}/package.json`) |
| 76 | + .then(exist => exist ? require(`${process.env.PWD}/package.json`) : {}) |
| 77 | + .then(packageJson => packageJson.gitmoji || {}) |
| 78 | + .then(gitmoji => gitmoji.blacklist || []); |
| 79 | +} |
| 80 | + |
| 81 | +function seperateChoices(choices) { |
| 82 | + return blacklist => { |
| 83 | + if (blacklist.length === 0) { |
| 84 | + return choices; |
| 85 | + } |
| 86 | + return [ |
| 87 | + ...choices.filter(choice => !blacklist.includes(choice.type)), |
| 88 | + new inquirer.Separator(), |
| 89 | + ...choices.filter(choice => blacklist.includes(choice.type)), |
| 90 | + new inquirer.Separator() |
| 91 | + ]; |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +function seperateBlacklistEmojis(choices) { |
| 96 | + return getGitmojiBlacklist() |
| 97 | + .then(seperateChoices(choices)); |
| 98 | +} |
| 99 | + |
| 100 | +function printInitSuccess() { |
| 101 | + console.log(`${chalk.green('🎉 SUCCESS 🎉')} gitmoji-commit-hook initialized with success.`); |
| 102 | +} |
| 103 | + |
| 104 | +function mapGitmojiItemToOption(gitmoji) { |
| 105 | + return { |
| 106 | + name: gitmoji.emoji + ' ' + gitmoji.description, |
| 107 | + value: gitmoji.emoji, |
| 108 | + type: gitmoji.name |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function createInquirerQuestion(emojis) { |
| 113 | + return [{ |
| 114 | + type: 'checkbox', |
| 115 | + name: 'emoji', |
| 116 | + message: 'Select emoji(s) for your commit', |
| 117 | + choices: emojis |
| 118 | + }]; |
| 119 | +} |
| 120 | + |
| 121 | +const isCommitEditMsgFile = test(/COMMIT_EDITMSG/g); |
| 122 | + |
| 123 | +function gitmojiCommitHook(gitHookPath, commitFile) { |
| 124 | + if (commitFile === '--init') { |
| 125 | + initProject(gitHookPath) |
| 126 | + .then(printInitSuccess) |
| 127 | + .catch(errorHandler); |
| 128 | + } else if (isCommitEditMsgFile(commitFile)) { |
| 129 | + getGitmojiList() |
| 130 | + .then(map(mapGitmojiItemToOption)) |
| 131 | + .then(seperateBlacklistEmojis) |
| 132 | + .then(createInquirerQuestion) |
| 133 | + .then(inquirer.prompt) |
| 134 | + .then(answers => answers.emoji) |
| 135 | + .then(join(' ')) |
| 136 | + .then(prependMessageToFile(commitFile)) |
| 137 | + .catch(errorHandler); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +module.exports = { |
| 142 | + rejectIf, |
| 143 | + rejectIfNot, |
| 144 | + gitmojiCommitHook, |
| 145 | + prependMessage, |
| 146 | + mapGitmojiItemToOption, |
| 147 | + createInquirerQuestion, |
| 148 | + seperateChoices |
| 149 | +}; |
0 commit comments