Skip to content

Commit 63c8067

Browse files
committed
change to use storage path
Signed-off-by: shmck <[email protected]>
1 parent 1e81199 commit 63c8067

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

src/environment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ export const CONTENT_SECURITY_POLICY_EXEMPTIONS: string | null =
4848
export const WEBHOOK_TOKEN = process.env.CODEROAD_WEBHOOK_TOKEN || null
4949

5050
// a path to write session state to a file. Useful for maintaining session across containers
51-
export const SESSION_FILE_PATH = process.env.CODEROAD_SESSION_FILE_PATH || null
51+
export const SESSION_STORAGE_PATH = process.env.CODEROAD_STORAGE_PATH || null

src/services/context/state/Position.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Position {
1919
setTutorial(workspaceState: vscode.Memento, tutorial: TT.Tutorial): void {
2020
this.storage = new Storage<T.Position>({
2121
key: `coderoad:position:${tutorial.id}:${tutorial.version}`,
22+
filePath: 'coderoad_position',
2223
storage: workspaceState,
2324
defaultValue,
2425
})

src/services/context/state/Tutorial.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Tutorial {
99
constructor(workspaceState: vscode.Memento) {
1010
this.storage = new Storage<TT.Tutorial | null>({
1111
key: 'coderoad:currentTutorial',
12+
filePath: 'coderoad_tutorial',
1213
storage: workspaceState,
1314
defaultValue: null,
1415
})

src/services/node/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ export const removeFile = (...paths: string[]) => {
3535
export const readFile = (...paths: string[]): Promise<string | void> => {
3636
const filePath = getWorkspacePath(...paths)
3737
return asyncReadFile(getWorkspacePath(...paths), 'utf8').catch((err) => {
38-
console.warn(`Failed to read from ${filePath}`)
38+
console.warn(`Failed to read from ${filePath}: ${err.message}`)
3939
})
4040
}
4141

4242
export const writeFile = (data: any, ...paths: string[]): Promise<void> => {
4343
const filePath = getWorkspacePath(...paths)
4444
return asyncWriteFile(filePath, JSON.stringify(data)).catch((err) => {
45-
console.warn(`Failed to write to ${filePath}`)
45+
console.warn(`Failed to write to ${filePath}: ${err.message}`)
4646
})
4747
}

src/services/storage/index.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode'
22
import { readFile, writeFile } from '../node'
3-
import { SESSION_FILE_PATH } from '../../environment'
3+
import { SESSION_STORAGE_PATH } from '../../environment'
44

55
// NOTE: localStorage is not available on client
66
// and must be stored in editor
@@ -10,37 +10,47 @@ import { SESSION_FILE_PATH } from '../../environment'
1010
// forcing it to be passed in through activation and down to other tools
1111
class Storage<T> {
1212
private key: string
13+
private filePath: string
1314
private storage: vscode.Memento
1415
private defaultValue: T
15-
constructor({ key, storage, defaultValue }: { key: string; storage: vscode.Memento; defaultValue: T }) {
16+
constructor({
17+
key,
18+
filePath,
19+
storage,
20+
defaultValue,
21+
}: {
22+
key: string
23+
filePath: string
24+
storage: vscode.Memento
25+
defaultValue: T
26+
}) {
1627
this.storage = storage
1728
this.key = key
29+
this.filePath = filePath
1830
this.defaultValue = defaultValue
1931
}
2032
public get = async (): Promise<T> => {
2133
const value: string | undefined = await this.storage.get(this.key)
2234
if (value) {
2335
return JSON.parse(value)
24-
} else if (SESSION_FILE_PATH) {
36+
} else if (SESSION_STORAGE_PATH) {
2537
try {
2638
// optionally read from file as a fallback to local storage
27-
const sessionFile = await readFile(SESSION_FILE_PATH)
39+
const sessionFile = await readFile(SESSION_STORAGE_PATH, `${this.filePath}.json`)
2840
if (!sessionFile) {
2941
throw new Error('No session file found')
3042
}
31-
const session = JSON.parse(sessionFile)
43+
const data: T = JSON.parse(sessionFile)
3244

33-
if (session) {
34-
const keys = Object.keys(session)
45+
if (data) {
3546
// validate session
47+
const keys = Object.keys(data)
3648
if (keys.length) {
37-
// should only be one
38-
this.key = keys[0]
39-
return session[this.key]
49+
return data
4050
}
4151
}
4252
} catch (err) {
43-
console.warn(`Failed to read or parse session file: ${SESSION_FILE_PATH}`)
53+
console.warn(`Failed to read or parse session file: ${SESSION_STORAGE_PATH}/${this.filePath}.json`)
4454
}
4555
}
4656
return this.defaultValue
@@ -61,12 +71,12 @@ class Storage<T> {
6171
this.writeToSessionFile(next)
6272
}
6373
public writeToSessionFile(data: string) {
64-
// optionally write to file
65-
if (SESSION_FILE_PATH) {
74+
// optionally write state to file, useful when state cannot be controlled across containers
75+
if (SESSION_STORAGE_PATH) {
6676
try {
67-
writeFile({ [this.key]: data }, SESSION_FILE_PATH)
77+
writeFile(data, SESSION_STORAGE_PATH, `${this.filePath}.json`)
6878
} catch (err: any) {
69-
console.warn(`Failed to write coderoad session to path: ${SESSION_FILE_PATH}`)
79+
console.warn(`Failed to write coderoad session to path: ${SESSION_STORAGE_PATH}/${this.filePath}.json`)
7080
}
7181
}
7282
}

0 commit comments

Comments
 (0)