Skip to content

Commit eac3bf9

Browse files
committed
feat(main): implement spellcheck
1 parent 0633afe commit eac3bf9

4 files changed

Lines changed: 96 additions & 26 deletions

File tree

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ $ npm install -g git-consistent
1616
# or
1717
$ yarn global add git-consistent
1818
19-
# options
20-
$ git config --global alias.con "consistent -i"
19+
$ git config --global alias.con "consistent -i -t" # recommended option
2120
```
2221

2322
## Usage
@@ -74,11 +73,7 @@ $ git-consistent --help
7473
--type <type> commit type
7574
-m, --subject <subject> The subject contains succinct description of the change
7675
--body [body] The body contains details of the change (default: )
77-
-d, --duet run git-duet mode
78-
-D, --dry-run run dry-run mode
79-
-i, --interactive run interactive mode
80-
-s, --skip-options skip not required term input (interactive mode only)
81-
-S, --silent don't show commit command
76+
...
8277
-V, --version output the version number
8378
-h, --help output usage information
8479
```
@@ -200,6 +195,16 @@ Date: Sat Feb 10 17:40:33 2018 +0900
200195
This is test.
201196
```
202197

198+
#### spell check
199+
200+
With `-t` option.
201+
202+
```sh
203+
$ git consistent -t --type="feat" --scope="" --subject="this is some text we want to ceck for typos"
204+
git commit -m "feat: this is some text we want to ceck for typos"
205+
Is 'ceck' misspelled? Did you mean that? 'check', 'ceca', 'neck', 'cock', 'deck', 'peck', 'heck', 'beck', 'Peck', 'Beck', 'Keck'
206+
```
207+
203208
#### git-duet
204209

205210
Run [git-duet](https://github.com/git-duet/git-duet) mode when with `-d` option.
@@ -253,23 +258,22 @@ Date: Sat Feb 10 15:13:40 2018 +0900
253258
254259
## command options
255260
256-
| Option | Default | Description |
257-
|----------------------|---------|------------------------------------------------------|
258-
| `-d, --duet` | false | run git-duet mode |
259-
| `-D, --dry-run` | false | run dry-run mode |
260-
| `-i, --interactive` | false | run interactive mode |
261-
| `-s, --skip-options` | false | skip not required term input (interactive mode only) |
262-
| `-S, --silent` | false | dont show commit command |
263-
| `-V, --version` | | output the version number |
261+
| Option | Description |
262+
|----------------------|------------------------------------------------------|
263+
| `-d, --duet` | run git-duet mode |
264+
| `-D, --dry-run` | run dry-run mode |
265+
| `-i, --interactive` | run interactive mode |
266+
| `-s, --skip-options` | skip not required term input (interactive mode only) |
267+
| `-S, --silent` | dont show commit command |
268+
| `-t, --typo-check`, | check spell |
269+
| `-V, --version` | output the version number |
264270
265271
---
266272
267273
# TODO
268274
## feature
269275
### support emoji
270276
271-
### typo check
272-
273277
## Develop
274278
### test
275279

git-consistent

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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

1113
const templateFileName = '.gitcommit_template'
1214
const 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+
200220
const 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

224244
const 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

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
},
2121
"dependencies": {
2222
"commander": "^2.14.1",
23+
"dictionary-en-us": "^2.0.0",
2324
"js-yaml": "^3.10.0",
2425
"lodash": "^4.17.5",
26+
"nodehun": "^2.0.11",
27+
"nodehun-sentences": "^1.0.4",
2528
"prompt-sync": "^4.1.5"
2629
}
2730
}

yarn.lock

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,70 @@ argparse@^1.0.7:
88
dependencies:
99
sprintf-js "~1.0.2"
1010

11+
array-unique@^0.1.1:
12+
version "0.1.1"
13+
resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.1.1.tgz#b2dc8ec765d306537d7291e0e621e849d2573cf9"
14+
15+
async@^2.1.4:
16+
version "2.6.0"
17+
resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4"
18+
dependencies:
19+
lodash "^4.14.0"
20+
1121
commander@^2.14.1:
1222
version "2.14.1"
1323
resolved "https://registry.yarnpkg.com/commander/-/commander-2.14.1.tgz#2235123e37af8ca3c65df45b026dbd357b01b9aa"
1424

25+
dictionary-en-us@^2.0.0:
26+
version "2.0.0"
27+
resolved "https://registry.yarnpkg.com/dictionary-en-us/-/dictionary-en-us-2.0.0.tgz#b9135f403444bb5e4b3cf465fb7d3f2fd276a839"
28+
1529
esprima@^4.0.0:
1630
version "4.0.0"
1731
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804"
1832

33+
in-publish@^2.0.0:
34+
version "2.0.0"
35+
resolved "https://registry.yarnpkg.com/in-publish/-/in-publish-2.0.0.tgz#e20ff5e3a2afc2690320b6dc552682a9c7fadf51"
36+
1937
js-yaml@^3.10.0:
2038
version "3.10.0"
2139
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc"
2240
dependencies:
2341
argparse "^1.0.7"
2442
esprima "^4.0.0"
2543

26-
lodash@^4.17.5:
44+
lodash@^4.14.0, lodash@^4.17.5:
2745
version "4.17.5"
2846
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
2947

48+
nodehun-sentences@^1.0.4:
49+
version "1.0.4"
50+
resolved "https://registry.yarnpkg.com/nodehun-sentences/-/nodehun-sentences-1.0.4.tgz#a4aef2d7707d7d568133806e957a6a4ab9eb9106"
51+
dependencies:
52+
async "^2.1.4"
53+
in-publish "^2.0.0"
54+
partial "^0.0.3"
55+
unique-words "^1.0.0"
56+
57+
nodehun@^2.0.11:
58+
version "2.0.11"
59+
resolved "https://registry.yarnpkg.com/nodehun/-/nodehun-2.0.11.tgz#cbba5595720f325fb247fb0e7e2018a56f7ed188"
60+
61+
partial@^0.0.3:
62+
version "0.0.3"
63+
resolved "https://registry.yarnpkg.com/partial/-/partial-0.0.3.tgz#2d107bed98fcea8d28d7665495b39e61dfcd18c6"
64+
3065
prompt-sync@^4.1.5:
3166
version "4.1.5"
3267
resolved "https://registry.yarnpkg.com/prompt-sync/-/prompt-sync-4.1.5.tgz#709ac182388b0e9a4a45b5683ed0449ed19f3eb8"
3368

3469
sprintf-js@~1.0.2:
3570
version "1.0.3"
3671
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
72+
73+
unique-words@^1.0.0:
74+
version "1.0.0"
75+
resolved "https://registry.yarnpkg.com/unique-words/-/unique-words-1.0.0.tgz#40b26a19f98061663c813cd97a1a20a7f8b55990"
76+
dependencies:
77+
array-unique "^0.1.1"

0 commit comments

Comments
 (0)