import { RedspotArguments , RedspotParamDefinitions } from '../../../types' ; import { ArgumentsParser } from '../../cli/ArgumentsParser' ; import { unsafeObjectKeys } from '../../util/unsafe' ; import { RedspotError } from '../errors' ; import { ERRORS } from '../errors-list' ; import ProcessEnv = NodeJS . ProcessEnv ; const REDSPOT_ENV_ARGUMENT_PREFIX = 'REDSPOT_' ; export function paramNameToEnvVariable ( paramName ) { return ArgumentsParser . paramNameToCLA ( paramName ) . replace ( ArgumentsParser . PARAM_PREFIX , REDSPOT_ENV_ARGUMENT_PREFIX ) . replace ( /-/g , '_' ) . toUpperCase ( ) ; } export function getEnvVariablesMap ( redspotArguments ) { const values = { } ; for ( const [ name , value ] of Object . entries ( redspotArguments ) ) { if ( value === undefined ) { continue ; } values [ paramNameToEnvVariable ( name ) ] = value . toString ( ) ; } return values ; } export function getEnvRedspotArguments ( paramDefinitions , envVariables ) { const envArgs = { } ; for ( const paramName of unsafeObjectKeys ( paramDefinitions ) ) { const definition = paramDefinitions [ paramName ] ; const envVarName = paramNameToEnvVariable ( paramName ) ; const rawValue = envVariables [ envVarName ] ; if ( rawValue !== undefined ) { try { envArgs [ paramName ] = definition . type . parse ( paramName , rawValue ) ; } catch ( error ) { throw new RedspotError ( ERRORS . ARGUMENTS . INVALID_ENV_VAR_VALUE , { varName : envVarName , value : rawValue } , error ) ; } } else { envArgs [ paramName ] = definition . defaultValue ; } } return envArgs as RedspotArguments ; }