/
prompts.js
75 lines (68 loc) 路 1.87 KB
/
prompts.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
// @flow
import inquirer from 'inquirer'
import inquirerAutocompletePrompt from 'inquirer-autocomplete-prompt'
import configurationVault from '@utils/configurationVault'
import filterGitmojis from '@utils/filterGitmojis'
import getDefaultCommitContent from '@utils/getDefaultCommitContent'
import { type CommitOptions } from '.'
import guard from './guard'
const TITLE_MAX_LENGTH_COUNT: number = 48
inquirer.registerPrompt('autocomplete', inquirerAutocompletePrompt)
export type Gitmoji = {
code: string,
description: string,
emoji: string,
name: string
}
export type Answers = {
gitmoji: string,
scope?: string,
title: string,
message: string
}
export default (
gitmojis: Array<Gitmoji>,
options: CommitOptions
): Array<Object> => {
const { title, message, scope } = getDefaultCommitContent(options)
return [
{
name: 'gitmoji',
message: 'Choose a gitmoji:',
type: 'autocomplete',
source: (answersSoFor: any, input: string) => {
return Promise.resolve(
filterGitmojis(input, gitmojis).map((gitmoji) => ({
name: `${gitmoji.emoji} - ${gitmoji.description}`,
value: gitmoji[configurationVault.getEmojiFormat()]
}))
)
}
},
...(configurationVault.getScopePrompt()
? [
{
name: 'scope',
message: 'Enter the scope of current changes:',
...(scope ? { default: scope } : {})
}
]
: []),
{
name: 'title',
message: 'Enter the commit title',
validate: guard.title,
transformer: (input: string) => {
return `[${
(title || input).length
}/${TITLE_MAX_LENGTH_COUNT}]: ${input}`
},
...(title ? { default: title } : {})
},
{
name: 'message',
message: 'Enter the commit message:',
...(message ? { default: message } : {})
}
]
}