├── commitlint.config.js ├── readme_src └── demo.gif ├── .github └── FUNDING.yml ├── LICENSE ├── package.json ├── README.md ├── .gitignore └── index.js /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = {extends: ['@commitlint/config-conventional']} 2 | -------------------------------------------------------------------------------- /readme_src/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GitSquared/komit/HEAD/readme_src/demo.gif -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: GitSquared 4 | custom: ['https://gaby.dev/donate'] 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License Copyright (c) 2020 Gabriel SAILLARD 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice (including the next 11 | paragraph) shall be included in all copies or substantial portions of the 12 | Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 17 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 19 | OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "komit", 3 | "version": "1.0.5", 4 | "description": "An interactive prompt for conventional commit messages that doesn't get in your way. Designed to run as a git hook.", 5 | "main": "index.js", 6 | "bin": "./index.js", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "husky": { 11 | "hooks": { 12 | "prepare-commit-msg": "node index.js $HUSKY_GIT_PARAMS", 13 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 14 | } 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/GitSquared/komit.git" 19 | }, 20 | "keywords": [ 21 | "git", 22 | "commit", 23 | "commitlint", 24 | "terminal", 25 | "conventional-commit" 26 | ], 27 | "author": "Gabriel SAILLARD (https://gaby.dev)", 28 | "license": "MIT", 29 | "bugs": { 30 | "url": "https://github.com/GitSquared/komit/issues" 31 | }, 32 | "homepage": "https://github.com/GitSquared/komit#readme", 33 | "dependencies": { 34 | "ansi-colors": "^4.1.1", 35 | "enquirer": "^2.3.6", 36 | "force-stdin-tty": "0.0.1" 37 | }, 38 | "devDependencies": { 39 | "@commitlint/cli": "^17.6.1", 40 | "@commitlint/config-conventional": "^17.6.1", 41 | "husky": "^8.0.3" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 👮 Komit 2 | ![npm badge](https://badgen.net/npm/v/komit) 3 | 4 | Komit is a small prompt designed to be run as a git hook to help follow the [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) message standard. 5 | 6 | Komit doesn't try to get in your way, and assumes you only need help writing the header part of the commit. It lets git open your favorite editor to change the body & footer of your message before commiting. 7 | 8 | It also helps maintaining a consistent list of commit scopes by syncing a `.commitscopesrc` file in your repository. 9 | 10 | ## Demo 11 | 12 | ![Komit demo](https://raw.githubusercontent.com/GitSquared/komit/master/readme_src/demo.gif) 13 | 14 | ## Installation with [Husky](https://github.com/typicode/husky) 15 | 16 | Install the package: 17 | ``` 18 | npm i -D komit 19 | ``` 20 | 21 | Add the hook to your package.json: 22 | ```json 23 | "husky": { 24 | "hooks": { 25 | "prepare-commit-msg": "komit $HUSKY_GIT_PARAMS" 26 | } 27 | }, 28 | ``` 29 | 30 | ## Installation with traditional git hooks 31 | 32 | Add to `.git/hooks/prepare-commit-msg`: 33 | ```sh 34 | #!/bin/sh 35 | 36 | npx komit .git/COMMIT_EDITMSG 37 | ``` 38 | 39 | ## Credits 40 | Built with the amazing [enquirer](https://github.com/enquirer/enquirer) lib. Thanks to @dosentmatter for [force-stdin-tty](https://github.com/dosentmatter/force-stdin-tty) which fixed stdin access problems when running as a Husky hook. 41 | 42 | Written by [Gaby](https://gaby.dev), a software architect based in Paris. 43 | 44 | ## License 45 | [MIT](https://github.com/GitSquared/komit/blob/master/LICENSE) 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | .vercel -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const fs = require('fs') 3 | const cp = require('child_process') 4 | const path = require('path') 5 | const { forceStdinTty } = require('force-stdin-tty') 6 | const { prompt } = require('enquirer') 7 | const c = require('ansi-colors') 8 | const pkg = require('./package.json') 9 | 10 | const msgPath = path.resolve(process.cwd(), process.argv[process.argv.length - 1]) 11 | const scopesRc = path.resolve(process.cwd(), '.commitscopesrc') 12 | 13 | const scopes = [] 14 | 15 | let type, scope, headerPrefix, maxTitleLength 16 | 17 | process.stdout.write(c.bold('\n👮 Welcome to Komit\n') + c.dim(`v${pkg.version}\n\n`)) 18 | 19 | let overwroteStdin = false 20 | try { 21 | overwroteStdin = forceStdinTty() 22 | } catch { 23 | process.stdout.write(c.bold.red("⚠️ Komit couldn't access your stdin stream.\n")) 24 | process.exit(1) 25 | } 26 | 27 | try { 28 | fs.readFileSync(scopesRc, { 29 | encoding: 'utf8' 30 | }).split('\n').forEach(line => { 31 | if (line.startsWith('#') || line.trim().length === 0) return 32 | scopes.push(line.toLowerCase().trim().replace(/ /g, '-')) 33 | }) 34 | } catch(error) { 35 | // ignore 36 | } 37 | 38 | function scopeSuggest(input, choices) { 39 | let newPrompt = true 40 | const list = choices.filter(ch => { 41 | if (ch.name === 'new' || ch.name === '(unscoped)') return false 42 | 43 | const test = ch.message.toLowerCase() 44 | if (test === input.toLowerCase().trim().replace(/ /g, '-')) { 45 | newPrompt = false 46 | } 47 | 48 | return test.includes(input.toLowerCase().trim().replace(/ /g, '-')) 49 | }) 50 | 51 | if (newPrompt && input) { 52 | list.push({ 53 | name: 'new', 54 | normalized: true, 55 | message: `🏷️ New scope: ${input.toLowerCase().trim().replace(/ /g, '-')}`, 56 | value: input.toLowerCase().trim().replace(/ /g, '-') 57 | }) 58 | } 59 | 60 | if (input.length === 0) { 61 | list.unshift({ 62 | name: '(unscoped)', 63 | normalized: true, 64 | message: '(unscoped)', 65 | value: '(unscoped)' 66 | }) 67 | } 68 | 69 | return list 70 | } 71 | 72 | prompt([ 73 | { 74 | type: 'autocomplete', 75 | name: 'type', 76 | message: 'Commit type:', 77 | choices: [ 78 | 'build', 79 | 'ci', 80 | 'chore', 81 | 'docs', 82 | 'feat', 83 | 'fix', 84 | 'perf', 85 | 'refactor', 86 | 'revert', 87 | 'style', 88 | 'test' 89 | ], 90 | result: result => { 91 | type = result 92 | return result 93 | } 94 | }, 95 | { 96 | type: 'autocomplete', 97 | name: 'scope', 98 | message: 'Scope:', 99 | choices: [ 100 | '(unscoped)', 101 | ...scopes 102 | ], 103 | suggest: scopeSuggest, 104 | result: result => { 105 | scope = result 106 | headerPrefix = `${type}${(scope !== '(unscoped)') ? `(${scope})` : ''}: ` 107 | maxTitleLength = 100 - headerPrefix.length 108 | return result 109 | } 110 | }, 111 | { 112 | type: 'input', 113 | name: 'title', 114 | message: 'Title:', 115 | validate: title => { 116 | if (title.length === 0) { 117 | return 'Commit title cannot be empty!' 118 | } 119 | if (title.length > maxTitleLength) { 120 | return `Title is too long (${title.length}/${maxTitleLength}) characters)` 121 | } 122 | if (title[0].charCodeAt(0) !== title[0].toLowerCase().charCodeAt(0)) { 123 | return 'Title must start in lowercase' 124 | } 125 | if (title[title.length - 1] === '.') { 126 | return 'Title cannot end with a full stop "."' 127 | } 128 | return true 129 | } 130 | } 131 | ]).then(answer => { 132 | if (answer.scope !== '(unscoped)' && !scopes.includes(answer.scope)) { 133 | scopes.push(answer.scope) 134 | fs.writeFileSync(scopesRc, scopes.join('\n')) 135 | process.stdout.write(c.dim('Updated commit scopes file at .commitscopesrc, adding file to commit.\n')) 136 | cp.execSync(`git add ${scopesRc}`, { 137 | cwd: process.cwd() 138 | }) 139 | } 140 | 141 | const commitHeader = `${headerPrefix}${answer.title}` 142 | process.stdout.write( 143 | '\n' + 144 | c.bold.green(type) + 145 | (scope !== '(unscoped)' ? `(${c.italic.blue(scope)}): ` : ': ') + 146 | c.bold.underline(answer.title) + 147 | '\n' 148 | ) 149 | 150 | fs.writeFileSync(msgPath, commitHeader) 151 | 152 | if (overwroteStdin) { 153 | process.stdin.destroy() 154 | } 155 | 156 | process.exit(0) 157 | }).catch(error => { 158 | 159 | if (overwroteStdin) { 160 | process.stdin.destroy() 161 | } 162 | 163 | if (error !== '') { 164 | process.stdout.write(c.bold.red(`${error}\n`)) 165 | process.exit(1) 166 | } else { 167 | process.stdout.write('\nGot SIGINT, bypassing prompts...\n') 168 | process.exit(0) 169 | } 170 | }) 171 | --------------------------------------------------------------------------------