Skip to content

Commit 7eff077

Browse files
committed
refactor(activationReloadState): use globalState abstraction
1 parent e4c21dc commit 7eff077

File tree

5 files changed

+68
-84
lines changed

5 files changed

+68
-84
lines changed

packages/core/src/lambda/commands/createNewSamApp.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export async function resumeCreateNewSamApp(
6363
let samVersion: string | undefined
6464
const samInitState: SamInitState | undefined = activationReloadState.getSamInitState()
6565
try {
66+
getLogger().debug('SAM: resumeCreateNewSamApp')
6667
const templateUri = vscode.Uri.file(samInitState!.template!)
6768
const readmeUri = vscode.Uri.file(samInitState!.readme!)
6869
const folder = vscode.workspace.getWorkspaceFolder(templateUri)

packages/core/src/shared/activationReloadState.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as vscode from 'vscode'
76
import { Runtime } from 'aws-sdk/clients/lambda'
87
import globals from './extensionGlobals'
98

10-
export const activationTemplatePathKey = 'ACTIVATION_TEMPLATE_PATH_KEY'
11-
export const activationLaunchPathKey = 'ACTIVATION_LAUNCH_PATH_KEY'
12-
export const samInitRuntimeKey = 'SAM_INIT_RUNTIME_KEY'
13-
export const samInitImageBooleanKey = 'SAM_INIT_IMAGE_BOOLEAN_KEY'
14-
export const samInitArchKey = 'SAM_INIT_ARCH_KEY'
15-
169
export interface SamInitState {
1710
template: string | undefined
1811
readme: string | undefined
@@ -27,31 +20,27 @@ export interface SamInitState {
2720
export class ActivationReloadState {
2821
public getSamInitState(): SamInitState | undefined {
2922
return {
30-
template: this.extensionContext.globalState.get<string>(activationTemplatePathKey),
31-
readme: this.extensionContext.globalState.get<string>(activationLaunchPathKey),
32-
runtime: this.extensionContext.globalState.get<string>(samInitRuntimeKey),
33-
architecture: this.extensionContext.globalState.get<string>(samInitArchKey),
34-
isImage: this.extensionContext.globalState.get<boolean>(samInitImageBooleanKey),
23+
template: globals.globalState.get<string>('ACTIVATION_TEMPLATE_PATH_KEY'),
24+
readme: globals.globalState.get<string>('ACTIVATION_LAUNCH_PATH_KEY'),
25+
runtime: globals.globalState.get<string>('SAM_INIT_RUNTIME_KEY'),
26+
architecture: globals.globalState.get<string>('SAM_INIT_ARCH_KEY'),
27+
isImage: globals.globalState.get<boolean>('SAM_INIT_IMAGE_BOOLEAN_KEY'),
3528
}
3629
}
3730

3831
public setSamInitState(state: SamInitState): void {
39-
void this.extensionContext.globalState.update(activationTemplatePathKey, state.template)
40-
void this.extensionContext.globalState.update(activationLaunchPathKey, state.readme)
41-
void this.extensionContext.globalState.update(samInitRuntimeKey, state.runtime)
42-
void this.extensionContext.globalState.update(samInitArchKey, state.architecture)
43-
void this.extensionContext.globalState.update(samInitImageBooleanKey, state.isImage)
32+
void globals.globalState.update('ACTIVATION_TEMPLATE_PATH_KEY', state.template)
33+
void globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', state.readme)
34+
void globals.globalState.update('SAM_INIT_RUNTIME_KEY', state.runtime)
35+
void globals.globalState.update('SAM_INIT_ARCH_KEY', state.architecture)
36+
void globals.globalState.update('SAM_INIT_IMAGE_BOOLEAN_KEY', state.isImage)
4437
}
4538

4639
public clearSamInitState(): void {
47-
void this.extensionContext.globalState.update(activationTemplatePathKey, undefined)
48-
void this.extensionContext.globalState.update(activationLaunchPathKey, undefined)
49-
void this.extensionContext.globalState.update(samInitRuntimeKey, undefined)
50-
void this.extensionContext.globalState.update(samInitArchKey, undefined)
51-
void this.extensionContext.globalState.update(samInitImageBooleanKey, undefined)
52-
}
53-
54-
protected get extensionContext(): vscode.ExtensionContext {
55-
return globals.context
40+
void globals.globalState.update('ACTIVATION_TEMPLATE_PATH_KEY', undefined)
41+
void globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', undefined)
42+
void globals.globalState.update('SAM_INIT_RUNTIME_KEY', undefined)
43+
void globals.globalState.update('SAM_INIT_ARCH_KEY', undefined)
44+
void globals.globalState.update('SAM_INIT_IMAGE_BOOLEAN_KEY', undefined)
5645
}
5746
}

packages/core/src/shared/filesystemUtilities.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ export async function fileExists(p: string): Promise<boolean> {
7171
}
7272

7373
/**
74-
* @description Wraps readFileAsync and resolves the Buffer to a string for convenience
75-
*
76-
* @param filePath filename to read
77-
* @param encoding Optional - file encoding
78-
*
79-
* @returns the contents of the file as a string
74+
* @deprecated use {@link fs} exist methods instead.
8075
*/
8176
export async function readFileAsString(pathLike: string): Promise<string> {
8277
return fs.readFileAsString(pathLike)

packages/core/src/shared/globalState.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import { getLogger } from './logger/logger'
88
import * as redshift from '../awsService/redshift/models/models'
99
import { TypeConstructor, cast } from './utilities/typeConstructors'
1010

11+
type samInitStateKey =
12+
| 'ACTIVATION_TEMPLATE_PATH_KEY'
13+
| 'ACTIVATION_LAUNCH_PATH_KEY'
14+
| 'SAM_INIT_RUNTIME_KEY'
15+
| 'SAM_INIT_IMAGE_BOOLEAN_KEY'
16+
| 'SAM_INIT_ARCH_KEY'
17+
1118
type globalKey =
19+
| samInitStateKey
1220
| 'aws.downloadPath'
1321
| 'aws.lastTouchedS3Folder'
1422
| 'aws.lastUploadedToS3Folder'
@@ -87,6 +95,8 @@ export class GlobalState implements vscode.Memento {
8795
* {@link String}, {@link Boolean}, etc.
8896
* @param defaultVal Value returned if `key` has no value.
8997
*/
98+
tryGet<T>(key: globalKey, type: TypeConstructor<T>): T | undefined
99+
tryGet<T>(key: globalKey, type: TypeConstructor<T>, defaulVal: T): T
90100
tryGet<T>(key: globalKey, type: TypeConstructor<T>, defaulVal?: T): T | undefined {
91101
try {
92102
return this.getStrict(key, type, defaulVal)
@@ -150,8 +160,7 @@ export class GlobalState implements vscode.Memento {
150160
throw new Error()
151161
}
152162
return v
153-
},
154-
undefined
163+
}
155164
)
156165
return all?.[warehouseArn]
157166
}
@@ -183,21 +192,17 @@ export class GlobalState implements vscode.Memento {
183192
* @param id Session id
184193
*/
185194
getSsoSessionCreationDate(id: string): number | undefined {
186-
const all = this.tryGet<Record<string, number>>(
187-
'#sessionCreationDates',
188-
(v) => {
189-
if (v !== undefined && typeof v !== 'object') {
190-
throw new Error()
191-
}
192-
const item = (v as any)?.[id]
193-
// Requested item must be a number.
194-
if (item !== undefined && typeof item !== 'number') {
195-
throw new Error()
196-
}
197-
return v
198-
},
199-
undefined
200-
)
195+
const all = this.tryGet<Record<string, number>>('#sessionCreationDates', v => {
196+
if (v !== undefined && typeof v !== 'object') {
197+
throw new Error()
198+
}
199+
const item = (v as any)?.[id]
200+
// Requested item must be a number.
201+
if (item !== undefined && typeof item !== 'number') {
202+
throw new Error()
203+
}
204+
return v
205+
})
201206
return all?.[id]
202207
}
203208
}

packages/core/src/test/shared/activationReloadState.test.ts

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
'use strict'
77

88
import assert from 'assert'
9-
import {
10-
activationLaunchPathKey,
11-
ActivationReloadState,
12-
samInitRuntimeKey,
13-
samInitImageBooleanKey,
14-
activationTemplatePathKey,
15-
} from '../../shared/activationReloadState'
9+
import { ActivationReloadState } from '../../shared/activationReloadState'
1610
import globals, { checkDidReload } from '../../shared/extensionGlobals'
1711

1812
describe('ActivationReloadState', async function () {
@@ -27,10 +21,10 @@ describe('ActivationReloadState', async function () {
2721
})
2822

2923
it('decides globals.didReload', async function () {
30-
await globals.context.globalState.update(activationLaunchPathKey, undefined)
24+
await globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', undefined)
3125
assert.strictEqual(checkDidReload(globals.context), false)
3226

33-
await globals.context.globalState.update(activationLaunchPathKey, '/some/path')
27+
await globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', '/some/path')
3428
assert.strictEqual(checkDidReload(globals.context), true)
3529
})
3630

@@ -45,22 +39,22 @@ describe('ActivationReloadState', async function () {
4539
})
4640

4741
assert.strictEqual(
48-
globals.context.globalState.get(activationLaunchPathKey),
42+
globals.globalState.get('ACTIVATION_LAUNCH_PATH_KEY'),
4943
'somepath',
5044
'Unexpected Launch Path value was set'
5145
)
5246
assert.strictEqual(
53-
globals.context.globalState.get(activationTemplatePathKey),
47+
globals.globalState.get('ACTIVATION_TEMPLATE_PATH_KEY'),
5448
'sometemplate',
5549
'Unexpected Template Path value was set'
5650
)
5751
assert.strictEqual(
58-
globals.context.globalState.get(samInitRuntimeKey),
52+
globals.globalState.get('SAM_INIT_RUNTIME_KEY'),
5953
undefined,
6054
'Unexpected init runtime key value was set'
6155
)
6256
assert.strictEqual(
63-
globals.context.globalState.get(samInitImageBooleanKey),
57+
globals.globalState.get('SAM_INIT_IMAGE_BOOLEAN_KEY'),
6458
false,
6559
'Unexpected init image boolean value was set'
6660
)
@@ -76,22 +70,22 @@ describe('ActivationReloadState', async function () {
7670
})
7771

7872
assert.strictEqual(
79-
globals.context.globalState.get(activationLaunchPathKey),
73+
globals.globalState.get('ACTIVATION_LAUNCH_PATH_KEY'),
8074
'somepath',
8175
'Unexpected Launch Path value was set'
8276
)
8377
assert.strictEqual(
84-
globals.context.globalState.get(activationTemplatePathKey),
78+
globals.globalState.get('ACTIVATION_TEMPLATE_PATH_KEY'),
8579
'sometemplate',
8680
'Unexpected Template Path value was set'
8781
)
8882
assert.strictEqual(
89-
globals.context.globalState.get(samInitRuntimeKey),
83+
globals.globalState.get('SAM_INIT_RUNTIME_KEY'),
9084
'someruntime',
9185
'Unexpected init runtime value was set'
9286
)
9387
assert.strictEqual(
94-
globals.context.globalState.get(samInitImageBooleanKey),
88+
globals.globalState.get('SAM_INIT_IMAGE_BOOLEAN_KEY'),
9589
false,
9690
'Unexpected init image boolean value was set'
9791
)
@@ -107,22 +101,22 @@ describe('ActivationReloadState', async function () {
107101
})
108102

109103
assert.strictEqual(
110-
globals.context.globalState.get(activationLaunchPathKey),
104+
globals.globalState.get('ACTIVATION_LAUNCH_PATH_KEY'),
111105
'somepath',
112106
'Unexpected Launch Path value was set'
113107
)
114108
assert.strictEqual(
115-
globals.context.globalState.get(activationTemplatePathKey),
109+
globals.globalState.get('ACTIVATION_TEMPLATE_PATH_KEY'),
116110
'sometemplate',
117111
'Unexpected Template Path value was set'
118112
)
119113
assert.strictEqual(
120-
globals.context.globalState.get(samInitRuntimeKey),
114+
globals.globalState.get('SAM_INIT_RUNTIME_KEY'),
121115
'someruntime',
122116
'Unexpected init runtime value was set'
123117
)
124118
assert.strictEqual(
125-
globals.context.globalState.get(samInitImageBooleanKey),
119+
globals.globalState.get('SAM_INIT_IMAGE_BOOLEAN_KEY'),
126120
true,
127121
'Unexpected init image boolean value was set'
128122
)
@@ -131,9 +125,9 @@ describe('ActivationReloadState', async function () {
131125

132126
describe('getSamInitState', async function () {
133127
it('path defined, without runtime', async function () {
134-
await globals.context.globalState.update(activationLaunchPathKey, 'getsomepath')
135-
await globals.context.globalState.update(activationTemplatePathKey, 'gettemplatepath')
136-
await globals.context.globalState.update(samInitRuntimeKey, undefined)
128+
await globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', 'getsomepath')
129+
await globals.globalState.update('ACTIVATION_TEMPLATE_PATH_KEY', 'gettemplatepath')
130+
await globals.globalState.update('SAM_INIT_RUNTIME_KEY', undefined)
137131

138132
assert.strictEqual(
139133
activationReloadState.getSamInitState()?.readme,
@@ -158,9 +152,9 @@ describe('ActivationReloadState', async function () {
158152
})
159153

160154
it('path defined, with runtime', async function () {
161-
await globals.context.globalState.update(activationLaunchPathKey, 'getsomepath')
162-
await globals.context.globalState.update(activationTemplatePathKey, 'gettemplatepath')
163-
await globals.context.globalState.update(samInitRuntimeKey, 'getsomeruntime')
155+
await globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', 'getsomepath')
156+
await globals.globalState.update('ACTIVATION_TEMPLATE_PATH_KEY', 'gettemplatepath')
157+
await globals.globalState.update('SAM_INIT_RUNTIME_KEY', 'getsomeruntime')
164158

165159
assert.strictEqual(
166160
activationReloadState.getSamInitState()?.readme,
@@ -185,10 +179,10 @@ describe('ActivationReloadState', async function () {
185179
})
186180

187181
it('path defined, with runtime and isImage', async function () {
188-
await globals.context.globalState.update(activationLaunchPathKey, 'getsomepath')
189-
await globals.context.globalState.update(activationTemplatePathKey, 'gettemplatepath')
190-
await globals.context.globalState.update(samInitRuntimeKey, 'getsomeruntime')
191-
await globals.context.globalState.update(samInitImageBooleanKey, true)
182+
await globals.globalState.update('ACTIVATION_LAUNCH_PATH_KEY', 'getsomepath')
183+
await globals.globalState.update('ACTIVATION_TEMPLATE_PATH_KEY', 'gettemplatepath')
184+
await globals.globalState.update('SAM_INIT_RUNTIME_KEY', 'getsomeruntime')
185+
await globals.globalState.update('SAM_INIT_IMAGE_BOOLEAN_KEY', true)
192186

193187
assert.strictEqual(
194188
activationReloadState.getSamInitState()?.readme,
@@ -224,25 +218,25 @@ describe('ActivationReloadState', async function () {
224218
activationReloadState.clearSamInitState()
225219

226220
assert.strictEqual(
227-
globals.context.globalState.get(activationLaunchPathKey),
221+
globals.globalState.get('ACTIVATION_LAUNCH_PATH_KEY'),
228222
undefined,
229223
'Expected launch path to be cleared (undefined)'
230224
)
231225

232226
assert.strictEqual(
233-
globals.context.globalState.get(activationTemplatePathKey),
227+
globals.globalState.get('ACTIVATION_TEMPLATE_PATH_KEY'),
234228
undefined,
235229
'Expected template path to be cleared (undefined)'
236230
)
237231

238232
assert.strictEqual(
239-
globals.context.globalState.get(samInitRuntimeKey),
233+
globals.globalState.get('SAM_INIT_RUNTIME_KEY'),
240234
undefined,
241235
'Expected runtime key to be cleared (undefined)'
242236
)
243237

244238
assert.strictEqual(
245-
globals.context.globalState.get(samInitImageBooleanKey),
239+
globals.globalState.get('SAM_INIT_IMAGE_BOOLEAN_KEY'),
246240
undefined,
247241
'Expected isImage key to be cleared (undefined)'
248242
)

0 commit comments

Comments
 (0)