Skip to content

Commit 4169dda

Browse files
authored
feat(@142vip/utils): 优化promptSearch函数类型支持,优雅处理VipInquirer工具ctrl+c意外退出报错 (#475)
1 parent 2abe83c commit 4169dda

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

packages/utils/src/pkgs/inquirer.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
select,
1010
Separator,
1111
} from '@inquirer/prompts'
12+
import { name } from '../../package.json'
13+
import { vipLogger, VipPackageJSON } from '../core'
1214

1315
/**
1416
* 参考:
@@ -33,18 +35,17 @@ interface VipInquirerOptions {
3335
loop?: false
3436
}
3537

38+
type SearchSourceResponse<T> = (string | VipInquirerSeparator)[] | readonly (Separator | VipInquirerChoice<T>)[] | Promise<(string | VipInquirerSeparator)[]> | Promise<(VipInquirerSeparator | VipInquirerChoice<T>)[]>
39+
3640
/**
3741
* 搜索源
3842
*/
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>
4244

4345
/**
4446
* 简单搜索源
4547
*/
46-
type SimpleSearchSource<T> = (input: T) => T[]
47-
48+
type SimpleSearchSource<T> = (input: T | undefined) => SearchSourceResponse<T>
4849
/**
4950
* 输入框,只输入数字
5051
* - https://github.com/SBoudrias/Inquirer.js/tree/main/packages/number
@@ -126,24 +127,47 @@ async function promptSearch<T extends string>(message: string, source: SearchSou
126127
* 搜索源简单处理
127128
*/
128129
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+
}
130134
return sources.filter((name: string) => name.includes(input))
131135
}
132136
}
133137

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+
134158
/**
135159
* 终端交互
136160
*/
137161
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),
147171
handleSimpleSearchSource,
148172
}
149173

0 commit comments

Comments
 (0)