Skip to content

Commit 0f964f3

Browse files
committed
feat(main): implement forment check
1 parent 92ebda5 commit 0f964f3

4 files changed

Lines changed: 84 additions & 24 deletions

File tree

.git_consistent

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ subject:
5959
type: string
6060
required: true
6161
description: 'The subject contains succinct description of the change'
62+
rules:
63+
firstLatter: lower
64+
dotAtEnd: false
65+
ascii: false
6266
body:
6367
type: text
6468
default: ''
6569
required: false
6670
description: 'Body'
71+
rules:
72+
firstLatter: upper
73+
dotAtEnd: true
74+
ascii: false

.vscode/launch.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
"args": [
1414
"-i",
1515
"--type='feat'",
16-
// "--scope='main'",
17-
"--subject='this is test'",
18-
"--body='This is test.'",
19-
"-d"
16+
"--scope=''",
17+
// "--subject='this is test'",
18+
"-D"
2019
]
2120
}
2221
]

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ scope:
108108
suffix: ')'
109109
```
110110
111+
### format check
112+
113+
```yml
114+
subject:
115+
type: string
116+
required: true
117+
description: 'The subject contains succinct description of the change'
118+
rules:
119+
firstLatter: lower
120+
dotAtEnd: false
121+
ascii: false
122+
```
123+
124+
```sh
125+
$ git consistent --subject="Write documents."
126+
subject must be first latter is lowercase.
127+
subject should put dot (.) at the end.
128+
129+
$ git consistent --subject="ドキュメントを書いた"
130+
subject must be first latter is lowercase.
131+
subject should only alphabet.
132+
133+
$ git consistent -i
134+
Enter subject:
135+
subject is required.
136+
Enter subject: Write documents.
137+
subject must be first latter is lowercase.
138+
subject should put dot (.) at the end.
139+
Enter subject: write document
140+
```
141+
142+
111143
### git-duet
112144
113145
Run [git-duet](https://github.com/git-duet/git-duet) mode when with `-d` option.
@@ -128,21 +160,7 @@ Date: Sat Feb 10 15:13:40 2018 +0900
128160
---
129161
130162
# TODO
131-
132-
## feature
133-
### format check
134-
135-
```sh
136-
$ git consistent --type="feat" --subject="Implement new feature"
137-
The subject must begin with lowercase letters.
138-
139-
$ git consistent --type="foo" --subject="implement new feature"
140-
'foo' is not defined.
141-
You should select follow values.
142-
'feat', 'fix', 'docs' and 'refactor'.
143-
```
144-
145-
## develop
163+
## Develop
146164
### test
147165
148166
```sh

git-consistent

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,42 @@ const input = (term, definition) => {
103103
default:
104104
throw new Error(`${type} is not defined.`)
105105
}
106-
checkValue(inputValue)
107106
return inputValue
108107
}
109108

110-
const checkValue = (value) => {
111-
// TODO
109+
const checkValue = (term, value, definition) => {
110+
const rules = definition.rules
111+
const errorMessages = []
112+
113+
if (definition.required && _.isEmpty(value)) errorMessages.push(`${term} is required.`)
114+
115+
_.forEach(rules, (ruleSetting, ruleName) => {
116+
switch (ruleName) {
117+
case 'firstLatter':
118+
if (['small', 'lower', 'lowercase'].includes(ruleSetting) && /^[^a-z].*/.test(value)) {
119+
errorMessages.push(`${term} must be first latter is lowercase.`)
120+
} else if (['big', 'upper', 'uppercase'].includes(ruleSetting) && /^[^A-Z].*/.test(value)) {
121+
errorMessages.push(`${term} must be first latter is uppercase.`)
122+
}
123+
break
124+
case 'dotAtEnd':
125+
if (ruleSetting) {
126+
if (/[^\.]$/.test(value)) errorMessages.push(`${term} should not put dot (.) at the end.`)
127+
} else {
128+
if (/\.$/.test(value)) errorMessages.push(`${term} should put dot (.) at the end.`)
129+
}
130+
break
131+
case 'ascii':
132+
if (!ruleSetting && /[\u30a0-\u30ff\u3040-\u309f\u3005-\u3006\u30e0-\u9fcf]/.test(value)) {
133+
errorMessages.push(`${term} should only alphabet.`)
134+
}
135+
break
136+
default:
137+
throw new Error(`${ruleName} is not defined.`)
138+
}
139+
})
140+
141+
return errorMessages.join("\n")
112142
}
113143

114144
const replaceTerms = (program, template, definitions, terms) => {
@@ -132,15 +162,20 @@ const replaceTerm = (program, template, definition, term) => {
132162
} else {
133163
value = ''
134164
}
135-
while (definition.required && _.isEmpty(value)) {
136-
console.log(`${warningColor}${term} is required${reset}`)
165+
let errorMessages = checkValue(term, value, definition)
166+
while (!_.isEmpty(errorMessages)) {
167+
console.log(`${warningColor}${errorMessages}${reset}`)
137168
value = input(term, definition)
169+
errorMessages = checkValue(term, value, definition)
138170
}
139171
}
140172
} else {
141173
if (_.isEmpty(value) && definition.required) throw new Error(`${term} is required.`)
142174
}
143175

176+
const errorMessages = checkValue(term, value, definition)
177+
if (!_.isEmpty(errorMessages)) throw new Error(errorMessages)
178+
144179
let decoratedValue = value
145180
if (!_.isEmpty(value)) {
146181
if (definition.prefix) decoratedValue = definition.prefix + decoratedValue

0 commit comments

Comments
 (0)