9
9
select ,
10
10
Separator ,
11
11
} from '@inquirer/prompts'
12
+ import { name } from '../../package.json'
13
+ import { vipLogger , VipPackageJSON } from '../core'
12
14
13
15
/**
14
16
* 参考:
@@ -33,18 +35,17 @@ interface VipInquirerOptions {
33
35
loop ?: false
34
36
}
35
37
38
+ type SearchSourceResponse < T > = ( string | VipInquirerSeparator ) [ ] | readonly ( Separator | VipInquirerChoice < T > ) [ ] | Promise < ( string | VipInquirerSeparator ) [ ] > | Promise < ( VipInquirerSeparator | VipInquirerChoice < T > ) [ ] >
39
+
36
40
/**
37
41
* 搜索源
38
42
*/
39
- type SearchSource < T > = ( term : string | undefined , opt : {
40
- signal : AbortSignal
41
- } ) => ( string | VipInquirerSeparator ) [ ] | readonly ( Separator | VipInquirerChoice < T > ) [ ] | Promise < ( string | VipInquirerSeparator ) [ ] > | Promise < ( VipInquirerSeparator | VipInquirerChoice < T > ) [ ] >
43
+ type SearchSource < T > = ( term : string | undefined , opt : { signal : AbortSignal } ) => SearchSourceResponse < T >
42
44
43
45
/**
44
46
* 简单搜索源
45
47
*/
46
- type SimpleSearchSource < T > = ( input : T ) => T [ ]
47
-
48
+ type SimpleSearchSource < T > = ( input : T | undefined ) => SearchSourceResponse < T >
48
49
/**
49
50
* 输入框,只输入数字
50
51
* - https://github.com/SBoudrias/Inquirer.js/tree/main/packages/number
@@ -126,24 +127,47 @@ async function promptSearch<T extends string>(message: string, source: SearchSou
126
127
* 搜索源简单处理
127
128
*/
128
129
function handleSimpleSearchSource ( sources : string [ ] ) : SimpleSearchSource < string > {
129
- return function ( input : string ) {
130
+ return function ( input : string | undefined ) : string [ ] {
131
+ if ( input == null ) {
132
+ return sources
133
+ }
130
134
return sources . filter ( ( name : string ) => name . includes ( input ) )
131
135
}
132
136
}
133
137
138
+ /**
139
+ * 使用try catch 处理Prompt退出时报错
140
+ * - ctrl+c 优雅地处理
141
+ */
142
+ function withTryCatch < F extends ( ...args : any [ ] ) => any > ( fn : F ) : F {
143
+ return ( async ( ...args : Parameters < F > ) => {
144
+ try {
145
+ return await fn ( ...args )
146
+ }
147
+ catch ( error ) {
148
+ if ( error instanceof Error && error . name === 'ExitPromptError' ) {
149
+ vipLogger . logByBlank ( `${ VipPackageJSON . getPkgGreenLabel ( name ) } 用户安全退出,欢迎下次使用👏🏻👏🏻👏🏻` )
150
+ }
151
+ else {
152
+ throw error
153
+ }
154
+ }
155
+ } ) as F
156
+ }
157
+
134
158
/**
135
159
* 终端交互
136
160
*/
137
161
export const VipInquirer = {
138
- promptList,
139
- promptInput,
140
- promptInputRequired,
141
- promptNumber,
142
- promptPassword,
143
- promptSelect,
144
- promptCheckBox,
145
- promptConfirm,
146
- promptSearch,
162
+ promptList : withTryCatch ( promptList ) ,
163
+ promptInput : withTryCatch ( promptInput ) ,
164
+ promptInputRequired : withTryCatch ( promptInputRequired ) ,
165
+ promptNumber : withTryCatch ( promptNumber ) ,
166
+ promptPassword : withTryCatch ( promptPassword ) ,
167
+ promptSelect : withTryCatch ( promptSelect ) ,
168
+ promptCheckBox : withTryCatch ( promptCheckBox ) ,
169
+ promptConfirm : withTryCatch ( promptConfirm ) ,
170
+ promptSearch : withTryCatch ( promptSearch ) ,
147
171
handleSimpleSearchSource,
148
172
}
149
173
0 commit comments