Skip to content

Commit

Permalink
feat(main): implement forment check
Browse files Browse the repository at this point in the history
  • Loading branch information
isuke committed Feb 10, 2018
1 parent 92ebda5 commit 0f964f3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
8 changes: 8 additions & 0 deletions .git_consistent
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ subject:
type: string
required: true
description: 'The subject contains succinct description of the change'
rules:
firstLatter: lower
dotAtEnd: false
ascii: false
body:
type: text
default: ''
required: false
description: 'Body'
rules:
firstLatter: upper
dotAtEnd: true
ascii: false
7 changes: 3 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
"args": [
"-i",
"--type='feat'",
// "--scope='main'",
"--subject='this is test'",
"--body='This is test.'",
"-d"
"--scope=''",
// "--subject='this is test'",
"-D"
]
}
]
Expand Down
48 changes: 33 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@ scope:
suffix: ')'
```
### format check
```yml
subject:
type: string
required: true
description: 'The subject contains succinct description of the change'
rules:
firstLatter: lower
dotAtEnd: false
ascii: false
```
```sh
$ git consistent --subject="Write documents."
subject must be first latter is lowercase.
subject should put dot (.) at the end.

$ git consistent --subject="ドキュメントを書いた"
subject must be first latter is lowercase.
subject should only alphabet.

$ git consistent -i
Enter subject:
subject is required.
Enter subject: Write documents.
subject must be first latter is lowercase.
subject should put dot (.) at the end.
Enter subject: write document
```
### git-duet
Run [git-duet](https://github.com/git-duet/git-duet) mode when with `-d` option.
Expand All @@ -128,21 +160,7 @@ Date: Sat Feb 10 15:13:40 2018 +0900
---
# TODO
## feature
### format check
```sh
$ git consistent --type="feat" --subject="Implement new feature"
The subject must begin with lowercase letters.
$ git consistent --type="foo" --subject="implement new feature"
'foo' is not defined.
You should select follow values.
'feat', 'fix', 'docs' and 'refactor'.
```

## develop
## Develop
### test
```sh
Expand Down
45 changes: 40 additions & 5 deletions git-consistent
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,42 @@ const input = (term, definition) => {
default:
throw new Error(`${type} is not defined.`)
}
checkValue(inputValue)
return inputValue
}

const checkValue = (value) => {
// TODO
const checkValue = (term, value, definition) => {
const rules = definition.rules
const errorMessages = []

if (definition.required && _.isEmpty(value)) errorMessages.push(`${term} is required.`)

_.forEach(rules, (ruleSetting, ruleName) => {
switch (ruleName) {
case 'firstLatter':
if (['small', 'lower', 'lowercase'].includes(ruleSetting) && /^[^a-z].*/.test(value)) {
errorMessages.push(`${term} must be first latter is lowercase.`)
} else if (['big', 'upper', 'uppercase'].includes(ruleSetting) && /^[^A-Z].*/.test(value)) {
errorMessages.push(`${term} must be first latter is uppercase.`)
}
break
case 'dotAtEnd':
if (ruleSetting) {
if (/[^\.]$/.test(value)) errorMessages.push(`${term} should not put dot (.) at the end.`)
} else {
if (/\.$/.test(value)) errorMessages.push(`${term} should put dot (.) at the end.`)
}
break
case 'ascii':
if (!ruleSetting && /[\u30a0-\u30ff\u3040-\u309f\u3005-\u3006\u30e0-\u9fcf]/.test(value)) {
errorMessages.push(`${term} should only alphabet.`)
}
break
default:
throw new Error(`${ruleName} is not defined.`)
}
})

return errorMessages.join("\n")
}

const replaceTerms = (program, template, definitions, terms) => {
Expand All @@ -132,15 +162,20 @@ const replaceTerm = (program, template, definition, term) => {
} else {
value = ''
}
while (definition.required && _.isEmpty(value)) {
console.log(`${warningColor}${term} is required${reset}`)
let errorMessages = checkValue(term, value, definition)
while (!_.isEmpty(errorMessages)) {
console.log(`${warningColor}${errorMessages}${reset}`)
value = input(term, definition)
errorMessages = checkValue(term, value, definition)
}
}
} else {
if (_.isEmpty(value) && definition.required) throw new Error(`${term} is required.`)
}

const errorMessages = checkValue(term, value, definition)
if (!_.isEmpty(errorMessages)) throw new Error(errorMessages)

let decoratedValue = value
if (!_.isEmpty(value)) {
if (definition.prefix) decoratedValue = definition.prefix + decoratedValue
Expand Down

0 comments on commit 0f964f3

Please sign in to comment.