Skip to content

Commit 8605076

Browse files
author
Vandita Patidar
committed
Improving the shared utility function
1 parent 064ee11 commit 8605076

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +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'
18+
import { handleOverwriteConflict } from '../../../shared/utilities/messages'
1919
import { getPattern } from '../../../shared/utilities/downloadPatterns'
2020
import { MetadataManager } from './metadataManager'
2121

@@ -91,14 +91,8 @@ export async function downloadPatternCode(config: CreateServerlessLandWizardForm
9191
const fullAssetName = assetName + '.zip'
9292
const location = vscode.Uri.joinPath(config.location, config.name)
9393

94-
const shouldProceed = await confirmOverwriteIfExists(location, config.name)
95-
if (!shouldProceed) {
96-
throw new ToolkitError(`Folder already exists: ${config.name}`)
97-
}
94+
await handleOverwriteConflict(location, config.name)
9895

99-
if (await fs.exists(location)) {
100-
await fs.delete(location, { recursive: true, force: true })
101-
}
10296
try {
10397
await getPattern(serverlessLandOwner, serverlessLandRepo, fullAssetName, location, true)
10498
} catch (error) {

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

Lines changed: 17 additions & 3 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 { ToolkitError } from '../../shared/errors'
1718
import { fs } from '../../shared/fs/fs'
1819
import { openUrl } from './vsCodeUtils'
1920
import { AmazonQPromptSettings, ToolkitPromptSettings } from '../../shared/settings'
@@ -147,21 +148,34 @@ export async function showViewLogsMessage(
147148
* @param itemName The name of the item for display in the message
148149
* @returns Promise<boolean> - true if should proceed (path doesn't exist or user confirmed overwrite)
149150
*/
150-
export async function confirmOverwriteIfExists(path: vscode.Uri, itemName: string): Promise<boolean> {
151+
export async function handleOverwriteConflict(path: vscode.Uri, itemName: string): Promise<boolean> {
151152
if (!(await fs.exists(path))) {
152153
return true
153154
}
154155

155-
return showConfirmationMessage({
156+
const choice = showConfirmationMessage({
156157
prompt: localize(
157158
'AWS.toolkit.confirmOverwrite',
158159
'{0} already exists in the selected directory, overwrite?',
159-
itemName
160+
itemName,
161+
path.fsPath
160162
),
161163
confirm: localize('AWS.generic.overwrite', 'Yes'),
162164
cancel: localize('AWS.generic.cancel', 'No'),
163165
type: 'warning',
164166
})
167+
168+
if (!choice) {
169+
throw new ToolkitError(`Folder already exists: ${itemName}`)
170+
}
171+
172+
try {
173+
await fs.delete(path, { recursive: true, force: true })
174+
} catch (error) {
175+
throw new ToolkitError(`Failed to delete existing folder: ${itemName}`)
176+
}
177+
178+
return true
165179
}
166180

167181
/**

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,20 @@ describe('downloadPatternCode', () => {
117117
getPatternStub.restore()
118118
})
119119
it('successfully downloads pattern code', async () => {
120-
sandbox.stub(messages, 'confirmOverwriteIfExists').resolves(true)
120+
sandbox.stub(messages, 'handleOverwriteConflict').resolves(true)
121121

122122
await downloadPatternCode(mockConfig, mockConfig.assetName)
123123
assertDownloadPatternCall(getPatternStub, mockConfig)
124124
})
125125
it('downloads pattern when directory exists and user confirms overwrite', async function () {
126-
sandbox.stub(messages, 'confirmOverwriteIfExists').resolves(true)
127-
sandbox.stub(fs, 'delete').resolves()
126+
sandbox.stub(messages, 'handleOverwriteConflict').resolves(true)
128127

129128
await downloadPatternCode(mockConfig, mockConfig.assetName)
130129
assertDownloadPatternCall(getPatternStub, mockConfig)
131130
})
132131
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)
132+
const handleOverwriteStub = sandbox.stub(messages, 'handleOverwriteConflict')
133+
handleOverwriteStub.rejects(new Error('Folder already exists: test-project'))
135134

136135
await assert.rejects(
137136
() => downloadPatternCode(mockConfig, mockConfig.assetName),

0 commit comments

Comments
 (0)