Skip to content

Commit 124e059

Browse files
committed
address comments
1 parent ea77516 commit 124e059

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ import { getSpawnEnv } from '../env/resolveEnv'
2222
import {
2323
getErrorCode,
2424
getProjectRoot,
25+
getRecentResponse,
2526
getSamCliPathAndVersion,
2627
getTerminalFromError,
2728
isDotnetRuntime,
28-
registerTemplateBuild,
29-
throwIfTemplateIsBeingBuilt,
30-
unregisterTemplateBuild,
3129
updateRecentResponse,
3230
} from './utils'
3331
import { getConfigFileUri, validateSamBuildConfig } from './config'
3432
import { runInTerminal } from './processTerminal'
35-
import { buildMementoRootKey } from './constants'
33+
import { buildMementoRootKey, buildProcessMementoRootKey, globalIdentifier } from './constants'
3634
import { SemVer } from 'semver'
3735

3836
export interface BuildParams {
@@ -266,11 +264,12 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
266264
isSuccess: true,
267265
}
268266
} catch (error) {
269-
await unregisterTemplateBuild(params.template.uri.path)
270267
throw ToolkitError.chain(error, 'Failed to build SAM template', {
271268
details: { terminal: getTerminalFromError(error), ...resolveBuildArgConflict(buildFlags) },
272269
code: getErrorCode(error),
273270
})
271+
} finally {
272+
await unregisterTemplateBuild(params.template.uri.path)
274273
}
275274
}
276275

@@ -312,3 +311,34 @@ export async function resolveBuildFlags(buildFlags: string[], samCliVersion: Sem
312311
}
313312
return buildFlags
314313
}
314+
315+
/**
316+
* Returns true if there's an ongoing build process for the provided template, false otherwise
317+
* @Param templatePath The path to the template.yaml file
318+
*/
319+
function isBuildInProgress(templatePath: string): boolean {
320+
const expirationDate = getRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath)
321+
if (expirationDate) {
322+
return Date.now() < parseInt(expirationDate)
323+
}
324+
return false
325+
}
326+
327+
/**
328+
* Throws an error if there's a build in progress for the provided template
329+
* @Param templatePath The path to the template.yaml file
330+
*/
331+
export function throwIfTemplateIsBeingBuilt(templatePath: string) {
332+
if (isBuildInProgress(templatePath)) {
333+
throw new ToolkitError('This template is already being built', { code: 'BuildInProgress' })
334+
}
335+
}
336+
337+
export async function registerTemplateBuild(templatePath: string) {
338+
const expirationDate = Date.now() + 5 * 60 * 1000 // five minutes
339+
await updateRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath, expirationDate.toString())
340+
}
341+
342+
export async function unregisterTemplateBuild(templatePath: string) {
343+
await updateRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath, undefined)
344+
}

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,6 @@ export async function updateRecentResponse(
110110
}
111111
}
112112

113-
/**
114-
* Returns true if there's an ongoing build process for the provided template, false otherwise
115-
* @Param templatePath The path to the template.yaml file
116-
*/
117-
function isBuildInProgress(templatePath: string): boolean {
118-
const expirationDate = getRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath)
119-
if (expirationDate) {
120-
return Date.now() < parseInt(expirationDate)
121-
}
122-
return false
123-
}
124-
125-
/**
126-
* Throws an error if there's a build in progress for the provided template
127-
* @Param templatePath The path to the template.yaml file
128-
*/
129-
export function throwIfTemplateIsBeingBuilt(templatePath: string) {
130-
if (isBuildInProgress(templatePath)) {
131-
throw new ToolkitError('This template is already being built', { code: 'BuildInProgress' })
132-
}
133-
}
134-
135-
export async function registerTemplateBuild(templatePath: string) {
136-
const expirationDate = Date.now() + 5 * 60 * 1000 // five minutes
137-
await updateRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath, expirationDate.toString())
138-
}
139-
140-
export async function unregisterTemplateBuild(templatePath: string) {
141-
await updateRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath, undefined)
142-
}
143-
144113
export function getSamCliErrorMessage(stderr: string): string {
145114
// Split the stderr string by newline, filter out empty lines, and get the last line
146115
const lines = stderr

0 commit comments

Comments
 (0)