Skip to content

Commit ada2ccb

Browse files
committed
refactor sync entry point logic
1 parent b00e23c commit ada2ccb

File tree

1 file changed

+70
-28
lines changed
  • packages/core/src/shared/sam

1 file changed

+70
-28
lines changed

packages/core/src/shared/sam/sync.ts

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { openUrl } from '../utilities/vsCodeUtils'
3535
import { showOnce } from '../utilities/messages'
3636
import { IamConnection } from '../../auth/connection'
3737
import { CloudFormationTemplateRegistry } from '../fs/templateRegistry'
38-
import { TreeNode } from '../treeview/resourceTreeDataProvider'
38+
import { isTreeNode, TreeNode } from '../treeview/resourceTreeDataProvider'
3939
import { getSpawnEnv } from '../env/resolveEnv'
4040
import {
4141
getProjectRoot,
@@ -151,6 +151,29 @@ export const syncFlagItems: DataQuickPickItem<string>[] = [
151151
},
152152
]
153153

154+
export enum SamSyncEntryPoints {
155+
SamTemplateFile,
156+
SamConfigFile,
157+
RegionNodeContextMenu,
158+
AppBuilderNodeButton,
159+
CommandPalette,
160+
}
161+
162+
function getSyncEntryPoint(arg: vscode.Uri | AWSTreeNodeBase | TreeNode | undefined) {
163+
if (arg instanceof vscode.Uri) {
164+
if (arg.path.endsWith('samconfig.toml')) {
165+
return SamSyncEntryPoints.SamConfigFile
166+
}
167+
return SamSyncEntryPoints.SamTemplateFile
168+
} else if (arg instanceof AWSTreeNodeBase) {
169+
return SamSyncEntryPoints.RegionNodeContextMenu
170+
} else if (isTreeNode(arg)) {
171+
return SamSyncEntryPoints.AppBuilderNodeButton
172+
} else {
173+
return SamSyncEntryPoints.CommandPalette
174+
}
175+
}
176+
154177
export class SyncWizard extends Wizard<SyncParams> {
155178
registry: CloudFormationTemplateRegistry
156179
public constructor(
@@ -175,6 +198,7 @@ export class SyncWizard extends Wizard<SyncParams> {
175198
const existValidSamConfig: boolean | undefined = await validateSamSyncConfig(projectRoot)
176199
return createSyncParamsSourcePrompter(existValidSamConfig)
177200
})
201+
178202
this.form.region.bindPrompter(() => createRegionPrompter().transform((r) => r.id), {
179203
showWhen: ({ paramsSource }) =>
180204
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
@@ -434,21 +458,30 @@ export async function prepareSyncParams(
434458
): Promise<Partial<SyncParams>> {
435459
// Skip creating dependency layers by default for backwards compat
436460
const baseParams: Partial<SyncParams> = { skipDependencyLayer: true }
461+
const entryPoint = getSyncEntryPoint(arg)
437462

438-
if (arg instanceof AWSTreeNodeBase) {
439-
// "Deploy" command was invoked on a regionNode.
440-
return { ...baseParams, region: arg.regionCode }
441-
} else if (arg instanceof vscode.Uri) {
442-
if (arg.path.endsWith('samconfig.toml')) {
443-
// "Deploy" command was invoked on a samconfig.toml file.
444-
// TODO: add step to verify samconfig content to skip param source prompter
445-
const config = await SamConfig.fromConfigFileUri(arg)
463+
switch (entryPoint) {
464+
case SamSyncEntryPoints.SamTemplateFile: {
465+
const entryPointArg = arg as vscode.Uri
466+
const template = {
467+
uri: entryPointArg,
468+
data: await CloudFormation.load(entryPointArg.fsPath, validate),
469+
}
470+
471+
return {
472+
...baseParams,
473+
template: template,
474+
projectRoot: getProjectRootUri(template.uri),
475+
}
476+
}
477+
case SamSyncEntryPoints.SamConfigFile: {
478+
const config = await SamConfig.fromConfigFileUri(arg as vscode.Uri)
446479
const params = getSyncParamsFromConfig(config)
447480
const projectRoot = vscode.Uri.joinPath(config.location, '..')
448481
const templateUri = params.templatePath
449482
? vscode.Uri.file(path.resolve(projectRoot.fsPath, params.templatePath))
450483
: undefined
451-
const template = templateUri
484+
const samConfigFileTemplate = templateUri
452485
? {
453486
uri: templateUri,
454487
data: await CloudFormation.load(templateUri.fsPath),
@@ -457,29 +490,38 @@ export async function prepareSyncParams(
457490
// Always use the dependency layer if the user specified to do so
458491
const skipDependencyLayer = !config.getCommandParam('sync', 'dependency_layer')
459492

460-
return { ...baseParams, ...params, template, projectRoot, skipDependencyLayer } as SyncParams
493+
return {
494+
...baseParams,
495+
...params,
496+
template: samConfigFileTemplate,
497+
projectRoot,
498+
skipDependencyLayer,
499+
} as SyncParams
461500
}
462-
463-
// "Deploy" command was invoked on a template.yaml file.
464-
const template = {
465-
uri: arg,
466-
data: await CloudFormation.load(arg.fsPath, validate),
501+
case SamSyncEntryPoints.RegionNodeContextMenu: {
502+
const entryPointArg = arg as AWSTreeNodeBase
503+
return { ...baseParams, region: entryPointArg.regionCode }
467504
}
468-
469-
return { ...baseParams, template, projectRoot: getProjectRootUri(template.uri) }
470-
} else if (arg && arg.getTreeItem()) {
471-
// "Deploy" command was invoked on a TreeNode on the AppBuilder.
472-
const templateUri = (arg.getTreeItem() as vscode.TreeItem).resourceUri
473-
if (templateUri) {
474-
const template = {
475-
uri: templateUri,
476-
data: await CloudFormation.load(templateUri.fsPath, validate),
505+
case SamSyncEntryPoints.AppBuilderNodeButton: {
506+
const entryPointArg = arg as TreeNode
507+
const templateUri = (entryPointArg.getTreeItem() as vscode.TreeItem).resourceUri
508+
if (templateUri) {
509+
const template = {
510+
uri: templateUri,
511+
data: await CloudFormation.load(templateUri.fsPath, validate),
512+
}
513+
return {
514+
...baseParams,
515+
template,
516+
projectRoot: getProjectRootUri(templateUri),
517+
}
477518
}
478-
return { ...baseParams, template, projectRoot: getProjectRootUri(template.uri) }
519+
return baseParams
479520
}
521+
case SamSyncEntryPoints.CommandPalette:
522+
default:
523+
return baseParams
480524
}
481-
482-
return baseParams
483525
}
484526

485527
export type SamSyncResult = {

0 commit comments

Comments
 (0)