@oclif/plugin-not-found
Version:
"did you mean" for oclif
36 lines (35 loc) • 1.08 kB
JavaScript
import { confirm } from '@inquirer/prompts';
import { blueBright, reset } from 'ansis';
import levenshtein from 'fast-levenshtein';
import { setTimeout } from 'node:timers/promises';
const getConfirmation = async (suggestion) => {
const ac = new AbortController();
const { signal } = ac;
const confirmation = confirm({
default: true,
message: `Did you mean ${blueBright(suggestion)}?`,
theme: {
prefix: '',
style: {
message: (text) => reset(text),
},
},
});
void setTimeout(10_000, 'timeout', { signal })
.catch(() => false)
.then(() => {
confirmation.cancel();
});
return confirmation.then((value) => {
ac.abort();
return value;
});
};
const closest = (target, possibilities) => possibilities
.map((id) => ({ distance: levenshtein.get(target, id, { useCollator: true }), id }))
.toSorted((a, b) => a.distance - b.distance)[0]?.id ?? '';
const exports = {
closest,
getConfirmation,
};
export default exports;