-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore-inputs.service.ts
87 lines (74 loc) · 3.19 KB
/
core-inputs.service.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { EInputs } from '@core/inputs/inputs.enum';
import { IInput } from '@core/inputs/types/input';
import { IInputs } from '@core/inputs/types/inputs';
import { AnnotationsService } from '@utils/annotations/annotations.service';
import { EAnnotationError } from '@utils/annotations/enums/annotation-error.enum';
import { EAnnotationWarningIssue } from '@utils/annotations/enums/annotation-warning-issue.enum';
import { LoggerFormatService } from '@utils/loggers/logger-format.service';
import { LoggerService } from '@utils/loggers/logger.service';
import { isFiniteNumber } from '@utils/numbers/is-finite-number';
import { ETreeRows } from '@utils/trees/tree-rows.enum';
import * as core from '@actions/core';
import { InputOptions } from '@actions/core';
import _ from 'lodash';
export class CoreInputsService {
public static logInputs(groupName: Readonly<string>, inputs: Readonly<IInputs>): CoreInputsService {
LoggerService.startGroup(groupName);
_.forIn(inputs, (value: Readonly<IInput>, inputName: Readonly<string>, inputs: Readonly<IInputs>): void => {
const lastInputName: string | undefined = _.findLastKey(inputs, (): true => true);
LoggerService.info(
LoggerFormatService.white(inputName === lastInputName ? ETreeRows.LAST : ETreeRows.ANY),
LoggerService.input(_.kebabCase(inputName) as EInputs),
LoggerService.value(value)
);
});
LoggerService.endGroup();
return CoreInputsService;
}
public static getNumberInput$$(input: Readonly<EInputs>, options?: Readonly<InputOptions>): number | never {
const inputValue: string = core.getInput(input, options);
const value: number = _.parseInt(inputValue);
if (!isFiniteNumber(value)) {
LoggerService.error(
`Wrong value given to the input`,
LoggerService.value(input),
LoggerFormatService.white(`->`),
LoggerService.value(inputValue)
);
AnnotationsService.error(EAnnotationError.WRONG_INPUT_VALUE, {
file: `core-inputs.service.ts`,
startLine: 35,
title: `Error`,
});
throw new Error(`Wrong value given to the input number ${input}`);
}
return value;
}
public static getEnumInput$$<TEnum>(
input: Readonly<EInputs>,
parser: (value: TEnum | string) => TEnum | undefined,
fallback: Readonly<TEnum>,
options?: Readonly<InputOptions>
): TEnum {
const inputValue: TEnum | string = core.getInput(input, options);
// The parser should check if the value belong to the enum
// If not, it should return undefined and this method will use the fallback value instead
if (!parser(inputValue)) {
LoggerService.warning(
`Wrong value given to the input`,
LoggerService.value(input),
LoggerFormatService.white(`->`),
LoggerService.value(inputValue)
);
AnnotationsService.warning(EAnnotationWarningIssue.WRONG_INPUT_VALUE, {
file: `core-inputs.service.ts`,
startLine: 57,
title: `Warning`,
});
LoggerService.info(`Falling back instead to`, LoggerService.value(_.toString(fallback)));
return fallback;
}
// TODO find a way to get rid of this type hack
return inputValue as unknown as TEnum;
}
}