import Client from './client' ; import { Output } from './output/create-output' ; import { ProjectEnvTarget , ProjectEnvType , ProjectEnvVariable , Secret , } from '../types' ; import getEnvRecords from './env/get-env-records' ; export default async function getDecryptedEnvRecords ( output , client , projectId ) { const { envs } = await getEnvRecords ( output , client , projectId , { target : ProjectEnvTarget . Development , decrypt : true , } ) ; const envsWithDecryptedSecrets = await Promise . all ( envs . map ( async ( { id , type , key , value } ) => { if ( type === ProjectEnvType . Secret ) { try { const secretIdOrName = value ; if ( ! secretIdOrName ) { return { id , type , key , value : '' , found : true } ; } output . debug ( `Fetching decrypted secret ${ secretIdOrName }` ) ; const secret = await client . fetch < Secret > ( `/v2/now/secrets/${ secretIdOrName }?decrypt=true` ) ; return { id , type , key , value : secret . value , found : true } ; } catch ( error ) { if ( error && error . status === 404 ) { return { id , type , key , value : '' , found : false } ; } throw error ; } } return { id , type , key , value , found : true } ; } ) ) ; for ( let env of envsWithDecryptedSecrets ) { if ( ! env . found ) { output . print ( '' ) ; output . warn ( `Unable to download variable ${ env . key } because associated secret was deleted` ) ; } } return { envs : envsWithDecryptedSecrets } ; }