11#!/usr/bin/env node
22
3- const path = require ( 'path' )
4- const fs = require ( 'fs' )
5- const _ = require ( 'lodash' )
6- const yaml = require ( 'js-yaml' )
7- const execSync = require ( 'child_process' ) . execSync
8- const program = require ( 'commander' )
9- const prompt = require ( 'prompt-sync' ) ( )
3+ const path = require ( 'path' )
4+ const fs = require ( 'fs' )
5+ const _ = require ( 'lodash' )
6+ const yaml = require ( 'js-yaml' )
7+ const execSync = require ( 'child_process' ) . execSync
8+ const program = require ( 'commander' )
9+ const prompt = require ( 'prompt-sync' ) ( )
10+ const spellcheck = require ( 'nodehun-sentences' ) ;
11+ const nodehun = require ( 'nodehun' ) ;
1012
1113const templateFileName = '.gitcommit_template'
1214const definitionsFileName = '.git_consistent'
@@ -55,6 +57,7 @@ const createProgram = (program, definitions, terms) => {
5557 . option ( `-i, --interactive` , 'run interactive mode' )
5658 . option ( `-s, --skip-options` , 'skip not required term input (interactive mode only)' )
5759 . option ( `-S, --silent` , "don't show commit command" )
60+ . option ( `-t, --typo-check` , "check spell" )
5861 . version ( version )
5962 . parse ( process . argv )
6063}
@@ -197,6 +200,23 @@ const replaceTerm = (program, template, definition, term) => {
197200 return _ . replace ( template , `<${ term } >` , decoratedValue )
198201}
199202
203+ const checkSpell = ( message ) => {
204+ const dictionaryBase = path . dirname ( require . resolve ( 'dictionary-en-us' ) )
205+ const hunspell = new nodehun (
206+ fs . readFileSync ( path . join ( dictionaryBase , 'index.aff' ) ) ,
207+ fs . readFileSync ( path . join ( dictionaryBase , 'index.dic' ) )
208+ )
209+
210+ spellcheck ( hunspell , message , ( e , typos ) => {
211+ if ( e ) throw e
212+
213+ _ . forEach ( typos , ( typo ) => {
214+ process . stdout . write ( `${ yellow } Is '${ typo . word } ' misspelled? ${ reset } ` )
215+ console . log ( `${ yellow } Did you mean that? ${ typo . suggestions . map ( ( s ) => { return `'${ s } '` } ) . join ( ', ' ) } ${ reset } ` )
216+ } )
217+ } )
218+ }
219+
200220const checkAddedFile = ( ) => {
201221 const addedFiles = execSync ( `git diff --name-only --cached 2> /dev/null` ) . toString ( ) . trim ( )
202222 if ( _ . isEmpty ( addedFiles ) ) throw new Error ( `no changes added to commit.` )
@@ -222,10 +242,12 @@ const gitCommit = (commitMessage, duet = false, silent = false, dryRun = false)
222242}
223243
224244const main = ( program , template , definitions , terms ) => {
225- checkAddedFile ( )
245+ if ( ! program . dryRun ) checkAddedFile ( )
226246
227247 const commitMessage = replaceTerms ( program , template , definitions , terms )
228248
249+ if ( program . typoCheck ) checkSpell ( commitMessage )
250+
229251 gitCommit ( commitMessage . trim ( ) , program . duet , program . silent , program . dryRun )
230252}
231253
0 commit comments