export async function runSamSync()

in packages/core/src/shared/sam/sync.ts [347:407]


export async function runSamSync(args: SyncParams) {
    telemetry.record({ lambdaPackageType: args.ecrRepoUri !== undefined ? 'Image' : 'Zip' })

    const { path: samCliPath, parsedVersion } = await getSamCliPathAndVersion()
    const { boundArgs } = await saveAndBindArgs(args)

    if (!!args.templateParameters && Object.entries(args.templateParameters).length > 0) {
        const templateParameters = new Map<string, string>(Object.entries(args.templateParameters))
        const paramsToSet: string[] = []
        for (const [key, value] of templateParameters.entries()) {
            if (value) {
                await updateRecentResponse(syncMementoRootKey, args.template.uri.fsPath, key, value)
                paramsToSet.push(`ParameterKey=${key},ParameterValue=${value}`)
            }
        }
        paramsToSet.length > 0 && boundArgs.push('--parameter-overrides', paramsToSet.join(' '))
    }

    // '--no-watch' was not added until https://github.com/aws/aws-sam-cli/releases/tag/v1.77.0
    // Forcing every user to upgrade will be a headache for what is otherwise a minor problem
    if ((parsedVersion?.compare('1.77.0') ?? -1) >= 0) {
        boundArgs.push('--no-watch')
    }

    if ((parsedVersion?.compare('1.98.0') ?? 1) < 0) {
        await showOnce('sam.sync.updateMessage', async () => {
            const message = `Your current version of SAM CLI (${parsedVersion?.version}) does not include the latest improvements for "sam sync". Some parameters may not be available. Update to the latest version to get all new parameters/options.`
            const learnMoreUrl = vscode.Uri.parse(
                'https://aws.amazon.com/about-aws/whats-new/2023/03/aws-toolkits-jetbrains-vs-code-sam-accelerate/'
            )
            const openDocsItem = 'Open Upgrade Documentation'
            const resp = await vscode.window.showInformationMessage(message, localizedText.learnMore, openDocsItem)
            if (resp === openDocsItem) {
                await openUrl(samUpgradeUrl)
            } else if (resp === localizedText.learnMore) {
                await openUrl(learnMoreUrl)
            }
        })
    }

    const syncFlags: string[] = args.syncFlags ? JSON.parse(args.syncFlags) : []
    boundArgs.push(...syncFlags)

    const sam = new ChildProcess(samCliPath, ['sync', ...resolveSyncArgConflict(boundArgs)], {
        spawnOptions: await addTelemetryEnvVar({
            cwd: args.projectRoot.fsPath,
            env: await getSpawnEnv(process.env, { promptForInvalidCredential: true }),
        }),
    })

    // with '--watch' selected, the sync process will run in the background until the user manually kills it
    // we need to save the stack and region to the samconfig file now, otherwise the user would not see latest deployed resoure during this sync process
    const { paramsSource, stackName, region, projectRoot } = args
    const shouldWriteSyncSamconfigGlobal = paramsSource !== ParamsSource.SamConfig && !!stackName && !!region
    if (boundArgs.includes('--watch')) {
        shouldWriteSyncSamconfigGlobal && (await writeSamconfigGlobal(projectRoot, stackName, region))
    }

    await runInTerminal(sam, 'sync')
    shouldWriteSyncSamconfigGlobal && (await writeSamconfigGlobal(projectRoot, stackName, region))
}