import * as fs from 'fs' ; import fsExtra from 'fs-extra' ; import { ArgumentType , CLIArgumentType } from '../../../types' ; import { RedspotError } from '../errors' ; import { ERRORS } from '../errors-list' ; /**
 * String type.
 *
 * Accepts any kind of string.
 */ export const string = { name : 'string' , parse : ( argName , strValue ) => strValue , param argName { string } param value { any } throws validate : ( argName , value ) : void => { const isString = typeof value === 'string' ; if ( ! isString ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value , name : argName , type : string . name } ) ; } } } ; throws export const boolean = { name : 'boolean' , parse : ( argName , strValue ) => { if ( strValue . toLowerCase ( ) === 'true' ) { return true ; } if ( strValue . toLowerCase ( ) === 'false' ) { return false ; } throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value : strValue , name : argName , type : 'boolean' } ) ; } , param argName { string } param value { any } throws validate : ( argName , value ) : void => { const isBoolean = typeof value === 'boolean' ; if ( ! isBoolean ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value , name : argName , type : boolean . name } ) ; } } } ; throws export const int = { name : 'int' , parse : ( argName , strValue ) => { const decimalPattern = /^\d+(?:[eE]\d+)?$/ ; const hexPattern = /^0[xX][\dABCDEabcde]+$/ ; if ( strValue . match ( decimalPattern ) === null && strValue . match ( hexPattern ) === null ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value : strValue , name : argName , type : int . name } ) ; } return Number ( strValue ) ; } , param argName { string } param value { any } throws validate : ( argName , value ) : void => { const isInt = Number . isInteger ( value ) ; if ( ! isInt ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value , name : argName , type : int . name } ) ; } } } ; throws export const float = { name : 'float' , parse : ( argName , strValue ) => { const decimalPattern = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]\d+)?$/ ; const hexPattern = /^0[xX][\dABCDEabcde]+$/ ; if ( strValue . match ( decimalPattern ) === null && strValue . match ( hexPattern ) === null ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value : strValue , name : argName , type : float . name } ) ; } return Number ( strValue ) ; } , param argName { string } param value { any } throws validate : ( argName , value ) : void => { const isFloatOrInteger = typeof value === 'number' && ! isNaN ( value ) ; if ( ! isFloatOrInteger ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value , name : argName , type : float . name } ) ; } } } ; throws export const inputFile = { name : 'inputFile' , parse ( argName , strValue ) { try { fs . accessSync ( strValue , fsExtra . constants . R_OK ) ; const stats = fs . lstatSync ( strValue ) ; if ( stats . isDirectory ( ) ) { throw new Error ( `${ strValue } is a directory, not a file` ) ; } } catch ( error ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_INPUT_FILE , { name : argName , value : strValue } , error ) ; } return strValue ; } , param argName { string } param value { any } throws validate : ( argName , value ) : void => { try { inputFile . parse ( argName , value ) ; } catch ( error ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value , name : argName , type : inputFile . name } , error ) ; } } } ; export const json = { name : 'json' , parse ( argName , strValue ) { try { return JSON . parse ( strValue ) ; } catch ( error ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_JSON_ARGUMENT , { param : argName , error : error . message } , error ) ; } } , param argName { string } param value { any } throws validate : ( argName , value ) : void => { if ( value === undefined ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_VALUE_FOR_TYPE , { value , name : argName , type : json . name } ) ; } } } ; export const any = { name : 'any' , validate ( argName , argumentValue ) { } } ;