@@ -2,47 +2,30 @@ import type { CAC } from "cac";
2
2
3
3
import enquirer from "enquirer" ;
4
4
5
- import { ACTIVATION , gitCommitScopes , gitCommitTypes } from "@/shared/config" ;
6
- import { CommitScope , CommitType } from "@/shared/types" ;
7
5
import { loggerInfo , execCommand } from "@/shared/index" ;
8
- interface PromptResult {
9
- type : string ;
10
- scope : string ;
11
- description : string ;
12
- }
13
-
14
- interface GitCommitOptions {
15
- emoji : boolean ;
16
- }
6
+ import { ACTIVATION , gitCommitScopes , gitCommitTypes } from "@/shared/config" ;
7
+ import { GitCommitOptions } from "@/shared/types" ;
17
8
18
- export const gitCommit = async (
19
- types : Array < CommitType > ,
20
- scopes : Array < CommitScope > ,
21
- options : GitCommitOptions ,
22
- ) => {
23
- if ( ACTIVATION ) {
24
- loggerInfo (
25
- `gitCommit 参数信息: \ntypes ${ types } \nscopes${ scopes } \noptions${ options } ` ,
26
- ) ;
27
- }
28
- const { emoji : emojiStatus } = options ;
29
- const typesChoices = types . map ( ( { emoji, code, description } ) => {
9
+ const generateEnquirer = async ( ) : Promise <
10
+ Pick < GitCommitOptions , Exclude < keyof GitCommitOptions , "emoji" > >
11
+ > => {
12
+ const typesChoices = gitCommitTypes . map ( ( { emoji, code, description } ) => {
30
13
const formatCode = `${ code } :` . padEnd ( 20 ) ;
31
14
return {
32
- name : emojiStatus ? `${ emoji } ${ code } ` : code ,
15
+ name : `${ emoji } ${ code } ` ,
33
16
message : `${ emoji } ${ formatCode } ${ description } ` ,
34
17
} ;
35
18
} ) ;
36
19
37
- const scopesChoices = scopes . map ( ( { name, description } ) => {
20
+ const scopesChoices = gitCommitScopes . map ( ( { name, description } ) => {
38
21
const formatName = `${ name } :` . padEnd ( 20 ) ;
39
22
return {
40
23
name,
41
24
message : `${ formatName . padEnd ( 20 ) } ${ description } ` ,
42
25
} ;
43
26
} ) ;
44
27
45
- const result = await enquirer . prompt < PromptResult > ( [
28
+ const result = await enquirer . prompt < GitCommitOptions > ( [
46
29
{
47
30
name : "type" ,
48
31
type : "select" ,
@@ -60,9 +43,23 @@ export const gitCommit = async (
60
43
type : "text" ,
61
44
message : "请输入提交描述" ,
62
45
} ,
46
+ {
47
+ name : "emoji" ,
48
+ type : "confirm" ,
49
+ message : "要在提交信息中显示内置的 emoji 表情吗?" ,
50
+ } ,
63
51
] ) ;
52
+ return {
53
+ type : result . emoji ? result . type : result . type . split ( " " ) [ 1 ] ,
54
+ scope : result . scope ,
55
+ description : result . description ,
56
+ } ;
57
+ } ;
64
58
65
- const content = `${ result . type } (${ result . scope } ): ${ result . description } ` ;
59
+ export const gitCommit = async ( content : string ) => {
60
+ if ( ACTIVATION ) {
61
+ loggerInfo ( `gitCommit 参数信息: \n${ JSON . stringify ( content ) } ` ) ;
62
+ }
66
63
await execCommand ( "git" , [ "commit" , "-m" , content ] , { stdio : "inherit" } ) ;
67
64
} ;
68
65
@@ -72,12 +69,19 @@ export default function gitCommitInstaller(cli: CAC) {
72
69
setup : ( ) => {
73
70
cli
74
71
. command ( "commit" , "生成 angualr 规范的提交信息" )
75
- . option ( "--no-emoji" , "禁用 emoji" )
72
+ . option ( "-t, --type <type>" , "添加修改类型" )
73
+ . option ( "-s, --scope <scope>" , "填写修改范围" )
74
+ . option ( "-d, --description <description>" , "填写修改描述" )
76
75
. action ( async ( options ) => {
77
- const { emoji } = options ;
78
- await gitCommit ( gitCommitTypes , gitCommitScopes , {
79
- emoji,
80
- } ) ;
76
+ const { type, scope, description } = options ;
77
+ let content = "" ;
78
+ if ( ! type || ! scope || ! description ) {
79
+ const result = await generateEnquirer ( ) ;
80
+ content = `${ result . type } (${ result . scope } ): ${ result . description } ` ;
81
+ } else {
82
+ content = `${ type } (${ scope } ): ${ description } ` ;
83
+ }
84
+ await gitCommit ( content ) ;
81
85
} ) ;
82
86
} ,
83
87
} ;
0 commit comments