import { S3Client } from '@aws-sdk/client-s3' ; import { TogglesResp } from '.' ; import { getTogglesObject , putTogglesObject } from './s3-utils' ; import localToggles , { Toggle } from './toggles' ; export const withDefaultValuesUnmodified = ( from , to ) : Toggle [ ] => { /**
     * We don't deploy over the defaultValue as this can be set manually by
     * updating the toggle via setDefaultValueFor.
     *
     * If we have turned a toggle off manually - we expect it to remain
     * off even after a deploy.
     *
     */ const toggles = to . map ( toggle => { const { defaultValue } = from . find ( ( { id } ) => id === toggle . id ) ?? { defaultValue : toggle . defaultValue } ; return { ... toggle , defaultValue } ; } ) ; return toggles ; } ; export async function deploy ( client ) { const remoteToggles = await getTogglesObject ( client ) ; const togglesToDeploy = withDefaultValuesUnmodified ( remoteToggles . toggles , [ ... localToggles . toggles , ] ) ; const togglesAndTests = { toggles : togglesToDeploy , tests : localToggles . tests } ; const { $metadata : putObjectResponseMetadata } = await putTogglesObject ( client , togglesAndTests ) ; if ( putObjectResponseMetadata . httpStatusCode === 200 ) { console . info ( `Success!` ) ; } else { throw new Error ( `Error putting toggles in S3 ${ putObjectResponseMetadata }` ) ; } }