-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·84 lines (74 loc) · 1.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env node
const c = require('chalk')
const lint = require('./src/lint')
const prompts = require('./src/prompts')
const { findIssuePR, gitCommit } = require('./src/git')
const { checkType, checkScope, checkDes } = require('./src/continuousCheck')
const { typeMap, areaDes } = require('./src/convention')
const {
replLive,
onAny,
onTab,
onLine,
onInput,
onStop,
onSubmit,
} = require('repll')
const repll = replLive(prompts)
let commitMes = []
onAny(() => (commitMes = repll.history.filter(e => e.length)))
onLine(l => {
switch (l) {
case 2: {
printTips('body')
break
}
case 3: {
printTips('footer')
break
}
case 4: {
repll.refresh(
c`\n{yellow ${commitMes.join(
'\n\n'
)}\n\n}{red Press ctrl+s to commit it, ctrl+c to quit}`
)
break
}
}
})
onStop(() => {
if (repll.inputLine > 1 && !repll.issued) {
const issuePR = findIssuePR(repll)
if (issuePR === true) repll.issued = true
else if (issuePR) repll.refresh(`\n${issuePR}`)
}
}, 0.5)
onInput(() => {
if (repll.inputLine === 1) continuousCheck() && lint(repll)
else lint(repll)
})
onTab(v => {
const selectedList = Object.keys(typeMap).filter(
e => e.startsWith(v) && e.length > v.length
)
return repll.inputLine === 1 ? [selectedList, typeMap] : [[]]
})
onSubmit(() => {
const commit = gitCommit(process.argv[2], commitMes.join('\n\n'))
if (commit) {
// If we have message to print, we shall not restore cursor
repll.write(commit)
process.exit()
}
})
function printTips(name) {
repll.refresh(areaDes[name])
return false
}
// Continuous check
function continuousCheck() {
if (checkType(repll) || printTips('type'))
if (checkScope(repll) || printTips('scope'))
if (checkDes(repll) || printTips('des')) return true
}