-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.utils.ts
More file actions
38 lines (31 loc) · 1005 Bytes
/
input.utils.ts
File metadata and controls
38 lines (31 loc) · 1005 Bytes
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
35
36
37
38
import inquirer from 'inquirer';
import {
type InputPromptDto,
InputTypeEnum,
InputUtilsCustomChoiceEnum,
type InputUtils as InputUtilsInterface,
} from '@/interfaces';
export class InputUtils implements InputUtilsInterface {
public async prompt<T = string>(input: InputPromptDto) {
try {
const promptDto = [{ name: 'data', ...input }];
if (input.type === InputTypeEnum.List) {
const choicesWithCustom =
input.type === InputTypeEnum.List &&
input.choices.map((choice) =>
choice === InputUtilsCustomChoiceEnum.Separator
? new inquirer.Separator()
: choice,
);
Object.assign(promptDto[0], { choices: choicesWithCustom });
}
const { data } = await inquirer.prompt<{ data: T }>(promptDto as never);
return data;
} catch (error) {
if (error.message === 'User force closed the prompt with 0 null') {
process.exit(0);
}
throw error;
}
}
}