Skip to content

Commit f4bb273

Browse files
author
Vandita Patidar
committed
Creating shared utility function
1 parent 3b02aac commit f4bb273

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

packages/core/src/awsService/appBuilder/serverlessLand/main.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ExtContext } from '../../../shared/extensions'
1515
import { addFolderToWorkspace } from '../../../shared/utilities/workspaceUtils'
1616
import { ToolkitError } from '../../../shared/errors'
1717
import { fs } from '../../../shared/fs/fs'
18+
import { confirmOverwriteIfExists } from '../../../shared/utilities/messages'
1819
import { getPattern } from '../../../shared/utilities/downloadPatterns'
1920
import { MetadataManager } from './metadataManager'
2021

@@ -90,19 +91,12 @@ export async function downloadPatternCode(config: CreateServerlessLandWizardForm
9091
const fullAssetName = assetName + '.zip'
9192
const location = vscode.Uri.joinPath(config.location, config.name)
9293

94+
const shouldProceed = await confirmOverwriteIfExists(location, config.name)
95+
if (!shouldProceed) {
96+
throw new ToolkitError(`Folder already exists: ${config.name}`)
97+
}
98+
9399
if (await fs.exists(location)) {
94-
const choice = await vscode.window.showInformationMessage(
95-
localize(
96-
'AWS.toolkit.serverlessLand.fileExistsPrompt',
97-
'{0} already exists in the selected directory, overwrite?',
98-
config.name
99-
),
100-
'Yes',
101-
'No'
102-
)
103-
if (choice !== 'Yes') {
104-
throw new ToolkitError(`Folder already exists: ${config.name}`)
105-
}
106100
await vscode.workspace.fs.delete(location)
107101
}
108102
try {

packages/core/src/shared/utilities/messages.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Timeout } from './timeoutUtils'
1414
import { addCodiconToString } from './textUtilities'
1515
import { getIcon, codicon } from '../icons'
1616
import globals from '../extensionGlobals'
17+
import { fs } from '../../shared/fs/fs'
1718
import { openUrl } from './vsCodeUtils'
1819
import { AmazonQPromptSettings, ToolkitPromptSettings } from '../../shared/settings'
1920
import { telemetry, ToolkitShowNotification } from '../telemetry/telemetry'
@@ -140,6 +141,29 @@ export async function showViewLogsMessage(
140141
})
141142
}
142143

144+
/**
145+
* Checks if a path exists and prompts user for overwrite confirmation if it does.
146+
* @param path The file or directory path to check
147+
* @param itemName The name of the item for display in the message
148+
* @returns Promise<boolean> - true if should proceed (path doesn't exist or user confirmed overwrite)
149+
*/
150+
export async function confirmOverwriteIfExists(path: vscode.Uri, itemName: string): Promise<boolean> {
151+
if (!(await fs.exists(path))) {
152+
return true
153+
}
154+
155+
return showConfirmationMessage({
156+
prompt: localize(
157+
'AWS.toolkit.confirmOverwrite',
158+
'{0} already exists in the selected directory, overwrite?',
159+
itemName
160+
),
161+
confirm: localize('AWS.generic.overwrite', 'Yes'),
162+
cancel: localize('AWS.generic.cancel', 'No'),
163+
type: 'warning',
164+
})
165+
}
166+
143167
/**
144168
* Shows a modal confirmation (warning) message with buttons to confirm or cancel.
145169
*

packages/core/src/test/awsService/appBuilder/serverlessLand/main.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import {
1919
getProjectUri,
2020
downloadPatternCode,
2121
} from '../../../../awsService/appBuilder/serverlessLand/main'
22-
import { getTestWindow } from '../../../shared/vscode/window'
2322
import { fs } from '../../../../shared/fs/fs'
2423
import * as downloadPatterns from '../../../../shared/utilities/downloadPatterns'
2524
import { ExtContext } from '../../../../shared/extensions'
2625
import { workspaceUtils } from '../../../../shared'
26+
import * as messages from '../../../../shared/utilities/messages'
2727
import * as downloadPattern from '../../../../shared/utilities/downloadPatterns'
2828
import * as wizardModule from '../../../../awsService/appBuilder/serverlessLand/wizard'
2929

@@ -116,20 +116,28 @@ describe('downloadPatternCode', () => {
116116
sandbox.restore()
117117
getPatternStub.restore()
118118
})
119-
120119
it('successfully downloads pattern code', async () => {
120+
sandbox.stub(messages, 'confirmOverwriteIfExists').resolves(true)
121+
121122
await downloadPatternCode(mockConfig, mockConfig.assetName)
122123
assertDownloadPatternCall(getPatternStub, mockConfig)
123124
})
124-
125125
it('downloads pattern when directory exists and user confirms overwrite', async function () {
126-
getTestWindow().onDidShowMessage((message) => {
127-
message.selectItem('Yes')
128-
})
126+
sandbox.stub(messages, 'confirmOverwriteIfExists').resolves(true)
127+
sandbox.stub(vscode.workspace, 'fs').value({ delete: sandbox.stub().resolves() })
129128

130129
await downloadPatternCode(mockConfig, mockConfig.assetName)
131130
assertDownloadPatternCall(getPatternStub, mockConfig)
132131
})
132+
it('throws error when directory exists and user cancels overwrite', async function () {
133+
sandbox.stub(fs, 'exists').resolves(true)
134+
sandbox.stub(messages, 'confirmOverwriteIfExists').resolves(false)
135+
136+
await assert.rejects(
137+
() => downloadPatternCode(mockConfig, mockConfig.assetName),
138+
/Folder already exists: test-project/
139+
)
140+
})
133141
})
134142

135143
describe('openReadmeFile', () => {

0 commit comments

Comments
 (0)