Skip to content

Commit 63c8067

Browse files
committed
change to use storage path
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
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
exportconstWEBHOOK_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-
exportconstSESSION_FILE_PATH=process.env.CODEROAD_SESSION_FILE_PATH||null
51+
exportconstSESSION_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=newStorage<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=newStorage<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
exportconstreadFile=(...paths: string[]): Promise<string|void>=>{
3636
constfilePath=getWorkspacePath(...paths)
3737
returnasyncReadFile(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
exportconstwriteFile=(data: any, ...paths: string[]): Promise<void>=>{
4343
constfilePath=getWorkspacePath(...paths)
4444
returnasyncWriteFile(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*asvscodefrom'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
classStorage<T>{
1212
privatekey: string
13+
privatefilePath: string
1314
privatestorage: vscode.Memento
1415
privatedefaultValue: 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
publicget=async(): Promise<T>=>{
2133
constvalue: string|undefined=awaitthis.storage.get(this.key)
2234
if(value){
2335
returnJSON.parse(value)
24-
}elseif(SESSION_FILE_PATH){
36+
}elseif(SESSION_STORAGE_PATH){
2537
try{
2638
// optionally read from file as a fallback to local storage
27-
constsessionFile=awaitreadFile(SESSION_FILE_PATH)
39+
constsessionFile=awaitreadFile(SESSION_STORAGE_PATH,`${this.filePath}.json`)
2840
if(!sessionFile){
2941
thrownewError('No session file found')
3042
}
31-
constsession=JSON.parse(sessionFile)
43+
constdata: T=JSON.parse(sessionFile)
3244

33-
if(session){
34-
constkeys=Object.keys(session)
45+
if(data){
3546
// validate session
47+
constkeys=Object.keys(data)
3648
if(keys.length){
37-
// should only be one
38-
this.key=keys[0]
39-
returnsession[this.key]
49+
returndata
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
returnthis.defaultValue
@@ -61,12 +71,12 @@ class Storage<T> {
6171
this.writeToSessionFile(next)
6272
}
6373
publicwriteToSessionFile(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)
close