-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.ts
34 lines (29 loc) · 918 Bytes
/
utils.ts
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
import confirm from '@inquirer/confirm'
import chalk from 'chalk'
import {default as levenshtein} from 'fast-levenshtein'
import {setTimeout} from 'node:timers/promises'
const getConfirmation = async (suggestion: string): Promise<boolean> => {
const confirmation = confirm({
default: true,
message: `Did you mean ${chalk.blueBright(suggestion)}?`,
theme: {
prefix: '',
style: {
message: (text: string) => chalk.reset(text),
},
},
})
const defaultValue = setTimeout(10_000).then(() => {
confirmation.cancel()
return false
})
return Promise.race([defaultValue, confirmation])
}
const closest = (target: string, possibilities: string[]): string =>
possibilities
.map((id) => ({distance: levenshtein.get(target, id, {useCollator: true}), id}))
.sort((a, b) => a.distance - b.distance)[0]?.id ?? ''
export default {
closest,
getConfirmation,
}