Skip to content

Commit dbb7095

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

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
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)
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: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as vscode from 'vscode'
77
import * as nls from 'vscode-nls'
8+
import * as path from 'path'
89
import * as localizedText from '../localizedText'
910
import { getLogger } from '../../shared/logger/logger'
1011
import { ProgressEntry } from '../../shared/vscode/window'
@@ -14,6 +15,7 @@ import { Timeout } from './timeoutUtils'
1415
import { addCodiconToString } from './textUtilities'
1516
import { getIcon, codicon } from '../icons'
1617
import globals from '../extensionGlobals'
18+
import { ToolkitError } from '../../shared/errors'
1719
import { fs } from '../../shared/fs/fs'
1820
import { openUrl } from './vsCodeUtils'
1921
import { AmazonQPromptSettings, ToolkitPromptSettings } from '../../shared/settings'
@@ -147,21 +149,33 @@ export async function showViewLogsMessage(
147149
* @param itemName The name of the item for display in the message
148150
* @returns Promise<boolean> - true if should proceed (path doesn't exist or user confirmed overwrite)
149151
*/
150-
export async function confirmOverwriteIfExists(path: vscode.Uri, itemName: string): Promise<boolean> {
151-
if (!(await fs.exists(path))) {
152+
export async function handleOverwriteConflict(location: vscode.Uri): Promise<boolean> {
153+
if (!(await fs.exists(location))) {
152154
return true
153155
}
154156

155-
return showConfirmationMessage({
157+
const choice = showConfirmationMessage({
156158
prompt: localize(
157159
'AWS.toolkit.confirmOverwrite',
158160
'{0} already exists in the selected directory, overwrite?',
159-
itemName
161+
location.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: ${path.basename(location.fsPath)}`)
170+
}
171+
172+
try {
173+
await fs.delete(location, { recursive: true, force: true })
174+
} catch (error) {
175+
throw ToolkitError.chain(error, `Failed to delete existing folder: ${path.basename(location.fsPath)}`)
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)