Skip to content

Commit 0097c6f

Browse files
committed
refactor(schemas): use globalState abstraction
1 parent 5d58fca commit 0097c6f

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

packages/core/src/shared/globalState.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { TypeConstructor, cast } from './utilities/typeConstructors'
1111
type ToolId = 'codecatalyst' | 'codewhisperer' | 'testId'
1212
export type ToolIdStateKey = `${ToolId}.savedConnectionId`
1313

14+
export type JsonSchemasKey = 'devfileSchemaVersion' | 'samAndCfnSchemaVersion'
15+
1416
type samInitStateKey =
1517
| 'ACTIVATION_TEMPLATE_PATH_KEY'
1618
| 'ACTIVATION_LAUNCH_PATH_KEY'
@@ -24,6 +26,7 @@ type globalKey =
2426
| samInitStateKey
2527
| stepFunctionsKey
2628
| ToolIdStateKey
29+
| JsonSchemasKey
2730
| 'aws.amazonq.codewhisperer.newCustomizations'
2831
| 'aws.amazonq.hasShownWalkthrough'
2932
| 'aws.amazonq.showTryChatCodeLens'

packages/core/src/shared/schemas.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { AWS_SCHEME } from './constants'
1818
import fs from '../shared/fs/fs'
1919
import { normalizeVSCodeUri } from './utilities/vsCodeUtils'
2020
import { telemetry } from './telemetry/telemetry'
21+
import { JsonSchemasKey } from './globalState'
2122

2223
// Note: this file is currently 12+ MB. When requesting it, specify compression/gzip.
2324
export const samAndCfnSchemaUrl =
@@ -150,7 +151,6 @@ export async function getDefaultSchemas(): Promise<Schemas | undefined> {
150151

151152
// Sam schema is a superset of Cfn schema, so we can use it for both
152153
const samAndCfnSchemaDestinationUri = GlobalStorage.samAndCfnSchemaDestinationUri()
153-
const samAndCfnCacheKey = 'samAndCfnSchemaVersion'
154154

155155
const schemas: Schemas = {}
156156

@@ -159,8 +159,7 @@ export async function getDefaultSchemas(): Promise<Schemas | undefined> {
159159
destination: samAndCfnSchemaDestinationUri,
160160
eTag: undefined,
161161
url: samAndCfnSchemaUrl,
162-
cacheKey: samAndCfnCacheKey,
163-
extensionContext: globals.context,
162+
cacheKey: 'samAndCfnSchemaVersion',
164163
title: schemaPrefix + 'cloudformation.schema.json',
165164
})
166165
schemas['cfn'] = samAndCfnSchemaDestinationUri
@@ -173,8 +172,7 @@ export async function getDefaultSchemas(): Promise<Schemas | undefined> {
173172
destination: samAndCfnSchemaDestinationUri,
174173
eTag: undefined,
175174
url: samAndCfnSchemaUrl,
176-
cacheKey: samAndCfnCacheKey,
177-
extensionContext: globals.context,
175+
cacheKey: 'samAndCfnSchemaVersion',
178176
title: schemaPrefix + 'sam.schema.json',
179177
})
180178
schemas['sam'] = samAndCfnSchemaDestinationUri
@@ -188,7 +186,6 @@ export async function getDefaultSchemas(): Promise<Schemas | undefined> {
188186
version: devfileSchemaVersion,
189187
url: `https://raw.githubusercontent.com/devfile/api/${devfileSchemaVersion}/schemas/latest/devfile.json`,
190188
cacheKey: 'devfileSchemaVersion',
191-
extensionContext: globals.context,
192189
title: schemaPrefix + 'devfile.schema.json',
193190
})
194191
schemas['devfile'] = devfileSchemaUri
@@ -206,17 +203,15 @@ export async function getDefaultSchemas(): Promise<Schemas | undefined> {
206203
* @param params.version Remote version
207204
* @param params.url Url to fetch from
208205
* @param params.cacheKey Cache key to check version against
209-
* @param params.extensionContext VSCode extension context
210206
*/
211207
export async function updateSchemaFromRemote(params: {
212208
destination: vscode.Uri
213209
version?: string
214210
url: string
215-
cacheKey: string
216-
extensionContext: vscode.ExtensionContext
211+
cacheKey: JsonSchemasKey
217212
title: string
218213
}): Promise<void> {
219-
const cachedVersion = params.extensionContext.globalState.get<string>(params.cacheKey)
214+
const cachedVersion = globals.globalState.get<string>(params.cacheKey)
220215
const outdated = params.version && params.version !== cachedVersion
221216

222217
// Check that the cached file actually can be fetched. Else we might
@@ -257,17 +252,15 @@ export async function updateSchemaFromRemote(params: {
257252
* @param params.eTag E-Tag to send with fetch request. If this matches the url's it means we can use our cache.
258253
* @param params.url Url to fetch from
259254
* @param params.cacheKey Cache key to check version against
260-
* @param params.extensionContext VSCode extension context
261255
*/
262256
export async function updateSchemaFromRemoteETag(params: {
263257
destination: vscode.Uri
264258
eTag?: string
265259
url: string
266-
cacheKey: string
267-
extensionContext: vscode.ExtensionContext
260+
cacheKey: JsonSchemasKey
268261
title: string
269262
}): Promise<void> {
270-
const cachedETag = params.extensionContext.globalState.get<string>(params.cacheKey)
263+
const cachedETag = globals.globalState.get<string>(params.cacheKey)
271264

272265
// Check that the cached file actually can be fetched. Else we might
273266
// never update the cache.
@@ -320,18 +313,15 @@ async function doCacheContent(
320313
destination: vscode.Uri
321314
version?: string
322315
url: string
323-
cacheKey: string
324-
extensionContext: vscode.ExtensionContext
316+
cacheKey: JsonSchemasKey
325317
title: string
326318
}
327319
): Promise<void> {
328320
const parsedFile = { ...JSON.parse(content), title: params.title }
329321
const dir = vscode.Uri.joinPath(params.destination, '..')
330322
await fs.mkdir(dir)
331323
await fs.writeFile(params.destination.fsPath, JSON.stringify(parsedFile))
332-
await params.extensionContext.globalState.update(params.cacheKey, params.version).then(undefined, (err) => {
333-
getLogger().warn(`schemas: failed to update cache key for "${params.title}": ${err?.message}`)
334-
})
324+
await globals.globalState.update(params.cacheKey, params.version)
335325
}
336326

337327
/**

0 commit comments

Comments
 (0)