1 | import { CliValue, CliCommandDefinitionParameter, CliValueValidatorFunction } from './types';
|
2 | import { coerceCliValue, isFunction, isRegExp } from './utils';
|
3 | import { isLVal } from '@babel/types';
|
4 |
|
5 | export const getCommandDefinitionParameterValue = (
|
6 | value: CliValue,
|
7 | defParam: CliCommandDefinitionParameter,
|
8 | wasPassed = true
|
9 | ): CliValue => {
|
10 | let v = coerceCliValue(value || defParam.default, defParam.valueType);
|
11 | if (wasPassed && isFunction(defParam.transform)) {
|
12 | v = defParam.transform(v);
|
13 | }
|
14 | return v;
|
15 | };
|
16 |
|
17 | export const validateCommandDefinitionParameterValue = (
|
18 | value: CliValue,
|
19 | defParam: CliCommandDefinitionParameter
|
20 | ): boolean | string => {
|
21 | if (defParam.validate) {
|
22 | if (isFunction(defParam.valueType)) {
|
23 | const validator = defParam.validate as CliValueValidatorFunction;
|
24 | return validator(value);
|
25 | } else if (isRegExp(defParam.validate)) {
|
26 | const m = (defParam.validate as RegExp).exec(value as string);
|
27 | return !!m || `'${value}' fails RegExp validation: '${defParam.validate}'`;
|
28 | } else {
|
29 | console.assert(false);
|
30 |
|
31 | }
|
32 | }
|
33 | return true;
|
34 | };
|